I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Now run step 2-4 again. Note the followings:
1. You need not to modify the Loss_Regression and dLdW_Regression functions. They should generalise enough to work with multi-dimensional data
2. Since your training samples are normalised you can now use much higher learning rate e.g. k = 1e-2
3. Note that the plot function plot_scatter_and_line will not work in multivariate regression since it is designed for 1-D input only. Consider commenting it out.

Question: how many iterations are required to pass the threshold $\Delta L < 10^{-2}$ ?

Answer: you should need ~4000 iterations (and it will take a while to complete).
Task 1.5: (a) evaluate your learned model on the test set. (b) Suppose the house you want to sell has a size of 3000 $feet^2$, has 3 bedrooms, 2 baths, lot number 10000 and in NW direction. How much do you think its price would be? Hints: don't forget to normalise the test sample.

Answer: You will get ~150k USD only, much lower than the previous prediction based on size only. Your house has an advantage of size, but other parameters matter too.
Can you write Python code for it?

#enter your code here
# train multivariate regression model

# evaluate on test set

# test a single unknown sample


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Now we will perform gradient descent update procedure according to the following formula:
$$W = W - k\frac{\partial L}{\partial W}$$
Here we use fixed number of iterations and learning rate.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
np.random.seed(0)
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-8                                #learning rate
niters = 160                            #number of training iterations

#visualisation settings
vis_interval = niters/50
loss_collections = []
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
for i in range(niters):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if (i+1)% vis_interval == 0:
        ml_utils.plot_loss(axis_loss, range(i+1),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(i))
        fig.canvas.draw()
print("Learned parameters ", W.squeeze())


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Our goal is to build a house price prediction model that will approximate the price of a house given its size. To do it, we need to fit a linear line (y = ax + b) to the data above using linear regression. Remember the procedure:
1. Define training set
2. Define hypothesis function. Here $F(x,W) = Wx$
3. Loss function. Here $L(W) = \frac{1}{2N}{\sum_{i=1}^N{(F(x^{(i)},W)-z)^2}}$
4. Update procedure (gradient descent). $W = W - k\frac{\partial L}{\partial W}$
To speed up computation, you should avoid using loop when working with scripting languges e.g. Python, Matlab. Try using array/matrix instead. Here you are provided code for step 1 and 2. Your will be asked to implement step 3 and 4. Some skeleton code will be provided for your convenience.
Can you write Python code for it?

"""step 1: define training and test set X, Z."""
X_train, Z_train = ml_utils.extract_data(house_data_train, ['size'], ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, ['size'], ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise data, uncomment for now
#X_train, u, scale = ml_utils.normalise_data(X_train)
#X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print("size of X_train ", X_train.shape)
print("size of Z_train ", Z_train.shape)

"""step 2: define hypothesis function"""
def F_Regression(X, W):
    """
    Compute the hypothesis function y=F(x,W) in batch.
    input:  X  input array, must has size DxN (each column is one sample)
            W  parameter array, must has size 1xD
    output: linear multiplication of W*X, size 1xN
    """
    return np.dot(W,X)


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Question: Do you see any improvement in accuracy or convergence speed? Why?
Answer: Regularisation does help speed up the training (it adds stricter rules to the update procedure). Accuracy is the same (90%) is probably because (i) number of parameters to be trained is small (2-D) and so is the number of training samples; and (ii) the data are well separated. In a learning task which involves large number of parameters (such as neural network), regularisation proves a very efficient technique.
2.2 Multi-class classification
Here we are working with a very famous dataset. The Iris flower dataset has 150 samples of 3 Iris flower species (Setosa, Versicolour, and Virginica), each sample stores the height and length of its sepal and pedal in cm (4-D in total). Your task is to build a classifier to distinguish these flowers.
Can you write Python code for it?

#read the Iris dataset
iris = np.load('data/iris.npz')
X = iris['X']
Z = iris['Z']
print("size X ", X.shape)
print("size Z ", Z.shape)

#split train/test with ratio 120:30
TOTALS = Z.size
idx = np.random.permutation(TOTALS)
idx_train = idx[:120]
idx_test = idx[120:]

X_train = X[:, idx_train]
X_test = X[:, idx_test]
Z_train = Z[:, idx_train]
Z_test = Z[:, idx_test]

#normalise data
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

#concatenate array of "1s" to X array
X_train = np.concatenate((X_train, np.ones_like(Z_train)), axis = 0)
X_test = np.concatenate((X_test, np.ones_like(Z_test)), axis = 0)


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Quiz: you may notice the learning rate k is set to $10^{-8}$. Why is it too small? Try to play with several bigger values of k, you will soon find out that the training is extremely sensitive to the learning rate (the training easily  diverges or even causes "overflow" error with large k). Why is that?

Answer: It is because both the input (size of house) and output (price) have very large range of values, which result in very large gradient even with small k.
Task 1.3: Test your learned model. Suppose you want to sell a house of size 3000 $feat^2$, how much do you expect your house will cost?

Answer: you should get around 260k USD for that house.
Can you write Python code for it?

x = 3000
#enter your code here to predict house price for x


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Rerun the update loop again with the new loss and gradient functions. Note you may need to change the learning rate accordingly to have proper convergence. Now you have implemented both regularisation and momentum techniques, you can use a standard learning rate value of 0.01 which is widely used in practice.
Can you write Python code for it?

""" gradient descent with regularisation - parameter update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 0.01                                #learning rate
epsilon = 1e-6                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
for i in range(500):
    Y_train = F_Classification(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Classification_Reg(Y_train, Z_train, W)         #compute loss
    dLdW = dLdW_Classification_Reg(X_train, Y_train, Z_train, W) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_with_label_2d(axis_data, X_train, Z_train, W, "student score scatter plot")
        fig.canvas.draw()

print("Learned parameters ", W.squeeze())
print("Learning terminates after {} iterations".format(len(loss_collections)))


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Task 1.1: define the loss function for linear regression according to the following formula:
$$L = \frac{1}{2N}{\sum_{i=1}^N{(y^{(i)}-z^{(i)})^2}}$$
Please fill in the skeleton code below. Hints: (i) in Python numpy the square operator $x^2$ is implemented as x**2; (ii) try to use matrix form and avoid for loop
Can you write Python code for it?

"""step 3: loss function"""
def Loss_Regression(Y, Z):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
    output:  loss value, is a scalar
    """

    N = float(Z.size)
    diff = Y-Z
    return 1/(2*N)*np.dot(diff, diff.T).squeeze()
    # computes a multiplication: https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html

    # https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Task 1.2: compute gradient of the loss function w.r.t parameter W according to the following formula:<br>
$$\frac{\partial L}{\partial W} = \frac{1}{N}\sum_{i=1}^N{(y^{(i)}-z^{(i)})x^{(i)}}$$
Please fill in the skeleton code below.
Can you write Python code for it?

"""step 4: gradient descent - compute gradient"""
def dLdW_Regression(X, Y, Z):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   predicted values, has size 1xN
             Z   groundtruth values, has size 1xN
    output:  gradient, has same size as W
    """
    #enter your code here
    N = float(Z.size)
    return 1/N * (Y-Z).dot(X.T)


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Task 2.1: your first task is to define the hypothesis function. Do you remember what the hypothesis function in a binary classification task look like?
$$F(x,W) = \frac{1}{1+e^{-Wx}}$$
Can you write Python code for it?

def F_Classification(X, W):
    """
    Compute the hypothesis function given input array X and parameter W
    input:   X  input array, must has size DxN (each column is one sample)
             W  parameter array, must has size 1xD
    output:  sigmoid of W*X, size 1xN
    """
    #enter your code here

    pass


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
Task 2.4: one-vs-all. Train 3 binary one-vs-all classifiers $F_i$ (i=1-3), one for each class. An unknown feture vector x belongs to class i if:
$$max_i F(x,W_i)$$
Task 2.5: implement one-vs-one and compare the results with one-vs-all.
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/.ipynb_checkpoints/semcomp17_ml_tutorial-checkpoint.ipynb".
The first task is:
We achieve 90% accuracy (only two students have been misclassified). Not too bad, isn't it?
Task 2.3: regularisation
Now we want to add a regularisation term into the loss to prevent overfitting.
Regularisation loss is simply magnitude of the parameter vector W after removing the last element (i.e. bias doesn't count to regularisation).
$$L_R = \frac{1}{2}|W'|^2$$
where W' is W with the last element truncated.<br>
Now the total loss would be:
$$L(W) = -\frac{1}{N} \sum_{i=1}^N{[z^{(i)} log(F(x^{(i)},W)) + (1-z^{(i)})(log(1-F(x^{(i)},W))]} + \frac{1}{2}|W'|^2$$
The gradient become:
$$\frac{\partial L}{\partial W} = \frac{1}{N}(F(X,W) - Z)X^T + W''$$
where W'' is W with the last element change to 0.
Your task is to implement the loss and gradient function with added regularisation.
Can you write Python code for it?

"""step 3: loss function with regularisation"""
def Loss_Classification_Reg(Y, Z, W):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
             W   parameter vector, size 1xD
    output:  loss value, is a scalar
    """
    #enter your code here

    pass

"""step 4: gradient descent with regularisation - compute gradient"""
def dLdW_Classification_Reg(X, Y, Z, W):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   probability of label = 1, has size 1xN
             Z   groundtruth values, has size 1xN
             W   parameter vector, size 1xD
    output:  gradient, has same size as W
    """
    #enter your code here

    pass


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
1.3 Gradient descent with momentum
In the latest experiment, our training takes ~4000 iterations to converge. Now let try gradient descent with momentum to speed up the training. We will employ the following formula:
$$v_t = m*v_{t-1} + k\frac{\partial L}{\partial W}$$
$$W = W - v_t$$
Can you write Python code for it?

"""step 4: gradient descent with momentum - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-2                                #learning rate
epsilon = 1e-2                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
#axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        #ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Confirm that the error measurement on the test set has improved.
1.2 Multivariate regression
So far we assume the house price is affected by the size only. Now let consider also other fields "Bedrooms", "Baths", "lot" (location) and "NW" (whether or not the houses face Nothern West direction).

Important: now your feature vector is multi-dimensional, it is crucial to normalise your training set for gradient descent to converge properly. The code below is almost identical to the previous step 1, except it loads more fields and implements data normalisation.
Can you write Python code for it?

"""step 1: define training set X, Z."""
selected_fields = ['size', 'Bedrooms', 'Baths', 'lot', 'NW']
X_train, Z_train = ml_utils.extract_data(house_data_train, selected_fields, ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, selected_fields, ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print "size of X_train ", X_train.shape
print "size of Z_train ", Z_train.shape


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
For univariate regression, we are interested in the "size" parameter only. Let's extract necessary data and visualise it.
Can you write Python code for it?

X, Z = ml_utils.extract_data(house_data, ['size'], ['price'])
Z = Z/1000.0  #price has unit x1000 USD

plt.plot(X[0],Z[0], '.')
plt.xlabel('size (feet^2)')
plt.ylabel('price (USD x1000)')
plt.title('house data scatter plot')
plt.show()


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Now run step 2-4 again. Note the followings:
1. You need not to modify the Loss_Regression and dLdW_Regression functions. They should generalise enough to work with multi-dimensional data
2. Since your training samples are normalised you can now use much higher learning rate e.g. k = 1e-2
3. Note that the plot function plot_scatter_and_line will not work in multivariate regression since it is designed for 1-D input only. Consider commenting it out.

Question: how many iterations are required to pass the threshold $\Delta L < 10^{-2}$ ?

Answer: ~4000 iterations (and it will take a while to complete).
Task 1.5: (a) evaluate your learned model on the test set. (b) Suppose the house you want to sell has a size of 3000 $feet^2$, has 3 bedrooms, 2 baths, lot number 10000 and in NW direction. How much do you think its price would be? Hints: don't forget to normalise the test sample.

Answer: You will get ~150k USD only, much lower than the previous prediction based on size only. Your house has an advantage of size, but other parameters matter too.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
""" same code but change k = 1e-2"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-2                                #learning rate
epsilon = 1e-2                            #terminate condition

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
#axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss

    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        #ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))

"""apply on the test set"""
Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error

"""test a single sample"""
x = np.array([3000, 3,2, 10000, 1],dtype=np.float32)[...,None]
x = ml_utils.normalise_data(x, u, scale)
x = np.concatenate((x,np.ones((1,1))),axis=0)
print "Price: ", F_Regression(x,W).squeeze()


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Now we will perform gradient descent update procedure according to the following formula:
$$W = W - k\frac{\partial L}{\partial W}$$
Here we use fixed number of iterations and learning rate.
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Our goal is to build a house price prediction model that will approximate the price of a house given its size. To do it, we need to fit a linear line (y = ax + b) to the data above using linear regression. Remember the procedure:
1. Define training set
2. Define hypothesis function. Here $F(x,W) = Wx$
3. Loss function. Here $L(W) = \frac{1}{2N}{\sum_{i=1}^N{(F(x^{(i)},W)-z)^2}}$
4. Update procedure (gradient descent). $W = W - k\frac{\partial L}{\partial W}$
To speed up computation, you should avoid using loop when working with scripting languges e.g. Python, Matlab. Try using array/matrix instead. Here you are provided code for step 1 and 2. Your will be asked to implement step 3 and 4. Some skeleton code will be provided for your convenience.
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Question: Do you see any improvement in accuracy or convergence speed? Why?
Answer: Regularisation does help speed up the training (it adds stricter rules to the update procedure). Accuracy is the same (90%) is probably because (i) number of parameters to be trained is small (2-D) and so is the number of training samples; and (ii) the data are well separated. In a learning task which involves large number of parameters (such as neural network), regularisation proves a very efficient technique.
2.2 Multi-class classification
Here we are working with a very famous dataset. The Iris flower dataset has 150 samples of 3 Iris flower species (Setosa, Versicolour, and Virginica), each sample stores the height and length of its sepal and pedal in cm (4-D in total). Your task is to build a classifier to distinguish these flowers.
Can you write Python code for it?

#read the Iris dataset
iris = np.load('data/iris.npz')
X = iris['X']
Z = iris['Z']
print "size X ", X.shape
print "size Z ", Z.shape

#split train/test with ratio 120:30
TOTALS = Z.size
idx = np.random.permutation(TOTALS)
idx_train = idx[:120]
idx_test = idx[120:]

X_train = X[:, idx_train]
X_test = X[:, idx_test]
Z_train = Z[:, idx_train]
Z_test = Z[:, idx_test]

#normalise data
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

#concatenate array of "1s" to X array
X_train = np.concatenate((X_train, np.ones_like(Z_train)), axis = 0)
X_test = np.concatenate((X_test, np.ones_like(Z_test)), axis = 0)


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Quiz: you may notice the learning rate k is set to $10^{-8}$. Why is it too small? Try to play with several bigger values of k, you will soon find out that the training is extremely sensitive to the learning rate (the training easily  diverges or even causes "overflow" error with large k).

Answer: It is because both the input (size of house) and output (price) have very large range of values, which result in very large gradient.
Task 1.3: Test your learned model. Suppose you want to sell a house of size 3000 $feat^2$, how much do you expect your house will cost?

Answer: you should get around 260k USD for that house.
Can you write Python code for it?

x = 3000
x = np.array([x,1])[...,None]   #make sure feature vector has size 2xN, here N=1
print "Expected price: ", F_Regression(x,W).squeeze()


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Task 1.1: define the loss function for linear regression according to the following formula:
$$L = \frac{1}{2N}{\sum_{i=1}^N{(y^{(i)}-z^{(i)})^2}}$$
Please fill in the skeleton code below. Hints: (i) in Python numpy the square operator $x^2$ is implemented as x**2; (ii) try to use matrix form and avoid for loop
Can you write Python code for it?

"""step 3: loss function"""
def Loss_Regression(Y, Z):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
    output:  loss value, is a scalar
    """
    #enter the code here
    N = float(Z.size)
    diff = Y-Z
    return 1/(2*N)*np.dot(diff, diff.T).squeeze()


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Task 1.2: compute gradient of the loss function w.r.t parameter W according to the following formula:<br>
$$\frac{\partial L}{\partial W} = \frac{1}{N}\sum_{i=1}^N{(y^{(i)}-z^{(i)})x^{(i)}}$$
Please fill in the skeleton code below.
Can you write Python code for it?

"""step 4: gradient descent - compute gradient"""
def dLdW_Regression(X, Y, Z):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   predicted values, has size 1xN
             Z   groundtruth values, has size 1xN
    output:  gradient, has same size as W
    """
    #enter the code here
    N = float(Z.size)
    return 1/N * (Y-Z).dot(X.T)


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Task 1.4: The gradient descent in the code above terminates after 100 iterations. You may want it to terminate when improvement in the loss is below a threshold.
$$\Delta L_t = |L_t - L_{t-1}| < \epsilon$$
Edit the code to terminate the loop when the loss improvement is below $\epsilon=10^{-2}$. Re-evaluate your model to see if its performance has improved.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-8                                #learning rate
epsilon = 1e-2                            #terminate condition

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Regression(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Regression(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Regression(X_train, Y_train, Z_train) #compute gradient
    W = W - k*dLdW                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss

    if (len(loss_collections)+1) % vis_interval==0:
        #print "Iter #", len(loss_collections)
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_and_line(axis_data, X_train, Z_train, W, "iter #" + str(len(loss_collections)))
        fig.canvas.draw()

print "Learned parameters ", W.squeeze()
print "Learning terminates after {} iterations".format(len(loss_collections))

#run the test
Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Task 2.1: your first task is to define the hypothesis function. Do you remember the hypothesis function in a binary classification task? It has form of a sigmoid function:
$$F(x,W) = \frac{1}{1+e^{-Wx}}$$
Can you write Python code for it?

def F_Classification(X, W):
    """
    Compute the hypothesis function given input array X and parameter W
    input:   X  input array, must has size DxN (each column is one sample)
             W  parameter array, must has size 1xD
    output:  sigmoid of W*X, size 1xN
    """
    return 1/(1+np.exp(-np.dot(W,X)))


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
Task 2.2: define the loss function for binary classification. It is called "negative log loss":
$$L(W) = -\frac{1}{N} \sum_{i=1}^N{[z^{(i)} log(F(x^{(i)},W)) + (1-z^{(i)})(log(1-F(x^{(i)},W))]}$$
Next, define the gradient function:
$$\frac{\partial L}{\partial W} = \frac{1}{N}(F(X,W) - Z)X^T$$
Can you write Python code for it?

"""step 3: loss function for classification"""
def Loss_Classification(Y, Z):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
    output:  loss value, is a scalar
    """
    #enter the code here
    N = float(Z.size)

    return -1/N*(np.dot(np.log(Y), Z.T) + np.dot(np.log(1-Y), (1-Z).T)).squeeze()

"""step 4: gradient descent for classification - compute gradient"""
def dLdW_Classification(X, Y, Z):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   probability of label = 1, has size 1xN
             Z   groundtruth values, has size 1xN
    output:  gradient, has same size as W
    """
    #enter the code here
    N = float(Z.size)
    return 1/N * (Y-Z).dot(X.T)

W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 0.2                                #learning rate
epsilon = 1e-6                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
while(1):
    Y_train = F_Classification(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Classification(Y_train, Z_train)         #compute loss
    dLdW = dLdW_Classification(X_train, Y_train, Z_train) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        ml_utils.plot_loss(axis_loss,...


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_answer.ipynb".
The first task is:
We achieve 90% accuracy (only two students have been misclassified). Not too bad, isn't it?
Task 2.3: regularisation
Now we want to add a regularisation term into the loss to prevent overfitting.
Regularisation loss is simply magnitude of the parameter vector W after removing the last element (i.e. bias doesn't count to regularisation).
$$L_R = \frac{1}{2}|W'|^2$$
where W' is W with the last element truncated.<br>
Now the total loss would be:
$$L(W) = -\frac{1}{N} \sum_{i=1}^N{[z^{(i)} log(F(x^{(i)},W)) + (1-z^{(i)})(log(1-F(x^{(i)},W))]} + \frac{1}{2}|W'|^2$$
The gradient become:
$$\frac{\partial L}{\partial W} = \frac{1}{N}(F(X,W) - Z)X^T + W''$$
where W'' is W with the last element change to 0.
Your task is to implement the loss and gradient function with added regularisation.
Can you write Python code for it?

"""step 3: loss function with regularisation"""
def Loss_Classification_Reg(Y, Z, W):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
             W   parameter vector, size 1xD
    output:  loss value, is a scalar
    """
    #enter the code here
    N = float(Z.size)
    W_ = W[:,:-1]
    return -1/N*(np.dot(np.log(Y), Z.T) + np.dot(np.log(1-Y), (1-Z).T)).squeeze() + 0.5*np.dot(W_,W_.T).squeeze()

"""step 4: gradient descent with regularisation - compute gradient"""
def dLdW_Classification_Reg(X, Y, Z, W):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   probability of label = 1, has size 1xN
             Z   groundtruth values, has size 1xN
             W   parameter vector, size 1xD
    output:  gradient, has same size as W
    """
    #enter the code here
    N = float(Z.size)
    W_ = W
    W_[:,-1] = 0
    return 1/N * (Y-Z).dot(X.T) + W_


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
1.3 Gradient descent with momentum
Task 1.6: In the latest experiment, our training takes ~4000 iterations to converge. Now let try gradient descent with momentum to speed up the training. We will employ the following formula:
$$v_t = m*v_{t-1} + k\frac{\partial L}{\partial W}$$
$$W = W - v_t$$
Can you write Python code for it?

"""step 4: gradient descent with momentum - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-2                                #learning rate
epsilon = 1e-2                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#enter your code here
# to implement update loop with momentum


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
2. Classification
In this part you will walk through different steps to implement several basic classification tasks.
2.1. Binary classification
Imagine you were an USP professor who teaches Computer Science. This year there is 100 year-one students who want to register your module. You examine their performance based on their scores on two exams. You have gone through the records of 80 students and already made admission decisions for them. Now you want to build a model to automatically make admission decisions for the rest 20 students. Your training data will be the exam results and admission decisions for the 80 students that you have assessed.

Firstly, let load the data.
Can you write Python code for it?

student_data = pd.read_csv('data/student_data_binary_clas.txt', header = None, names=['exam1', 'exam2', 'decision'])
student_data

#split train/test set
X = np.array([student_data['exam1'], student_data['exam2']], dtype=np.float32)
Z = np.array([student_data['decision']], dtype = np.float32)

#assume the first 80 students have been assessed, use them as the training data
X_train = X[:,:80]
X_test = X[:,80:]

#you later have to manually assess the rest 20 students according to the university policies.
# Great, now you have a chance to evaluate your learned model
Z_train = Z[:,:80]
Z_test = Z[:,80:]

#normalise data
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

#concatenate array of "1s" to X array
X_train = np.concatenate((X_train, np.ones_like(Z_train)), axis = 0)
X_test = np.concatenate((X_test, np.ones_like(Z_test)), axis = 0)

#let visualise the training set
plt.close()
plt.ion()
fig = plt.figure(1)
axis_data = fig.add_subplot(111)
ml_utils.plot_scatter_with_label_2d(axis_data, X_train, Z_train,msg="student score scatter plot")


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Confirm that the error measurement on the test set has improved.
1.2 Multivariate regression
So far we assume the house price is affected by the size only. Now let consider also other fields "Bedrooms", "Baths", "lot" (location) and "NW" (whether or not the houses face Nothern West direction).

Important: now your feature vector is multi-dimensional, it is crucial to normalise your training set for gradient descent to converge properly. The code below is almost identical to the previous step 1, except it loads more fields and implements data normalisation.
Can you write Python code for it?

"""step 1: define training set X, Z."""
selected_fields = ['size', 'Bedrooms', 'Baths', 'lot', 'NW']
X_train, Z_train = ml_utils.extract_data(house_data_train, selected_fields, ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, selected_fields, ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise
X_train, u, scale = ml_utils.normalise_data(X_train)
X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print("size of X_train ", X_train.shape)
print("size of Z_train ", Z_train.shape)(


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
For univariate regression, we are interested in the "size" parameter only. Let's extract necessary data and visualise it.
Can you write Python code for it?

X, Z = ml_utils.extract_data(house_data, ['size'], ['price'])
Z = Z/1000.0  #price has unit x1000 USD

plt.plot(X[0],Z[0], '.')
plt.xlabel('size (feet^2)')
plt.ylabel('price (USD x1000)')
plt.title('house data scatter plot')


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Let preserve some data for test. Here we extract 10% for testing.
Can you write Python code for it?

np.random.seed(0)
idx = np.random.permutation(TOTALS)
idx_train = idx[:90]
idx_test = idx[90:]
house_data_train = {}
house_data_test = {}
for field in house_data.keys():
    house_data_test[field] = house_data[field][idx_test]
    house_data_train[field] = house_data[field][idx_train]


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Now evaluate your learned model using the test set. Measure the total error of your prediction
Can you write Python code for it?

Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Now run step 2-4 again. Note the followings:
1. You need not to modify the Loss_Regression and dLdW_Regression functions. They should generalise enough to work with multi-dimensional data
2. Since your training samples are normalised you can now use much higher learning rate e.g. k = 1e-2
3. Note that the plot function plot_scatter_and_line will not work in multivariate regression since it is designed for 1-D input only. Consider commenting it out.

Question: how many iterations are required to pass the threshold $\Delta L < 10^{-2}$ ?

Answer: you should need ~4000 iterations (and it will take a while to complete).
Task 1.5: (a) evaluate your learned model on the test set. (b) Suppose the house you want to sell has a size of 3000 $feet^2$, has 3 bedrooms, 2 baths, lot number 10000 and in NW direction. How much do you think its price would be? Hints: don't forget to normalise the test sample.

Answer: You will get ~150k USD only, much lower than the previous prediction based on size only. Your house has an advantage of size, but other parameters matter too.
Can you write Python code for it?

#enter your code here
# train multivariate regression model

# evaluate on test set

# test a single unknown sample


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Our goal is to build a house price prediction model that will approximate the price of a house given its size. To do it, we need to fit a linear line (y = ax + b) to the data above using linear regression. Remember the procedure:
1. Define training set
2. Define hypothesis function. Here $F(x,W) = Wx$
3. Loss function. Here $L(W) = \frac{1}{2N}{\sum_{i=1}^N{(F(x^{(i)},W)-z)^2}}$
4. Update procedure (gradient descent). $W = W - k\frac{\partial L}{\partial W}$
To speed up computation, you should avoid using loop when working with scripting languges e.g. Python, Matlab. Try using array/matrix instead. Here you are provided code for step 1 and 2. Your will be asked to implement step 3 and 4. Some skeleton code will be provided for your convenience.
Can you write Python code for it?

"""step 1: define training and test set X, Z."""
X_train, Z_train = ml_utils.extract_data(house_data_train, ['size'], ['price'])
X_test, Z_test = ml_utils.extract_data(house_data_test, ['size'], ['price'])

Z_train = Z_train/1000.0                        #price has unit x1000 USD
Z_test = Z_test/1000.0

##normalise data, uncomment for now
#X_train, u, scale = ml_utils.normalise_data(X_train)
#X_test = ml_utils.normalise_data(X_test, u, scale)

N = Z_train.size                                            #number of training samples
ones_array = np.ones((1,N),dtype=np.float32)
X_train = np.concatenate((X_train, ones_array), axis=0)           #why?
X_test  = np.concatenate((X_test, np.ones((1, Z_test.size), dtype=np.float32)), axis = 0) #same for test data
print("size of X_train ", X_train.shape)
print("size of Z_train ", Z_train.shape)

"""step 2: define hypothesis function"""
def F_Regression(X, W):
    """
    Compute the hypothesis function y=F(x,W) in batch.
    input:  X  input array, must has size DxN (each column is one sample)
            W  parameter array, must has size 1xD
    output: linear multiplication of W*X, size 1xN
    """
    return np.dot(W,X)


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Quiz: you may notice the learning rate k is set to $10^{-8}$. Why is it too small? Try to play with several bigger values of k, you will soon find out that the training is extremely sensitive to the learning rate (the training easily  diverges or even causes "overflow" error with large k). Why is that?

Answer: It is because both the input (size of house) and output (price) have very large range of values, which result in very large gradient even with small k.
Task 1.3: Test your learned model. Suppose you want to sell a house of size 3000 $feat^2$, how much do you expect your house will cost?

Answer: you should get around 260k USD for that house.
Can you write Python code for it?

x = 3000
#enter your code here to predict house price for x


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Rerun the update loop again with the new loss and gradient functions. Note you may need to change the learning rate accordingly to have proper convergence. Now you have implemented both regularisation and momentum techniques, you can use a standard learning rate value of 0.01 which is widely used in practice.
Can you write Python code for it?

""" gradient descent with regularisation - parameter update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 0.01                                #learning rate
epsilon = 1e-6                            #terminate condition
m = 0.9                                 #momentum
v = 0                                   #initial velocity

#visualisation settings
vis_interval = 10
loss_collections = []
prev_loss = 0
plt.close()
plt.ion()
fig = plt.figure(1,figsize=(16, 4))
axis_loss = fig.add_subplot(131)
axis_data = fig.add_subplot(132)
for i in range(500):
    Y_train = F_Classification(X_train,W)               #compute hypothesis function aka. predicted values
    loss = Loss_Classification_Reg(Y_train, Z_train, W)         #compute loss
    dLdW = dLdW_Classification_Reg(X_train, Y_train, Z_train, W) #compute gradient
    v = v*m + k*dLdW
    W = W - v                            #update
    loss_collections.append(loss)
    if abs(loss - prev_loss) < epsilon:
        break
    prev_loss = loss
    if (len(loss_collections)+1) % vis_interval==0:
        ml_utils.plot_loss(axis_loss, range(len(loss_collections)),loss_collections, "loss = " + str(loss))
        ml_utils.plot_scatter_with_label_2d(axis_data, X_train, Z_train, W, "student score scatter plot")
        fig.canvas.draw()

print("Learned parameters ", W.squeeze())
print("Learning terminates after {} iterations".format(len(loss_collections)))


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Task 1.1: define the loss function for linear regression according to the following formula:
$$L = \frac{1}{2N}{\sum_{i=1}^N{(y^{(i)}-z^{(i)})^2}}$$
Please fill in the skeleton code below. Hints: (i) in Python numpy the square operator $x^2$ is implemented as x**2; (ii) try to use matrix form and avoid for loop
Can you write Python code for it?

"""step 3: loss function"""
def Loss_Regression(Y, Z):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
    output:  loss value, is a scalar
    """

    N = float(Z.size)
    diff = Y-Z
    return 1/(2*N)*np.dot(diff, diff.T).squeeze()
    # computes a multiplication: https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html

    # https://docs.scipy.org/doc/numpy/reference/generated/numpy.squeeze.html


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Task 1.4: The gradient descent in the code above terminates after 100 iterations. You may want it to terminate when improvement in the loss is below a threshold.
$$\Delta L_t = |L_t - L_{t-1}| < \epsilon$$
Edit the code to terminate the loop when the loss improvement is below $\epsilon=10^{-2}$. Re-evaluate your model to see if its performance has improved.
Can you write Python code for it?

"""step 4: gradient descent - update loop"""
W = np.random.rand(1,X_train.shape[0]).astype(np.float32)   #W has size 1xD, randomly initialised
k = 1e-8                                #learning rate
epsilon = 1e-2                            #terminate condition

#enter your code here
#you may find it more convenient to copy the code (step 4: update loop with fixed iterations)
#  above and edit directly from it

#run the test
Y_test = F_Regression(X_test, W)
error = Loss_Regression(Y_test, Z_test)
print "Evaluation error: ", error


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Task 2.1: your first task is to define the hypothesis function. Do you remember what the hypothesis function in a binary classification task look like?
$$F(x,W) = \frac{1}{1+e^{-Wx}}$$
Can you write Python code for it?

def F_Classification(X, W):
    """
    Compute the hypothesis function given input array X and parameter W
    input:   X  input array, must has size DxN (each column is one sample)
             W  parameter array, must has size 1xD
    output:  sigmoid of W*X, size 1xN
    """
    #enter your code here

    pass


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
Task 2.4: one-vs-all. Train 3 binary one-vs-all classifiers $F_i$ (i=1-3), one for each class. An unknown feture vector x belongs to class i if:
$$max_i F(x,W_i)$$
Task 2.5: implement one-vs-one and compare the results with one-vs-all.
Can you write Python code for it?


I am working on the file "Intro ML Semcomp/semcomp17_ml/semcomp17_ml_tutorial.ipynb".
The first task is:
We achieve 90% accuracy (only two students have been misclassified). Not too bad, isn't it?
Task 2.3: regularisation
Now we want to add a regularisation term into the loss to prevent overfitting.
Regularisation loss is simply magnitude of the parameter vector W after removing the last element (i.e. bias doesn't count to regularisation).
$$L_R = \frac{1}{2}|W'|^2$$
where W' is W with the last element truncated.<br>
Now the total loss would be:
$$L(W) = -\frac{1}{N} \sum_{i=1}^N{[z^{(i)} log(F(x^{(i)},W)) + (1-z^{(i)})(log(1-F(x^{(i)},W))]} + \frac{1}{2}|W'|^2$$
The gradient become:
$$\frac{\partial L}{\partial W} = \frac{1}{N}(F(X,W) - Z)X^T + W''$$
where W'' is W with the last element change to 0.
Your task is to implement the loss and gradient function with added regularisation.
Can you write Python code for it?

"""step 3: loss function with regularisation"""
def Loss_Classification_Reg(Y, Z, W):
    """
    Compute the loss between the predicted (Y=F(X,W)) and the groundtruth (Z) values.
    input:   Y   predicted results Y = F(X,W) with given parameter W, has size 1xN
             Z   groundtruth vector Z, has size 1xN
             W   parameter vector, size 1xD
    output:  loss value, is a scalar
    """
    #enter your code here

    pass

"""step 4: gradient descent with regularisation - compute gradient"""
def dLdW_Classification_Reg(X, Y, Z, W):
    """
    Compute gradient of the loss w.r.t parameter W.
    input:   X   input array, each column is one sample, has size DxN
             Y   probability of label = 1, has size 1xN
             Z   groundtruth values, has size 1xN
             W   parameter vector, size 1xD
    output:  gradient, has same size as W
    """
    #enter your code here

    pass


I am working on the file "Intro Progrmación/Introducción a Python.ipynb".
The first task is:
Decisiones
Loops (Bucles)
Can you write Python code for it?

balance = 1000000
target  = 2000000
interés = 0.1
años    = 0
while balance <= target:
   años = años + 1
   InterésesGanados = balance*interés
   balance = balance + InterésesGanados

print ('Nesesitamos un periodo de %s años para obtener %s \nAdemás, Ganamos %.2f de intereses.' %(años, target,InterésesGanados ))


I am working on the file "Intro Progrmación/Introducción a Python.ipynb".
The first task is:
Más adelante veremos cmo hacer wed Scraping que nos permitira jararnos datos de web para asi obtener e tipo de cmabio actual y poder hacer una pequeña aplicacion de conversión de monedas.
Can you write Python code for it?


I am working on the file "Intro Progrmación/Introducción a Python.ipynb".
The first task is:
Pasemos a calcular el resultado de una inversión P a una tipo de interés r(no espresada como fracion, esto para practicar los operadores arirtmeticos) en un tiempo n (la composición es en este periodo
Can you write Python code for it?


I am working on the file "Intro Progrmación/Introducción a Python.ipynb".
The first task is:
Pyhton como calculadora
¿ Si tengo 2 MM de pesos por cuantos dólares los puedo cangear?
Recuerde:  [input('¿Cúal es el precio del dólar actual?')]. Ahora, lo que nesesitamos es entrar un número que sea e tipo de cambio. Comvertiremos el string en tipo númerico para despues hacer la conversion a dolares.
Can you write Python code for it?

TipoCambio = float(input('¿Cúal es el tipo de cambio del dólar-peso actual? '))

Pesos = float(input('¿Cuantos pesos va a cambiar?'))

Dolares = Pesos/TipoCambio
Dolares


I am working on the file "Intro Python.ipynb".
The first task is:
4. Tipos de dados:
Can you write Python code for it?

print('Integers (int): -2, -1, 0, 1, 2, 3, 4, 5')
print('Floats (floats): -1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25')
print('Strings (str): ', 'Hello World!', 'Spam spam spam', 'spam and eggs')
print('Respectivas funções: int(), float() e str()!')

# Concatenação de strings:
'Jayme ' + "Anchante"

'Jayme' + 42

'Jayme' + str(42)

'Jayme' * 5

'Jayme' * 'Anchante'

'Jayme' * 5.0


I am working on the file "Intro Python.ipynb".
The first task is:
5. Atribuindo valores a objetos:
Can you write Python code for it?

spam = 7 # atribuindo o valor '7' ao objeto 'spam'

spam # 'chamando' o objeto 'spam'

eggs = 3 # atribuindo o valor '3' ao objeto 'eggs'

spam + eggs # somando 'spam' e 'eggs'

spam = spam + 7 # sobreescrevendo um novo valor ao objeto 'spam'
spam # 'chamando' o objeto 'spam'


I am working on the file "Intro Python.ipynb".
The first task is:
Nome das variáveis tem que obedecer o seguinte:
Ser uma palavra.

Utilizar apenas letras, números e o caractere sublinhado (_).

Não pode começar com um número.

6. Expressões booleanas:
Can you write Python code for it?


I am working on the file "Intro To Data Science Lesson 2.ipynb".
The first task is:
By using SQL quieries, it makes it simple to retrieve the data that you would like to analyze and then wrangle that data specifically if necessary.
Data Retrieval from JSON formats
Another common format is the JSON format.  Data is commonly in this format when you are working with Relational Databases.  The example below shows how one can retrieve data from a database using that website's REST API.  Here we show to load JSON data into a Python dictionary.  Here we use the requests library and then load the data using the json library. Pprint is used to print the JSON in a more readable format.
Can you write Python code for it?

import json
import requests
import pprint
url = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key=4beab33cc6d65b05800d51f5e83bde1b&artist=Cher&album=Believe&format=json'
data = requests.get(url).text
df3 = json.loads(data)
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(df)


I am working on the file "Intro To Data Science Lesson 2.ipynb".
The first task is:
From the output, you see that this dataset gives information about baseball players.  Here is the last 10 entries of our Data Frame.
Can you write Python code for it?


I am working on the file "Intro To Data Science Lesson 2.ipynb".
The first task is:
Once the data is in the dictonary, we can access the information as such:
Can you write Python code for it?


I am working on the file "Intro To Data Science Lesson 2.ipynb".
The first task is:
One of the first things that you can do is to analyze the DataFrame to see if there is anything that doesn't look correct, such as variation in the number of entries in each column, min, max value etc.  This can be easily assess through the describe()
Can you write Python code for it?


I am working on the file "Intro To Data Science Lesson 2.ipynb".
The first task is:
The query below retrieves data on players that are less than 200 lbs and organizes the entries by birthCountry
Can you write Python code for it?

q2 = 'SELECT playerID,birthCountry,bats,weight FROM df WHERE weight < 200 GROUP BY birthCountry LIMIT 10'
sql_sol2 = pandasql.sqldf(q2.lower(),globals())
sql_sol2


I am working on the file "Intro to Data Science - Seattle Fremont Bridge.ipynb".
The first task is:
Let's do a simple clustering to identify these two groups
Can you write Python code for it?

from sklearn.mixture import GaussianMixture

gmm = GaussianMixture(2)
gmm.fit(X_PCA)

labels = gmm.predict(X_PCA)
labels

plt.scatter(X_PCA[:, 0], X_PCA[:, 1], c=labels, cmap='rainbow')


I am working on the file "Intro to Data Science - Seattle Fremont Bridge.ipynb".
The first task is:
Let's get the data from Seattle open data site
Data is published on a hourly basis from 2012 to now and can be found in the below url. Make a pandas dataframe from this for our analysis.
Can you write Python code for it?

url = 'https://data.seattle.gov/api/views/65db-xm6k/rows.csv?accessType=DOWNLOAD'
df = pd.read_csv(url, parse_dates=True)

df.head()

df.shape

df.index = pd.DatetimeIndex(df.Date)

df.head()

df.drop(columns=['Date'], inplace=True)
df.head()

df['total'] = df['Fremont Bridge East Sidewalk'] + df['Fremont Bridge West Sidewalk']
df.drop(columns=['Fremont Bridge East Sidewalk', 'Fremont Bridge West Sidewalk'], inplace=True)


I am working on the file "Intro to Data Science - Seattle Fremont Bridge.ipynb".
The first task is:
Let's start with visualization to understand this data
Can you write Python code for it?

df.resample('D').sum().plot()

df.resample('M').sum().plot()

df.groupby(df.index.hour).mean().plot()


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Here we create a Python Dictionary and then convert that into a Panda DataFrame.  Here we create a dictionary named family and create dataframe df from family.
Can you write Python code for it?

family = {'name': ['Ransford','Denzel'],
         'Birth year': [1984, 2004],
         'favorite subject': ['Math','Science']}
family

df = pd.DataFrame(family)
df


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Here we perform the tail on the DataFrame object.
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
In this line, we print the 2nd column
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
In this line, we print the 2nd row of the matrix
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Intro to Numpy Arrays
Here create a 1-dimension array with floating numbers.  Note we did not have to type print before array if it is the last line of code
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Intro to Panda Series and DataFrames
The Panda Series allows you to create a Panda Column in which you can put elements of any type within the same column.  Here I create Panda Series with information about my father.  I have modified the index so that it will print out the standard number indices
Can you write Python code for it?

series = pd.Series(['Ransford', "Hyman Sr.", 'January', 1941], index=['First Name', 'Last Name', 'Birth Month', 'Birth Year'])
series


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Notice here that we can index Dataframes by the index name just like in Python dictionaries.  Panda's dataframes have a function called describe which generates some interesting statiscal information.  This function is very helpful for doing some initial sanity checking on the dataframe's columns.  Here we are given the number of entries (given by the count row), the mean, standard deviation and the Interquartile Range (IQR).
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Notice that when we print out the dictionary it prints out in regular text, But when we print out the dataframe, it gives us a nice table in IPython. Pretty cool!!!
Now we are going to create a seperate dataframe and play around with some of it's member functions.
Can you write Python code for it?

frank_grades = {'subject':['Math','English','Social Studies','Science','Music','Art'],
               'grades': [95,87,80,96,98,70]}
df2 = pd.DataFrame(frank_grades)
df2


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
Now let us show how to perform certain slicing and indexing operations on numpy Arrays.If we want to print from the 3rd element until the end on a vector array
Can you write Python code for it?


I am working on the file "Intro to Data Science Lesson1.ipynb".
The first task is:
This operation I found to be kind of weird since I would have assumed the multiplication of two arrays to be the dot product, but that is not what occurs here.  In this operation, The first element of array is multiplied by each element of the first column in two_by_two_array  and the 2nd element of the array is multiplied by each element of the 2nd column of two_by_two_array.
Can you write Python code for it?


I am working on the file "Intro to IPython notebook.ipynb".
The first task is:
Next, we import all of the functions that come with the numpy package. This gives python a lot of array-based features that are optimized for computational speed.
Can you write Python code for it?


I am working on the file "Intro to IPython notebook.ipynb".
The first task is:
Starting from the example below, generate a few plots for practice:
 - a sine wave
 - the fourier transform of a sine wave
 - a sine wave with additional noise (add random values that are 10% of the sine-wave amplitude)
 - the fourier transform of the noisy sine wave
Hints: for these, you'll want to have a larger number of points in the array and we don't necessarily need to view the array each time, that was just for learning purposes. Try setting num=1024 in the linspace function.
The following functions will be useful (these are both part of numpy):
 - random.random()
 - fft.fft()
The dot indicates that the function is part of a module within numpy; the fft function is part of the fft module so you have to specify that when you use it. You can learn more about them with the help feature:
random.random?
Can you write Python code for it?

y = sin(x)

plt.plot(x,y)

plt.plot(x,y,".")  #another example, with dots this time...

random.random()

fft.fft(y)
newy=fft.fft(y)

plt.plot(abs(newy[0:100]))

newarry = y + .1*random.random(1024)

newarry

plt.plot(x,newarry)

fft.fft(newarry)

plt.plot(abs(fft.fft(newarry))[0:40])


I am working on the file "Intro to IPython notebook.ipynb".
The first task is:
The first is the command linspace
linspace creates an array filled with values set by the function parameters:
Can you write Python code for it?


I am working on the file "Intro to IPython notebook.ipynb".
The first task is:
You should see a line that says:
Signature: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
This describes the function and what values you can specify. We used only the start and stop but we could also use the num parameter to tell it how many points we want in the array.
You can close the help by typing esc or by clicking on the small x in the upper right corner.
Can you write Python code for it?


I am working on the file "Intro to Jupyter Notebooks.ipynb".
The first task is:
Or even something interactive, such as
Can you write Python code for it?

# Define an interactive plotting function
def updatePlot(s=0):
    print("data {0:.2f}, smallerdata {1:.2f}".format(data[s], smallerdata[s]))
    fig, ax = drawPlot()
    ax.axvline(s, color="grey", linestyle='dotted')
    ax.annotate(s=round(data[s], 2),
                 xy=(s, data[s]),
                 xytext=(s + 2, 0.5),
                 arrowprops={'arrowstyle': '->'});
    ax.annotate(s=round(smallerdata[s], 2),
                 xy=(s, smallerdata[s]),
                 xytext=(s + 2, 0.3),
                 arrowprops={'arrowstyle': '->'});
    plt.show();

# Define an interactive slider for exploring the data
slider = ipywidgets.interactive(updatePlot, s=(0, len(data) - 1, 1));
IPython.display.display(slider)


I am working on the file "Intro to Jupyter Notebooks.ipynb".
The first task is:
Or how many unique words are there.
Can you write Python code for it?

len(set("in all honesty, counting all the words in a sentence is best done in a computers mind. It doesn't mind counting at all".split()))


I am working on the file "Intro to Jupyter Notebooks.ipynb".
The first task is:
Or something more complicated, such as defining how a graph is produced.
Can you write Python code for it?

# Setup by importing some libraries and configuring a few things
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets
import IPython
%matplotlib inline

# Create two random datasets
data = np.random.random(15)
smallerdata = np.random.random(15) * 0.3

# Define a plotting function
def drawPlot():
    fig, ax = plt.subplots()
    ax.plot(range(len(data)), data, label="random data");
    ax.plot(range(len(smallerdata)), smallerdata, 'r--', label="smaller random data");
    plt.title("Two random dataset compared");
    ax.grid(axis='y');
    ax.legend(loc='upper right');
    return fig, ax

# Draw the plot
fig, ax = drawPlot()
plt.show()


I am working on the file "Intro to Jupyter Notebooks.ipynb".
The first task is:
We can make pretty graphs
Can you write Python code for it?

import matplotlib.pyplot as plt
import math
import numpy as np
%matplotlib inline

t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t**2, 'bs')


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
However, they point to the same place in memory...
Can you write Python code for it?


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
Numpy -> Tensor
Can you write Python code for it?

y = np.array([5,4,3])
type(y)

x = t.from_numpy(y)
type(x)

y

x

y += 10
y

x


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
Speed and Efficiency
Sometimes, PyTorch is faster than Numpy. Sometimes, Numpy is faster than PyTorch
Can you write Python code for it?

import time
def timer(f, trials=5):
    avg_time = 0
    for i in range(trials):
        start = time.time()
        f()
        stop = time.time()
        avg_time += (stop - start)/trials
    return avg_time

np_arr = np.random.rand(10000,10000)
t_arr = t.rand((10000,10000))

print('type(t_arr) = ', type(t_arr))
print('t_arr.shape = ', t_arr.shape)

print('type(np_arr) = ', type(np_arr))
print('np_arr.shape = ', np_arr.shape)

timer(lambda: t_arr*t_arr)

timer(lambda: np_arr*np_arr)


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
Tensors have 100+ built-in class methods
including all your favorite numpy convenience methods and a a growing linear algebra library
What about the GPU?
There is a class method call 'to()', that sends a tensor to a specific device. This is new in 0.4.0.
Can you write Python code for it?

t.cuda.list_gpu_processes()

t_arr.shape

t_arr.device # Should currently be on the cpu

device = t.device('cuda:0')
t_arr_gpu = t_arr.to(device)

t_arr.device

t_arr_gpu.device

timer(lambda: t_arr*t_arr)

timer(lambda: t_arr_gpu*t_arr_gpu)

timer(lambda: np_arr*np_arr)

0.47127203941345214/0.00011396408081054688


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
What about optimizing with some class methods?
Can you write Python code for it?

def exp1():
    y = t_arr * t_arr

y = t.empty(t_arr.shape)
def exp2():
    t.mul(t_arr, t_arr, out=y)

def exp3():
    t_arr.mul_(t_arr)

timer(exp1)

timer(exp2)

timer(exp3)

timer(lambda: np.matmul(np_arr,np_arr))

timer(lambda: t.matmul(t_arr,t_arr))

timer(lambda: t_arr@t_arr)


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
a deep learning research platform that provides maximum flexibility and speed
Central to all neural networks in PyTorch is the autograd package. Let’s first briefly visit this, and we will then go to training our first neural network.
The autograd package provides automatic differentiation for all built-in operations on Tensors. It is a define-by-run framework, which means that your backprop is defined by how your code is run, and that every single iteration can be different.
Can you write Python code for it?

b = t.tensor([2], dtype=t.float32)

b.requires_grad

y=b*b*b
y.requires_grad

y.backward()

b.requires_grad = True # or b.requires_grad_()

y=b*b*b
y.requires_grad

z = y*y

z.backward()

y.grad_fn

b.grad

z.grad

c = b.detach()

y=c*c

y.backward()

import matplotlib
import matplotlib.pyplot as plt

pts = t.tensor([(1,1),(1,2),(2,2)]).float()
plt.scatter(pts[:,0],pts[:,1]); plt.xlim(0,3); plt.ylim(0,3); plt.show()

params = t.tensor((1,0), requires_grad=True, dtype=t.float32)

diff = params[0]*pts[:,0]+params[1]-pts[:,1]
sq = (diff*diff).float()
loss = sq.mean()
loss.backward()
print(params.requires_grad)
params = params - 0.001*params.grad


I am working on the file "Intro to PyTorch.ipynb".
The first task is:
| Data type                | dtype                         |CPU tensor          |GPU tensor               |
|:------------------------:|:-----------------------------:|:------------------:|:-----------------------:|
| 32-bit floating point    | torch.float32 or torch.float  | torch.FloatTensor  | torch.cuda.FloatTensor  |
| 64-bit floating point    | torch.float64 or torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor |
| 16-bit floating point    | torch.float16 or torch.half   | torch.HalfTensor   | torch.cuda.HalfTensor   |
| 8-bit integer (unsigned) | torch.uint8                   | torch.ByteTensor   | torch.cuda.ByteTensor   |
| 8-bit integer (signed)   | torch.int8                    | torch.CharTensor   | torch.cuda.CharTensor   |
| 16-bit integer (signed)  | torch.int16 or torch.short    | torch.ShortTensor  | torch.cuda.ShortTensor  |
| 32-bit integer (signed)  | torch.int32 or torch.int      | torch.IntTensor    | torch.cuda.IntTensor    |
| 64-bit integer (signed)  | torch.int64 or torch.long     | torch.LongTensor   | torch.cuda.LongTensor   |
Converting between Tensors and Numpy Arrays...
Tensor -> Numpy
Can you write Python code for it?

import numpy as np

x = t.Tensor([1,2,3.])
x.type()

y = x.numpy()
type(y)


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Back to Exercise 2
Answer to Exercise 3
Can you write Python code for it?

holland = np.loadtxt('holland_temperature.dat')
newyork= np.loadtxt('newyork_temperature.dat')
beijing = np.loadtxt('beijing_temperature.dat')
plt.plot(np.linspace(1, 12, 12), holland)
plt.plot(np.linspace(1, 12, 12), newyork)
plt.plot(np.linspace(1, 12, 12), beijing)
plt.xlabel('Number of the month')
plt.ylabel('Mean monthly temperature (Celcius)')
plt.xlim(1, 12)
plt.legend(['Holland','New York','Beijing'], loc='best');


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Back to Exercise 3
Answer to Exercise 4
Can you write Python code for it?

air = np.loadtxt('holland_temperature.dat')
sea = np.loadtxt('holland_seawater.dat')
plt.subplot(211)
plt.plot(air, 'b', label='air temp')
plt.plot(sea, 'r', label='sea temp')
plt.legend(loc='best')
plt.ylabel('temp (Celcius)')
plt.xlim(0, 11)
plt.xticks([])
plt.subplot(212)
plt.plot(air-sea, 'ko')
plt.xticks(np.linspace(0, 11, 12),
           ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'])
plt.xlim(0, 11)
plt.ylabel('air - sea temp (Celcius)');


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Back to Exercise 4
Answer to Exercise 5
Can you write Python code for it?

gold = [46, 38, 29, 24, 13, 11, 11, 8, 8, 7, 107]
countries = ['USA', 'CHN', 'GBR', 'RUS', 'KOR', 'GER', 'FRA', 'ITA', 'HUN', 'AUS', 'OTHER']
plt.pie(gold, labels = countries, colors = ['Gold', 'MediumBlue', 'SpringGreen', 'BlueViolet'])
plt.axis('equal');


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
A variable can be raised to a power by using **
(a hat ^, as used in some other languages, doesn't work).
Can you write Python code for it?


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
In the above cell, x is an array of 5 floats (-4. is a float, -4 is an integer).
If you type np.linspace and then an opening parenthesis like:
np.linspace(
and then hit [shift-tab] a little help box pops up to explain the input arguments of the function. When you click on the + sign, you can scroll through all the documentation of the linspace function. Click on the x sign to remove the help box. Let's plot $y$ using 100 $x$ values from
$-4$ to $4$.
Can you write Python code for it?

a = 1
b = 1
c = -6
x = np.linspace(-4, 4, 100)
y = a*x**2 + b*x + c  # Compute y for all x values
plt.plot(x, y)


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Keyword arguments should come after regular arguments. plot(linewidth = 6, [1, 2, 3], [2, 4, 3]) gives an error.
Names may be added along the axes with the xlabel and ylabel functions, e.g., plt.xlabel('this is the x-axis'). Note that both functions take a string as argument. A title can be added to the figure with the plt.title command. Multiple curves can be added to the same figure by giving multiple plotting commands. They are automatically added to the same figure.
 Exercise 2
Plot $y=(x+2)(x-1)(x-2)$ for $x$ going from $-3$ to $3$ using a dashed red line. On the same figure, plot a blue circle for every point where $y$ equals zero. Set the size of the markers to 10 (you may need to read plt.plot? to find out how to do that). Label the axes as 'x-axis' and 'y-axis'. Add the title 'First nice Python figure of Your Name', where you enter your own name.
Answer to Exercise 2
Loading data files
Numerical data can be loaded from a data file using the loadtxt function of numpy; i.e., the command is np.loadtxt. You need to make sure the file is in the same directory as your notebook, or provide the full path. The filename (or path plus filename) needs to be between quotes.
 Exercise 3 Loading data and adding a legend
You are provided with the data files containing the mean montly temperature of Holland, New York City, and Beijing. The Dutch data is stored in holland_temperature.dat, and the other filenames are similar. Plot the temperature for each location against the number of the month (starting with 1 for January) all in a single graph. Add a legend by using the function plt.legend(['line1','line2']), etc., but then with more descriptive names. Find out about the legend command using plt.legend?. Place the legend in an appropriate spot (the upper left-hand corner may be nice, or let Python figure out the best place).
Answer to Exercise 3
 Exercise 4, Subplots and fancy tick markers
Load the average...
Can you write Python code for it?

a = 1
b = 1
c = -6
x = -2
y = a * x**2 + b * x + c
print 'y evaluated at x=-2 is', y
x = 0
y = a * x**2 + b * x + c
print 'y evaluated at x=0 is', y
x = 2
y = a * x**2 + b * x + c
print 'y evaluated at x=2 is', y


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Let's try to plot $y$ vs $x$ for $x$ going from $-4$ to $4$ for the polynomial in the exercise above. To do that, we need to evaluate $y$ at a bunch of points.
A sequence of values of the same type is called an array (for example an array of integers or floats).
Array functionality is available in the package numpy. Let's import numpy and call it np, so that any function in the numpy package may be called as np.function.
Can you write Python code for it?


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
Packages only have to be imported once in an IPython session. After the import, any plotting function may be called from any code cell as plt.function. For example
Can you write Python code for it?


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
The goal of the extra space to make the code more (visually) readable.
2 * 3 works just as well as 2*3.
When you are programming, you want to store your values in variables
Can you write Python code for it?


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
The user decides the order in which code blocks are executed. For example, In [6] means that it is the sixth execution of a code block. If you change the same code block and run it again, it will get number 7. If you define the variable a in code block 7, it will overwrite the value of a defined in a previous code block.
Variable names may be as long as you like (you gotta do the typing though). Selecting descriptive names aids in understanding the code. Variable names cannot have spaces, nor can they start with a number. And variable names are case sensitive. So the variable a is not the same as the variable A. The name of a variable may be anything you want, except for reserved words in the Python language. For example, it is not possible to create a variable print = 7, as print is a reserved word. You will learn many of the reserved words when we continue; they are colored green when you type them in the Notebook.
Basic plotting and a first array
Plotting is not part of standard Python. Luckily, a package exist to create beautiful graphics. A package is a library of functions for a specific set of tasks. There are many Python packages and we will use several of them.
The graphics package is called matplotlib. To be able to use the plotting functions in matplotlib we have to import it.

We will learn several different ways of importing packages. For now, we import the plotting part of matplotlib and call it plt.
We also give a second command which tells Jupyter to show any graphs inside this Notebook and not in a separate window.
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline


I am working on the file "Intro to Python & Jupyter tips.ipynb".
The first task is:
To create an array x consisting of, for example, 5 equally spaced points between -4 and 4 use the linspace command
Can you write Python code for it?


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
Easy as that! tf.nn.softmax() implements the softmax function for you. It takes in logits and returns softmax activations.
Quiz
Can you write Python code for it?

import tensorflow as tf

def run_2():
    output = None
    logit_data = [2.0, 1.0, 0.1]
    logits = tf.placeholder(tf.float32)

    # TODO: Calculate the softmax of the logits
    softmax = tf.nn.softmax(logit_data)

    with tf.Session() as sess:
        # TODO: Feed in the logit data
        output = sess.run(softmax, feed_dict={logits: logit_data})

    return output

print(run_2())


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
Note: If the data passed to the feed_dict doesn’t match the tensor type and can’t be cast into the tensor type, you’ll get the error “ValueError: invalid literal for...”.
Quiz
Can you write Python code for it?

import tensorflow as tf

def run():
    output = None
    x = tf.placeholder(tf.int32)

    with tf.Session() as sess:
        # TODO: Feed the x tensor 123
        output = sess.run(x, feed_dict={x: 123})

    return output

run()


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
One-Hot Encoding
Transforming your labels into one-hot encoded vectors is pretty simple with scikit-learn using LabelBinarizer. Check it out below!
Can you write Python code for it?

import numpy as np
from sklearn import preprocessing

# Example labels
labels = np.array([1,5,3,2,1,4,2,1,3])

# Create the encoder
lb = preprocessing.LabelBinarizer()

# Here the encoder finds the classes and assigns one-hot vectors
lb.fit(labels)

# And finally, transform the labels into one-hot encoded vectors
lb.transform(labels)


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
Quiz
Let's apply what you learned to convert an algorithm to TensorFlow. The code below is a simple algorithm using division and subtraction. Convert the following algorithm in regular Python to TensorFlow and print the results of the session. You can use tf.constant() for the values 10, 2, and 1.
Can you write Python code for it?

import tensorflow as tf

# TODO: Convert the following to TensorFlow:
x = tf.constant(10)
y = tf.constant(2)
z = tf.subtract(tf.divide(x, y), 1)

# TODO: Print z from a session
with tf.Session() as sess:
    output = sess.run(z)
    print(output)


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
Tensor
In TensorFlow, data isn’t stored as integers, floats, or strings. These values are encapsulated in an object called a tensor. In the case of hello_constant = tf.constant('Hello World!'), hello_constant is a 0-dimensional string tensor, but tensors come in a variety of sizes as shown below:
Can you write Python code for it?

# A is a 0-dimensional int32 tensor
A = tf.constant(1234)

# B is a 1-dimensional int32 tensor
B = tf.constant([123,456,789])

# C is a 2-dimensional int32 tensor
C = tf.constant([ [123,456,789], [222,333,444] ])


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
That's because the constant 1 is an integer but the constant 2.0 is a floating point value and subtract expects them to match.
In cases like these, you can either make sure your data is all of the same type, or you can cast a value to another type. In this case, converting the 2.0 to an integer before subtracting, like so, will give the correct result:
Can you write Python code for it?

tf.subtract(tf.cast(tf.constant(2.0), tf.int32), tf.constant(1))   # 1


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
The tf.zeros() function returns a tensor with all zeros.
TensorFlow Softmax
In the Intro to TFLearn lesson we used the softmax function to calculate class probabilities as output from the network. The softmax function squashes it's inputs, typically called logits or logit scores, to be between 0 and 1 and also normalizes the outputs such that they all sum to 1. This means the output of the softmax function is equivalent to a categorical probability distribution. It's the perfect function to use as the output activation for a network predicting multiple classes.

We're using TensorFlow to build neural networks and, appropriately, there's a function for calculating softmax.
Can you write Python code for it?


I am working on the file "Intro to TensorFlow/intro-to-tensorflow-notes.ipynb".
The first task is:
This function does exactly what you would expect it to do. tf.log() takes the natural log of a number.
Quiz
Print the cross entropy using softmax_data and one_hot_encod_label.
Can you write Python code for it?

import tensorflow as tf

softmax_data = [0.7, 0.2, 0.1]
one_hot_data = [1.0, 0.0, 0.0]

softmax = tf.placeholder(tf.float32)
one_hot = tf.placeholder(tf.float32)

# TODO: Print cross entropy from session
cross_entropy = -tf.reduce_sum(tf.multiply(one_hot_data, tf.log(softmax_data)))

with tf.Session() as session:
    output = session.run(cross_entropy, feed_dict={one_hot: one_hot_data, softmax: softmax_data})
    print(output)


I am working on the file "Intro to TensorFlow/tf_neural_network_lab.ipynb".
The first task is:
<img src="image/Learn Rate Tune - Image.png" style="height: 70%;width: 70%">
Problem 3
Below are 2 parameter configurations for training the neural network. In each configuration, one of the parameters has multiple options. For each configuration, choose the option that gives the best acccuracy.
Parameter configurations:
Configuration 1
* Epochs: 1
* Learning Rate:
  * 0.8
  * 0.5
  * 0.1
  * 0.05
  * 0.01
Configuration 2
* Epochs:
  * 1
  * 2
  * 3
  * 4
  * 5
* Learning Rate: 0.2
The code will print out a Loss and Accuracy graph, so you can see how well the neural network performed.
If you're having trouble solving problem 3, you can view the solution here.
Can you write Python code for it?

# Change if you have memory restrictions
batch_size = 128

# TODO: Find the best parameters for each configuration
epochs = 5
learning_rate = 0.2

### DON'T MODIFY ANYTHING BELOW ###
# Gradient Descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

# The accuracy measured against the validation set
validation_accuracy = 0.0

# Measurements use for graphing loss and accuracy
log_batch_step = 50
batches = []
loss_batch = []
train_acc_batch = []
valid_acc_batch = []

with tf.Session() as session:
    session.run(init)
    batch_count = int(math.ceil(len(train_features)/batch_size))

    for epoch_i in range(epochs):

        # Progress bar
        batches_pbar = tqdm(range(batch_count), desc='Epoch {:>2}/{}'.format(epoch_i+1, epochs), unit='batches')

        # The training cycle
        for batch_i in batches_pbar:
            # Get a batch of training features and labels
            batch_start = batch_i*batch_size
            batch_features = train_features[batch_start:batch_start + batch_size]
            batch_labels = train_labels[batch_start:batch_start + batch_size]

            # Run optimizer and get loss
            _, l = session.run(
                [optimizer, loss],
                feed_dict={features: batch_features, labels: batch_labels})

            # Log every 50 batches
            if not batch_i % log_batch_step:
                # Calculate Training and Validation accuracy
                training_accuracy = session.run(accuracy, feed_dict=train_feed_dict)
                validation_accuracy = session.run(accuracy, feed_dict=valid_feed_dict)

                # Log batches
                previous_batch = batches[-1] if batches else 0
                batches.append(log_batch_step + previous_batch)
                loss_batch.append(l)
                train_acc_batch.append(training_accuracy)
                valid_acc_batch.append(validation_accuracy)

        # Check accuracy against Validation data
        validation_accuracy =...


I am working on the file "Intro to TensorFlow/tf_neural_network_lab.ipynb".
The first task is:
Problem 2
Now it's time to build a simple neural network using TensorFlow. Here, your network will be just an input layer and an output layer.

For the input here the images have been flattened into a vector of $28 \times 28 = 784$ features. Then, we're trying to predict the image digit so there are 10 output units, one for each label. Of course, feel free to add hidden layers if you want, but this notebook is built to guide you through a single layer network.
For the neural network to train on your data, you need the following float32 tensors:
 - features
  - Placeholder tensor for feature data (train_features/valid_features/test_features)
 - labels
  - Placeholder tensor for label data (train_labels/valid_labels/test_labels)
 - weights
  - Variable Tensor with random numbers from a truncated normal distribution.
    - See tf.truncated_normal() documentation for help.
 - biases
  - Variable Tensor with all zeros.
    - See  tf.zeros() documentation for help.
If you're having trouble solving problem 2, review "TensorFlow Linear Function" section of the class.  If that doesn't help, the solution for this problem is available here.
Can you write Python code for it?

# All the pixels in the image (28 * 28 = 784)
features_count = 784
# All the labels
labels_count = 10

# TODO: Set the features and labels tensors
features = tf.placeholder(tf.float32)
labels = tf.placeholder(tf.float32)

# TODO: Set the weights and biases tensors
weights = tf.Variable(tf.truncated_normal((features_count, labels_count)))
biases = tf.Variable(tf.zeros(labels_count))

### DON'T MODIFY ANYTHING BELOW ###

#Test Cases
from tensorflow.python.ops.variables import Variable

assert features._op.name.startswith('Placeholder'), 'features must be a placeholder'
assert labels._op.name.startswith('Placeholder'), 'labels must be a placeholder'
assert isinstance(weights, Variable), 'weights must be a TensorFlow variable'
assert isinstance(biases, Variable), 'biases must be a TensorFlow variable'

assert features._shape == None or (\
    features._shape.dims[0].value is None and\
    features._shape.dims[1].value in [None, 784]), 'The shape of features is incorrect'
assert labels._shape  == None or (\
    labels._shape.dims[0].value is None and\
    labels._shape.dims[1].value in [None, 10]), 'The shape of labels is incorrect'
assert weights._variable._shape == (784, 10), 'The shape of weights is incorrect'
assert biases._variable._shape == (10), 'The shape of biases is incorrect'

assert features._dtype == tf.float32, 'features must be type float32'
assert labels._dtype == tf.float32, 'labels must be type float32'

# Feed dicts for training, validation, and test session
train_feed_dict = {features: train_features, labels: train_labels}
valid_feed_dict = {features: valid_features, labels: valid_labels}
test_feed_dict = {features: test_features, labels: test_labels}

# Linear Function WX + b
logits = tf.matmul(features, weights) + biases

prediction = tf.nn.softmax(logits)

# Cross entropy
cross_entropy = -tf.reduce_sum(labels * tf.log(prediction), reduction_indices=1)

# Training loss
loss = tf.reduce_mean(cross_entropy)

# Create an operation that initializes all variables
init =...


I am working on the file "Intro to TensorFlow/tf_neural_network_lab.ipynb".
The first task is:
The notMNIST dataset is too large for many computers to handle.  It contains 500,000 images for just training.  You'll be using a subset of this data, 15,000 images for each label (A-J).
Can you write Python code for it?

def download(url, file):
    """
    Download file from <url>
    :param url: URL to file
    :param file: Local file path
    """
    if not os.path.isfile(file):
        print('Downloading ' + file + '...')
        urlretrieve(url, file)
        print('Download Finished')

# Download the training and test dataset.
download('https://s3.amazonaws.com/udacity-sdc/notMNIST_train.zip', 'notMNIST_train.zip')
download('https://s3.amazonaws.com/udacity-sdc/notMNIST_test.zip', 'notMNIST_test.zip')

# Make sure the files aren't corrupted
assert hashlib.md5(open('notMNIST_train.zip', 'rb').read()).hexdigest() == 'c8673b3f28f489e9cdf3a3d74e2ac8fa',\
        'notMNIST_train.zip file is corrupted.  Remove the file and try again.'
assert hashlib.md5(open('notMNIST_test.zip', 'rb').read()).hexdigest() == '5d3c7e653e63471c88df796156a9dfa9',\
        'notMNIST_test.zip file is corrupted.  Remove the file and try again.'

# Wait until you see that all files have been downloaded.
print('All files downloaded.')

def uncompress_features_labels(file):
    """
    Uncompress features and labels from a zip file
    :param file: The zip file to extract the data from
    """
    features = []
    labels = []

    with ZipFile(file) as zipf:
        # Progress Bar
        filenames_pbar = tqdm(zipf.namelist(), unit='files')

        # Get features and labels from all files
        for filename in filenames_pbar:
            # Check if the file is a directory
            if not filename.endswith('/'):
                with zipf.open(filename) as image_file:
                    image = Image.open(image_file)
                    image.load()
                    # Load image data as 1 dimensional array
                    # We're using float32 to save on memory space
                    feature = np.array(image, dtype=np.float32).flatten()

                # Get the the letter from the filename.  This is the letter of the image.
                label = os.path.split(filename)[1][0]

                features.append(feature)
        ...


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
As with single indexing, the slice notation can be used to set elements of a
list.
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Booleans
Python has a data type to represent True and False values (Boolean variables)
and supports standard Boolean operators like "and", "or", and "not"
Can you write Python code for it?

x = True
y = False

x

not x

y

not y # if True return False, if False return True

x and y # if both arguments are True return true, else return False

x and (not y)

x or y  # if either argument is True, return True, else return False


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Combining indexing and comparison on arrays
NumPy arrays allows us to combine the comparison operators with indexing.  This facilitates data filtering and subsetting.
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Commonly used functions from matplotlib.pyplot include plot, scatter, imshow, savefig among others.  We explored a decent numbers of plotting functionality at the beginning of this notebook. Here are a few more examples.
Can you write Python code for it?

x = arange(1, 10, 0.25)
y = x + random.normal(size=len(x))
scatter(x,y,color='black')
pass

# see http://matplotlib.org/users/image_tutorial.html

import matplotlib.image as mpimg # required for loading images
img = mpimg.imread("http://matplotlib.org/_images/stinkbug.png")
imshow(img)

pass

# demonstrating subplots

fig, (ax1, ax2, ax3) = subplots(nrows=1, ncols=3)
fig.set_size_inches(15,5)

x = linspace(1, 100, 200)
y = log(x**2) - sqrt(x) + sin(x)
ax1.plot(x, y, color='blue')
ax1.set_xlabel("x")
ax1.set_ylabel("y")

z = sqrt(x) * sin(x) - exp(1/x**2)
ax2.plot(x, z, color='orange')
ax2.set_xlabel("x")
ax2.set_ylabel("z")

ax3.plot(x, y*z,color='purple')
ax3.set_xlabel("x")
ax3.set_ylabel("y * z")

pass


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Comparison operators
Python supports comparison operators on numeric data types. When you carry out a
comparison you get back a Boolean (True,False) value.
Can you write Python code for it?

4 < 5 # less than

4 > 5 # greater than

5 <= 5.0 # less than or equal to

5 == 5 # tests equality

5 == (5**0.5)**2  # the results of this comparison might surprise you

(5**0.5)**2 # the problem is that sqrt(5) can not be represented
            # exactly with floating point numbers. This is not a
            # limitation of only Python but is generally true
            # for all programming languages

# here's one way to test approximate equality when you suspect
# a floating point calculation might be imprecise

epsilon = 0.0000001
(5 - epsilon) <= ((5**0.5)**2) <= (5 + epsilon)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Comparison operators on arrays
NumPy arrays support the comparison operators, returning arrays of Booleans.
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Finally, there are a number of useful methods associated with list objects, such
as reverse() and sort().
Can you write Python code for it?

d = [1, 5, 3, 4, 1, 11, 3]
d.sort() # sort in place
d

d.reverse() # reverse in place
d


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Importing Functions
Python has a mechanism to allow you to build libraries of code, which can then
be "imported" as needed. Python libraries are usually referred to as "modules".
Here’s how we would make functions and various definitions from the  math
module available for use.
Can you write Python code for it?

import math

math.cos(2 * 3.141592654)  # cosine

math.pi  # a constant defined in math

pi = math.pi

math.cos(2 * pi)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
In the first example we retrieved all the elements of x that are larger than 5 (read "x where x is greater than 5"). In the second example we retrieved those elements of x that did not equal six.   The third example is slightly more complicated. We combined the logical_or function with comparison and indexing.  This allowed us to return those elements of the array x that are either less than four or greater than six. Combining indexing and comparison is a powerful concept. See the numpy documentation on logical functions for more information.
Generating Regular Sequences
Creating sequences of numbers that are separated by a specified value or that follow a particular patterns turns out to be a common task in programming. Python and NumPy have functions to simplify this task.
Can you write Python code for it?

arange(10)

# generate numbers from 3 to 12 (non-inclusive) stepping by 4
arange(3, 12, 4)

arange(1,10,0.5)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Indexing and Slicing NumPy arrays
Like the built-in lists, NumPy arrays are zero-indexed.
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Lists
Lists are the simplest "data structure".  Data structures are computational
objects for storing, accessing, and operating on data.
List represent ordered collections of arbitrary objects. We'll begin by working
with lists of numbers.
Can you write Python code for it?

x = [1,2,3,4,5] # a list with the numbers 1..5
x

# a list with floats and ints and complex numbers
y = [2.0, 4, 6, 8, 10.0, 11, (1+1j), 3.14159]
y

# lists of a length. We can use the `len` function to get this
len(x)

len(y)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Mathematical functions applied to arrays
Most of the standard mathematical functions can be applied to numpy arrays however you must use the functions defined in the NumPy module.
Can you write Python code for it?

x = array([2, 4, 6, 8])

cos(x)

sin(x)

log(x)


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
Notice how all the arithmetic operations operate elementwise on arrays. You can also perform arithmetic operations between arrays, which also operate element wise
Can you write Python code for it?


I am working on the file "Intro-Numerical-Computing-Python.ipynb".
The first task is:
NumPy arrays
NumPy is an extension package for Python that provides many facilities for numerical computing.  There is also a related package called SciPy that provides even more facilities for scientific computing. Both NumPy and SciPy can be downloaded from http://www.scipy.org/. NumPy does not come with the standard Python distribution, but it does come as an included package if you use the Anaconda Python distribution.  The NumPy package comes with documentation and a tutorial.  You can access the documentation here: http://docs.scipy.org/doc/.
The basic data structure in NumPy is the array, which you've already seen in several examples above.  As opposed to lists, all the elements in a NumPy array must be of the same type (but this type can differ between different arrays).  Arrays are commonly used to represent matrices (2D-arrays) but can be used to represent arrays of arbitrary dimension ($n$-dimensional arrays).
Arithmetic operations on NumPy arrays
Can you write Python code for it?

from numpy import *

x = array([2,4,6,8,10])
x

type(x)

-x

x**2

x * pi