Sale!

HW2 Regression Fun Time!

\$30.00

Category:

Description

HW2 Regression Fun Time!

Please run the cell below to import libraries needed for this HW. Please use the autograd numpy, otherwise you will have issues.

import matplotlib.pyplot as plt
Q1) [10 points] Implement the linear regression model discussed in class below. The function (called model) takes in an array of data points, x , and an array of weights, w, and returns a vector y_predicted containing the linear combination for each of the data points. This is the linear model we have discussed in class. You can assume each data point in x only has one feature. The length of the vector being returned should be the same as x.

def model(x,w):
y_predicted = np.dot(x.T,w[1:])+w[0]
return y_predicted
Q2) [10 ponts] Implement the least squares function discussed in class below. The function should take in an array of weights, w, an array of x’s and an array of ys. It should use the model function implemented above and return a float indicating the total cost.

def least_squares(w,x,y):
cost = np.sum((model(x,w)-y)**2)/float(y.size)
return cost
Q3) [5 point] This one is a freebie from HW1. Copy and paste your gradient descent function here. Specifically, the one that takes in the cost function as input and return the weight and cost history. We will be using a fixed alpha for this HW. The only difference is that this function should now also take in as input an array of x and ys, corresponding to our data. The w,x, and y are given as inputs to the cost funtion and its gradient.

weight_history = [w]
starting_cost = g(w,x,y)
cost_history = [starting_cost]
for i in range(1,max_its):
# define a new and previous value using a step
w = w – alpha*step
curr_cost = g(w,x,y)
weight_history.append(w)
cost_history.append(curr_cost)

return weight_history,cost_history
Q4) [1 points] Run the code below to import a dataset. Then, plot a scatter plot of the data (x vs y).

# import the dataset
import matplotlib.pyplot as plt
csvname = ‘kleibers_law_data.csv’
x = np.log(data[:-1,:])
y = np.log(data[-1:,:])
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x7f8eb99d0ca0>

Q5) [5 points] Use your gradient descent function to learn a linear regression model for the x and y above using the following parameters and plot the cost_history over the 1000 iterations

g = least_squares function you implemented

w = [w_0, w_1] , where w_0 and w_1 are random numbers between -0.1 and 0.1

max_its=1000

alpha=0.01

w = np.random.uniform(-0.1, 0.1, 2)

# plot cost over 1000
plt.plot(gd_results[1])
[<matplotlib.lines.Line2D at 0x7f8eb9bbd1c0>]

Q6) [5 points] Use the learned weights from above (note that the “learned” weights are the ones with the lowest cost) to plot the learned line. You can use the linspace method (shown below) to generate a list of xs that you can use for plotting. You need to generate a y for each of the candidate xs using the learned weights. On the same figure, also plot the scatter plot from Q4.

s = np.linspace(np.min(x),np.max(x))
plt.scatter(x,y) # from Q4

# get y for each x using learned weights …?
x2 = s
y2 = (gd_results[0][-1][1]*x2) + gd_results[0][-1][0]# mx+b
plt.plot(x2,y2,”red”)
1000
50

Q7) [1 points] Run the code below to import a dataset. Then, plot a scatter plot of the data (x vs y).

x = data[:-1,:]
y = data[-1:,:]
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x7f8eb9c42490>

Q8) [10 ponts] Implement the least absolute deviations function discussed in class. The function should take in an array of weights, w, an array of x’s and an array of ys. It should use the model function implemented in Q1 and return a float indicating the total cost.

def least_absolute_deviations(w,x,y):
cost = np.sum(np.absolute(model(x,w)-y))/float(y.size)
return cost
Q9) [5 points] Use the gradient descent function twice to learn two linear models using the new x and y from Q7 using the following parameters and plot the cost_history for both runs on the same plot. Make the plot for the first run blue and the plot for the second run red.

Run 1) g = least_squares function

w = [1.0,1.0]

max_its=100

alpha=0.1

Run 2) g = least_absoulte_deviations

w = [1.0,1.0]

max_its=100

alpha=0.1

w = np.array([1.0,1.0])

[<matplotlib.lines.Line2D at 0x7f8eb9c75790>]

Q10) [5 points] Use the learned weights from above to plot the two learned lines (use same colors as above). You can use the linspace method again to generate a list of xs that you can use. On the same figure, also plot the scatter plot from Q7. Which of these lines look like a better fit to you? The green (least absolute deviations) is a better fit.

s = np.linspace(np.min(x),np.max(x))
plt.scatter(x,y)
x_l1 = s
plt.plot(x_l1,y_l1,”red”)

x_l2 = s
plt.plot(x_l2,y_l2,”green”)
[<matplotlib.lines.Line2D at 0x7fddb1b46880>]

Q11) [6 points] Implement the mean squared error (MSE) and the mean absolute deviation functions from class. The functions should take in as input an array of actual ys and an array of predicted ys and return the prediction error.

def MSE(y_actual,y_pred):
error = np.sum((y_pred-y_actual)**2)/float(y_actual.size)
return error

error = np.sum(np.absolute(y_pred-y_actual))/float(y_actual.size)
return error
Q12) [4 points] Use the functions above to report the MSE and MAD for the two models learned in Q9, using the x and y from Q7. You should have 4 values total, two for each model. Which model is doing better? (Note that since you are evaluating the model on the training data, this corresponds to the training error) It looks like the MAD for LAD has the lowest error value and the values for both MSE and MAD do better for LAD

# what are the predicted values?
y_predicted_lsf = (model(x,w)-y)**2

# calculate the values
mse_lsf = MSE(y,y_pred_lsf)

print(“LSF: MSE = ” + str(mse_lsf) + ” MAD = ” + str(mad_lsf))
LSF: MSE = 119.69904205469814 MAD = 10.359401368163274
Q13) [6 points] Implement the L1 and L2 regularizers from class. Recall the regularizers take in input the weight vector and return a score based on the L1 or L2 norm of the weights

def L2_regularizer(w):
L2 = np.sum(w**2)
return L2

def L1_regularizer(w):
L1 = np.sum(w)
return L1
Q14) [12 points] Turn the least squares function implemented in Q2 into the Ridge (L2) and Lasso (L1) least squares (covered in class) using the functions implemented in Q13. Recall that  λ  is used as a hyperparameter to specify the smoothness of the function learned (higher  λ  leads to simpler and smoother functions whereas lower  λ  leads to better fitting to the data.  λ=0  is the same as non-regularized least-squares)

def ridge(w,x,y,lmbda):
data = least_squares(w,x,y)
L2 = L2_regularizer(w)
cost = data + (lmbda*L2)
return cost

def lasso(w,x,y,lmbda):
data = least_squares(w,x,y)
L1 = L1_regularizer(w)
cost = data + (lmbda*L1)
return cost
The rest of the questions are for bonus points, but highly recommended
Q15) [2 points] The file ‘weatherHistory.csv’has 96,454 lines, each one corresponding to a data point. Each row (i.e., data point), has several columns. Read the data file. Note that the first line is the header describing each column.

import csv
file = open(“weatherHistory.csv”)
print(header) # will use to determine proper indexing
rows = [] # will use later
rows.append(row)
# print(rows)
file.close()
[‘Date’, ‘Summary’, ‘Precip’, ‘Temperature’, ‘Apparent_Temperature’, ‘Humidity’, ‘Wind_Speed’, ‘Wind_Bearing’, ‘Visibility’, ‘Loud_Cover’, ‘Pressure’, ‘Daily_Summary’]
Q16) [5 points] Use the data above to set y to be the temperatures and X to be the following columns (in order): [Apparent_Temperature, Humidity, Wind_Speed, Wind_Bearing, Visibility, Pressure] Basically, we want to see whether we can predict the temperature, using the features in X.

y = []
x = [[]]
for row in rows:
y.append(row[3])
x_sub = [row[4],row[5],row[6],row[7],row[8],row[10]]
x.append(x_sub)
We are now going to using a well-known ML library called sklearn. If you do not have it installed, please do so using this instruction: https://scikit-learn.org/stable/install.html

sklearn comes with many models already implemented, below we import the standard linear regression, Ridge, and Lasso models from sklearn. We also import a method that can divide our data into train/test sets. Please run the cell below.

from sklearn.linear_model import LinearRegression, Ridge, Lasso
from sklearn.model_selection import train_test_split
This library is very easy to use. We briefly went over it in the class but please use the API and user guide ( https://scikit-learn.org) to learn exactly how to use this library.

For instance, learning a linear regression model using sklearn can be done in two lines:

linearModel = LinearRegression()

linearModel.fit(x_train, y_train)

Q17) [2 points] Use the train_test_split to divide your modified data from Q16 into 80% train, 20% test.