# Multiple Regression with Gradient Descent¶

In :
%%javascript
\$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')


In :
import graphlab
import numpy as np

In :
sales = graphlab.SFrame('kc_house_data.gl/')


Now we will write a function that will accept an SFrame, a list of feature names (e.g. ['sqft_living', 'bedrooms']) and an target feature e.g. ('price') and will return two things:

• A numpy matrix whose columns are the desired features plus a constant column (this is how we create an 'intercept')
• A numpy array containing the values of the output
In :
# All features in matrix, output in array
def get_numpy_data(data_sframe, features, output):
data_sframe['constant'] = 1 # add constant variable
features = ['constant'] + features # create list of features
features_sframe = data_sframe[features] # create sframe with all features
feature_matrix = features_sframe.to_numpy() # convert to np matrix

output_sarray = data_sframe[output] # select the output variable
output_array = output_sarray.to_numpy() # convert to np array
return(feature_matrix, output_array)


# Computing the Derivative¶

We are now going to move to computing the derivative of the regression cost function. Recall that the cost function is the sum over the data points of the squared difference between an observed output and a predicted output.

Since the derivative of a sum is the sum of the derivatives we can compute the derivative for a single data point and then sum over data points. We can write the squared difference between the observed output and predicted output for a single point as follows:

(w*[CONSTANT] + w*[feature_1] + ... + w[i] *[feature_i] + ... + w[k]*[feature_k] - output)^2

Where we have k features and a constant. So the derivative with respect to weight w[i] by the chain rule is:

2*(w*[CONSTANT] + w*[feature_1] + ... + w[i] *[feature_i] + ... + w[k]*[feature_k] - output)* [feature_i]

The term inside the paranethesis is just the error (difference between prediction and output). So we can re-write this as:

2*error*[feature_i]

That is, the derivative for the weight for feature i is the sum (over data points) of 2 times the product of the error and the feature itself. In the case of the constant then this is just twice the sum of the errors!

Recall that twice the sum of the product of two vectors is just twice the dot product of the two vectors. Therefore the derivative for the weight for feature_i is just two times the dot product between the values of feature_i and the current errors.

With this in mind complete the following derivative function which computes the derivative of the weight given the value of the feature (over all data points) and the errors (over all data points).

In :
def feature_derivative(errors, feature):
derivative = 2 * np.dot(errors, feature)
return(derivative)


Now we will write a function that performs a gradient descent. The basic premise is simple. Given a starting point we update the current weights by moving in the negative gradient direction. Recall that the gradient is the direction of increase and therefore the negative gradient is the direction of decrease and we're trying to minimize a cost function.

The amount by which we move in the negative gradient direction is called the 'step size'. We stop when we are 'sufficiently close' to the optimum. We define this by requiring that the magnitude (length) of the gradient vector to be smaller than a fixed 'tolerance'.

With this in mind, complete the following gradient descent function below using your derivative function above. For each step in the gradient descent we update the weight for each feature befofe computing our stopping criteria

In :
from math import sqrt # recall that the magnitude/length of a vector [g, g, g] is sqrt(g^2 + g^2 + g^2)

In :
def regression_gradient_descent(feature_matrix, output, initial_weights, step_size, tolerance):
converged = False
weights = np.array(initial_weights) # make sure it's a numpy array
while not converged:
predictions = np.dot(feature_matrix, weights)
errors = predictions - output
for i in range(len(weights)): # loop over each weight
derivative = feature_derivative(errors, feature_matrix[:, i])
weights[i] -= step_size * derivative
converged = True
return(weights)


A few things to note before we run the gradient descent. Since the gradient is a sum over all the data points and involves a product of an error and a feature the gradient itself will be very large since the features are large (squarefeet) and the output is large (prices). So while you might expect "tolerance" to be small, small is only relative to the size of the features.

For similar reasons the step size will be much smaller than you might expect but this is because the gradient has such large values.

# Simple Regression¶

First let's split the data into training and test data.

In :
train_data,test_data = sales.random_split(.8,seed=0)


Although the gradient descent is designed for multiple regression since the constant is now a feature we can use the gradient descent function to estimat the parameters in the simple regression on squarefeet. The folowing cell sets up the feature_matrix, output, initial weights and step size for the first model:

In :
# let's test out the gradient descent
simple_features = ['sqft_living']
my_output = 'price'
(simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output)
initial_weights = np.array([-47000., 1.])
step_size = 7e-12
tolerance = 2.5e7


In :
simple_weights = regression_gradient_descent(simple_feature_matrix, output,initial_weights, step_size,tolerance)
simple_weights

Out:
281.91211911641625

Use your newly estimated weights and your predict_output() function to compute the predictions on all the TEST data (you will need to create a numpy array of the test feature_matrix and test output first:

In :
(test_simple_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)
testpred = predict_output(test_simple_feature_matrix, simple_weights)


Now that you have the predictions on test data, compute the RSS on the test data set. Save this value for comparison later. Recall that RSS is the sum of the squared errors (difference between prediction and output).

In :
rss1 = ((testpred - test_output)**2).sum()

2.75400047593e+14


# Multiple Regression¶

Now we will use more than one actual feature. Use the following code to produce the weights for a second model with the following parameters:

In :
model_features = ['sqft_living', 'sqft_living15']
my_output = 'price'
(feature_matrix, output) = get_numpy_data(train_data, model_features, my_output)
initial_weights = np.array([-100000., 1., 1.])
step_size = 4e-12
tolerance = 1e9


Use the above parameters to estimate the model weights. Record these values for your quiz.

In :
model_weights = regression_gradient_descent(feature_matrix, output, initial_weights, step_size, tolerance)


Use your newly estimated weights and the predict_output function to compute the predictions on the TEST data. Don't forget to create a numpy array for these features from the test set first!

In :
(test_feature_matrix, test_output) = get_numpy_data(test_data, model_features, my_output)
predictions2 = predict_output(test_feature_matrix, model_weights)


Now use your predictions and the output to compute the RSS for model 2 on TEST data.

In :
rss2 = ((predictions2 - test_output)**2).sum()

2.70263446465e+14

print "Simple model RSS: %s, Multiple model RSS: %s" % (rss1, rss2)

Simple model RSS: 2.75400047593e+14, Multiple model RSS: 2.70263446465e+14