<jupyter_start><jupyter_text>Assignments for Task 0 Libraries to learn:1. Jupyter Notebook2. Python3. Numpy and Matplotlib4. Pandas5. Sklearn  0. Activating Python Virtual EnvironmentOnce you are done with software installation, you can activate your virtual environment by typing in your Terminal or Anaconda Prompt:`conda activate HC9999_stage1`You should see your terminal prompt change to HC9999_stage1. All the tasks below should be performed inside this environment.  1. Jupyter Notebook  1.1. Quick introduction to Jupyter NotebookJupyter Notebook has features like auto-completion, inline documentation, etc. which come handy when you are using lots of libraries in your code.You will learn how to use it on the way. But for starters:**1. Writing code**You can write code in the cells. To execute the code, press ```CTRL + ENTER```. To add a cell after executing current cell, use ```SHIFT + ENTER```.**2. Markdown in Jupyter notebook**You can use Markdown in Jupyter Notebook. Markdown is also written in cells but for that you need to convert code cell to Markdown cell. To do that go on a cell and press ```ESC``` key till the cell turns **BLUE** from **GREEN**. Now press ```M``` key and try to type anything and press ```SHIFT + ENTER```. To convert a cell back to code cell, use ```ESC``` till it turns **BLUE** and press ```Y``` key. **3. Auto-completion**Use ```TAB``` key to show suggestions for auto-complete.**4. Documentation**Use ```Shift + TAB``` keys to see documentation. The **more times you press TAB, you get to see more elaborate documentation**. 1.2. Resources 1. http://nbviewer.jupyter.org/github/jvns/pandas-cookbook/blob/master/cookbook/A%20quick%20tour%20of%20IPython%20Notebook.ipynb2. http://cs231n.github.io/ipython-tutorial/3. https://medium.com/codingthesmartway-com-blog/getting-started-with-jupyter-notebook-for-python-4e7082bd5d46  2. Python**Python is a programming language** that we will use for all higher level tasks. It is very easy to learn and use. If you are familiar with any programming languages like C or Java you wouldn't have any difficulty picking it up. For Python, follow this tutorial by Stanford University: http://cs231n.github.io/python-numpy-tutorial/Also see [official documentation](https://docs.python.org/3.6/) or google search for specific python stuff.  Assigment 1.1Create a function **sigmoid**. This should accept a list and return a list by evaluating sigmoid function on each element of the list.<jupyter_code>import math 

def sigmoid(x_list):
    '''
    Returns a list of sigmoids

    Sigmoid is defined as f(x) = 1/(1+e^-x)
    '''
    y_list = []
    for i in x_list:
        y_list.append(1.0/(1.0+math.exp(-i)))
    
    return y_list
x_list = [1,2,3,4,-4]
sigmoid(x_list)<jupyter_output><empty_output><jupyter_text>3. NumPy and Matplotlib**NumPy** is a numerical computing library for Python. You can use NumPy to do linear algebra operations easily in Python. It has a similar interface to other numerical computing softwares like Octave, MATLAB, etc.**Matplotlib** is a **plotting package for Python**. Soon enough in ML, we will have lots of columns of numbers and we might not be able to make sense of them by just looking at them. That is when Matplotlib is going to come handy. Matplotlib allows us to plot charts, histograms, images, etc. In simple words it is used for **data visualization**.The same tutorial we used for Python has enough of NumPy guide for us to start. Later we will use functions as needed and required. It also has basic Matplotlib usage guide.For NumPy and Matplotlib follow this tutorial by Stanford University to get started: http://cs231n.github.io/python-numpy-tutorial/  Assignment 1.2Complete functions for **forward pass** and **MSE**. Forward pass for linear regression and MSE is mean squared error calculated on predicted and actual output.<jupyter_code># NumPy Task
# Complete the following functions

# GIVEN:
# X and y_actual

# TODO:
# forward_pass function: Forward pass of linear regression
# mse function: calculates L2 loss between actual and predicted

import numpy as np

np.random.seed(seed=1)

# TODO: Complete the function
def forward_pass(W, x, b):
    '''
    Returns predicted y
    y_pred = W*x + b
    '''
    return (W*x) + b


# TODO: Complete the function
def mse(y_pred, y_actual):
    '''
    Returns MSE = (1/N)*(Summation(i = 1 to N)[(y_pred - y_actual)^2])
    '''
    return np.mean(np.square(y_pred-y_actual))
    
    
# Generate data
# Lets assume input is a table of size 2000 * 2 where 2000 indicates number of samples and 2 are attributes, 
# one dependent and one independent.
x = np.random.randn(2000, 1) # independent
y_actual = 2*x + 5 + np.random.randn(2000, 1) # dependent


# TODO:
# Divide dataset: 1500 samples in training set and 500 samples in test set
x_train = x[:1500]
y_train = y_actual[:1500]

x_test = x[1500:]
y_test = y_actual[1500:]



# Initialize random weight
W = np.random.randn(1)
b = np.random.randn(1)

# Compute Forward Pass for x_train
y_pred_train = forward_pass(W, x_train, b)

# Compute MSE between 'y_train' and 'y_pred_train'
print('Mean Squared Error for y_train and y_pred_train:', mse(y_train, y_pred_train))

# Compute Forward Pass for x_test
y_pred_test = forward_pass(W, x_test, b)

# Compute MSE between 'y_test' and 'y_pred_test'
print('Mean Squared Error for y_test and y_pred_test:', mse(y_test, y_pred_test))<jupyter_output>Mean Squared Error for y_train and y_pred_train: 29.999152323334894
Mean Squared Error for y_test and y_pred_test: 29.114060058145174<jupyter_text>Visualization with Matplotlib<jupyter_code>import matplotlib.pyplot as plt
# Visualize predicted values

# make a scatter plot with x_train and y_pred_train
plt.scatter(x_train, y_pred_train)
# put a title
plt.title('Predicted')

# generate and show a graph
# unless we use show(), the graph is saved only in memory
plt.show()
# Visualize actual values

plt.scatter(x_train, y_train)
plt.title('Actual')
plt.show()<jupyter_output><empty_output><jupyter_text>4. Pandas**Pandas** is used for data analytics. With Pandas, you can load datasets as tables. You can then perform operations on the data now present as a table.You can do operations with Pandas that you can do in Numpy or Python lists and dictionaries, like slicing, indexing, etc. Resources1. ml-coding-tutorial.pdf is very brief and to the point for completing tasks2. Very good for overall understanding of pandas https://github.com/jvns/pandas-cookbook3. Tutorials from pandas official website http://pandas.pydata.org/pandas-docs/stable/tutorials.html<jupyter_code>import pandas as pd

# we will read and check our data
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

# pandas can load data from URIs like URLs, Filesystems, etc
# try downloading some CSV files and load it with pandas
iris.head()
# This function will show simple stats for loaded dataset
iris.describe()
# Checking data types of individual columns
iris.dtypes<jupyter_output><empty_output><jupyter_text>You can use indexing like in numpy and dictionaries in pandas too. There are also iloc, loc and ix for indexing.<jupyter_code># You can use loc, iloc and ix to select single or multiple rows
print('Single row\n------------\n' + str(iris.iloc[0]))
print('\nMultiple rows\n---------------\n' + str(iris.iloc[[0, 5]]))
# Pandas has dataframes and series as major data structures
# This is a Series
type(iris.loc[1]) # single row
# This is a DataFrame
type(iris.loc[[1,2]]) # multiple rows
iris['sepal_length'] # single column
iris[['sepal_length', 'sepal_width']] # multiple columns
iris['species'].unique()<jupyter_output><empty_output><jupyter_text>5. Sklearn**Scikit learn** is a library for machine learning. It has several model architectures which can learn data that you provide. Even if you are writing your own model architectures or algorithms, sklearn comes in handy for data pre-processing operations like normalization and calculating errors and accuracies, etc.  Resources1. ml-coding-tutorial.pdf is very brief and to the point for completing tasks2. Tutorials from sklearn official website http://scikit-learn.org/stable/tutorial/index.html<jupyter_code>from sklearn.linear_model import LinearRegression<jupyter_output><empty_output><jupyter_text>We will now fit the above **"x"** and **"y_actual"** values and again evaluate the loss after fitting unlike before where we never trained the algorithm.<jupyter_code># import the model and create an object of it
model = LinearRegression()

# every model or estimator as sklearn likes to call it will have a fit method
# use it with train data
model.fit(x_train, y_train)

# similarly the model will have a predict function to make predictions on new data
# evaluate model accuracy with train and test data
y_pred = model.predict(x_test)
print('Mean Squared Error:', mse(y_pred, y_test)) # notice the difference in values
plt.scatter(x_test, y_pred)
plt.title('Predicted')
plt.show()
plt.scatter(x_test, y_test)
plt.title('Actual')
plt.show()<jupyter_output><empty_output><jupyter_text>Assignment 1.3Use pandas to load iris dataset. Iris dataset has 3 types flowers in it, you can see from above. Your task is to figure out name of the flower given other parameters. Tasks:1. Load iris dataset as pandas dataframe.2. Shuffle the dataset.3. Seperate the dataframe into independent X and dependent y parameters. 4. Split X and y into train and test set. Use train_test_split from sklearn.model_selection.5. Use SVC classifier from sklearn. Checkout [sklearn docs](http://scikit-learn.org/stable/documentation.html) or just google how to use SVC. The interface is similar to LinearRegression.6. Fit the train data on it.7. Predict y on X_test.8. Find loss.<jupyter_code># 1. load pandas library and load iris data set as a dataframe
import pandas as pd

data = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')

# 2. Data-preprocessing: shuffle the dataframe using sample() function pre-defined on iris dataframe
# You can also google search how to shuffle dataframe
# Additionally comment on why do you need to shuffle the dataframe

data = data.sample(frac=1).reset_index(drop=True)
# 3. split iris dataframe columns into two set: 
# - dependent parameters X (four columns)
# - independent parameters y which is name of the flower (1 column)

# select all rows leaving only flower name column using iloc
# input variables; other than flower information
# output variable; only flower information

x = data.iloc[:,:4]
y = data.iloc[:,-1]
# 4. divide X and y into train and test set
# use train_test_split from sklearn.model_selection

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.3)
# 5. import the model


# 6. Fit the model
# use SVC from sklearn.svm 

from sklearn.svm import SVC

model = SVC()
model.fit(x_train, y_train)
# 7. Predict the test examples

pred = model.predict(x_test)
# 8. Find the accuracy on test samples using score() function

model.score(x_test, y_test)
# For a manual check you can store predicted values in a numpy array 
# and then use np.column_stack to view them side by side 
import numpy as np

y_pred_test = model.predict(x_test)
np.column_stack([pred, y_test])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup