Extra-credit Prelab - Linear Regression
In this prelab, you will be introduced to the concept of linear regression and gradient descent. You will then use these concepts to try and find a best fit line to the given Boston housing price data.
Refer to the Submission Instruction page for more information.
Part 1 - Plotting data
It is always a good practice to visualize the data before starting any task. The dataset contains the median value of house price in $1,000 (response, ) and the average number of rooms per dwelling (features, ). Use a scatter plot to display the house prices vs the number of rooms.
Using the following Python script as an example, display the scatter plot of the training and test data.
import pandas as pd import numpy as np import matplotlib.pyplot as plt # Read data file boston = pd.read_csv('BostonHousePrices.csv') # Store the response in y and the feature in x y = boston['Value'].to_numpy() x = boston[['Rooms']].to_numpy() # Create the training set and test set train_y = y[:400] train_x = x[:400] test_x = x[400:] test_y = y[400:] # Plot the data using plt.scatter function # ======== YOUR CODE HERE ======== # ================================
Part 2 - Gradient Descent
In this section, you will fit the linear regression parameter to the dataset. We use to denote the input features (i.e. number of rooms), and to denote the output responses (i.e. house prices). Our goal is to learn a function (or hypothesis) so that can predict the correct responses. We use a cost funtion to measure the accuracy of the prediction. The cost function will be an average of the squared error between the predictions and the actual responses, defined by
where is the size of the dataset. Note that the superscript (i) denotes the i-th dataset. We can choose the hypothesis to be "linear" given by
Now we need to estimate the parameters that minimize the cost function. In order to achieve this, we will use an iterative algorithm called gradient descent which gently updates our parameters in accordance with the gradient of the cost function and a small step size or learning rate . Recall from calculus that the gradient gives the direction of fastest increase of a function, thus by subtracting the gradient times alpha we are moving in the direction of fastest decrease of a function which we need in order to minimize our cost function . In our update equation the current iteration is at timestep while the new update is timestep . The following figure shows an example of the cost function as a function of . The derivative at the red point will give us a direction toward the minimum of the cost function, i.e. decrease if the derivative is positive.
Denoting for the parameters at time step , is updated by
We can rewrite the equation for each parameter (note that is 1): where and are updated simultaneously. .
Using the code below, implement functions to compute the cost junction, , and its partial derivative with respect to the parameter .
- Matrix multiplication can be done by '@' operation in Python.
- We can re-write the cost function by the following matrix form. where
# Variables: x - the first array # y - the second array # Description: calculate the mean squared error loss between two arrays def mse(x, y): return np.mean((x-y)**2) # Variables: theta - linear regression parameter # X - features # y - responses # Description: compute the cost function J, given theta and dataset def cost_lin(theta, X, y): # ======== YOUR CODE HERE ======== # ================================ return J # Variables: theta - linear regression parameter # X - features # y - responses # Description: computes the partial derivative of the cost with respect to parameter theta. def grad_lin(theta, X, y): m = X.shape # ======== YOUR CODE HERE ======== # ================================ return grad
Part 3 - Training
We are now ready to train our parameters given the data set. In the following codes, we initialize the learning rate to 0.01 and set the parameters to the arrays of zeros. Note that we need to add another dimension () to our feature to accommodate .
- Using the code below, train the parameter using the cost function and the gradient function implemented in Assignment 2. You may change the learning rate, number of iterations and the initial parmeter values.
- Plot the cost function with the iterations.
- Use your final parameters to plot the linear fit together with the scatter plot in Assignment 1.
#Train Classifier num_iter = 15000 alpha = 1*10**(-2) # Add a column of ones to train_x ones = np.ones((train_x.shape,1)) train_X = np.hstack((ones, train_x)) # Add a column of ones to test_x ones = np.ones((test_x.shape,1)) test_X = np.hstack((ones, test_x)) # Initialize parameters init_theta = np.zeros((1, train_X.shape)) # ======== YOUR CODE HERE ======== # ================================
Prelab will be graded as follows:
Assignment 1 [0.5 point]
A scatterplot of the dataset (number of rooms vs house price). [0.5 point]
Assignment 2 [0.5 point]
Correct implementation of the cost function and gradient function. [0.5 point]
Assignmet 3 [1.0 point]
Plot of the cost function with iterations. [0.5 point]
Plot of the linear fit on the scatter plot. [0.5 point]