In this notebook, you will implement ridge regression via gradient descent. You will:

- Convert an SFrame into a Numpy array
- Write a Numpy function to compute the derivative of the regression weights with respect to a single feature
- Write gradient descent function to compute the regression weights given an initial weight vector, step size, tolerance, and L2 penalty

Make sure you have the latest version of GraphLab Create (>= 1.7)

```
In [1]:
```import graphlab

```
In [2]:
```sales = graphlab.SFrame('kc_house_data.gl/')

```
```

`get_numpy_data()`

from the second notebook of Week 2.

```
In [7]:
```import numpy as np # note this allows us to refer to numpy as np instead

```
In [8]:
```def get_numpy_data(data_sframe, features, output):
data_sframe['constant'] = 1 # this is how you add a constant column to an SFrame
# add the column 'constant' to the front of the features list so that we can extract it along with the others:
features = ['constant'] + features # this is how you combine two lists
# select the columns of data_SFrame given by the features list into the SFrame features_sframe (now including constant):
features_sframe = data_sframe[features]
# the following line will convert the features_SFrame into a numpy matrix:
feature_matrix = features_sframe.to_numpy()
# assign the column of data_sframe associated with the output to the SArray output_sarray
output_sarray = data_sframe[output]
# the following will convert the SArray into a numpy array by first converting it to a list
output_array = output_sarray.to_numpy()
return(feature_matrix, output_array)

`predict_output()`

function to compute the predictions for an entire matrix of features given the matrix and the weights:

```
In [9]:
```def predict_output(feature_matrix, weights):
# assume feature_matrix is a numpy matrix containing the features as columns and weights is a corresponding numpy array
# create the predictions vector by using np.dot()
predictions = np.dot(feature_matrix, weights)
return(predictions)

```
Cost(w)
= SUM[ (prediction - output)^2 ]
+ l2_penalty*(w[0]^2 + w[1]^2 + ... + w[k]^2).
```

Since the derivative of a sum is the sum of the derivatives, we can take the derivative of the first part (the RSS) as we did in the notebook for the unregularized case in Week 2 and add the derivative of the regularization part. As we saw, the derivative of the RSS with respect to `w[i]`

can be written as:

`2*SUM[ error*[feature_i] ].`

The derivative of the regularization term with respect to `w[i]`

is:

`2*l2_penalty*w[i].`

Summing both, we get

`2*SUM[ error*[feature_i] ] + 2*l2_penalty*w[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, plus `2*l2_penalty*w[i]`

.

**We will not regularize the constant.** Thus, in the case of the constant, the derivative is just twice the sum of the errors (without the `2*l2_penalty*w[0]`

term).

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, plus `2*l2_penalty*w[i]`

.

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). To decide when to we are dealing with the constant (so we don't regularize it) we added the extra parameter to the call `feature_is_constant`

which you should set to `True`

when computing the derivative of the constant and `False`

otherwise.

```
In [10]:
```def feature_derivative_ridge(errors, feature, weight, l2_penalty, feature_is_constant):
derivative = 0
# If feature_is_constant is True, derivative is twice the dot product of errors and feature
if feature_is_constant:
derivative = 2 * np.dot(errors, feature)
# Otherwise, derivative is twice the dot product plus 2*l2_penalty*weight
else:
derivative = 2 * np.dot(errors, feature) + 2 * l2_penalty * weight
return derivative

To test your feature derivartive run the following:

```
In [11]:
```(example_features, example_output) = get_numpy_data(sales, ['sqft_living'], 'price')
my_weights = np.array([1., 10.])
test_predictions = predict_output(example_features, my_weights)
errors = test_predictions - example_output # prediction errors
# next two lines should print the same values
print feature_derivative_ridge(errors, example_features[:,1], my_weights[1], 1, False)
print np.sum(errors*example_features[:,1])*2+20.
print ''
# next two lines should print the same values
print feature_derivative_ridge(errors, example_features[:,0], my_weights[0], 1, True)
print np.sum(errors)*2.

```
```

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. Unlike in Week 2, this time we will set a **maximum number of iterations** and take gradient steps until we reach this maximum number. If no maximum number is supplied, the maximum should be set 100 by default. (Use default parameter values in Python.)

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 before computing our stopping criteria.

```
In [22]:
```def ridge_regression_gradient_descent(feature_matrix, output, initial_weights, step_size, l2_penalty, max_iterations=100):
weights = np.array(initial_weights) # make sure it's a numpy array
iter = 0
#while not reached maximum number of iterations:
while iter <= max_iterations:
iter = iter + 1
# compute the predictions based on feature_matrix and weights using your predict_output() function
predictions = predict_output(feature_matrix, weights)
# compute the errors as predictions - output
errors = predictions - output
for i in xrange(len(weights)): # loop over each weight
# Recall that feature_matrix[:,i] is the feature column associated with weights[i]
# compute the derivative for weight[i].
#(Remember: when i=0, you are computing the derivative of the constant!)
derivative = feature_derivative_ridge(errors, feature_matrix[:, i], weights[i], l2_penalty, i==0)
# subtract the step size times the derivative from the current weight
weights[i] = weights[i] - derivative * step_size
return weights

```
In [13]:
```simple_features = ['sqft_living']
my_output = 'price'

Let us split the dataset into training set and test set. Make sure to use `seed=0`

:

```
In [14]:
```train_data,test_data = sales.random_split(.8,seed=0)

`'sqft_living'`

to predict `'price'`

. Use the `get_numpy_data`

function to get a Numpy versions of your data with only this feature, for both the `train_data`

and the `test_data`

.

```
In [15]:
```(simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output)
(simple_test_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)

Let's set the parameters for our optimization:

```
In [16]:
```initial_weights = np.array([0., 0.])
step_size = 1e-12
max_iterations=1000

First, let's consider no regularization. Set the `l2_penalty`

to `0.0`

and run your ridge regression algorithm to learn the weights of your model. Call your weights:

`simple_weights_0_penalty`

we'll use them later.

```
In [27]:
```simple_weights_0_penalty = ridge_regression_gradient_descent(simple_feature_matrix, output, initial_weights,
step_size, 0.0, max_iterations)

Next, let's consider high regularization. Set the `l2_penalty`

to `1e11`

and run your ridge regression algorithm to learn the weights of your model. Call your weights:

`simple_weights_high_penalty`

we'll use them later.

```
In [29]:
```simple_weights_high_penalty = ridge_regression_gradient_descent(simple_feature_matrix, output, initial_weights,
step_size, 1e11, max_iterations)

```
In [30]:
```import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(simple_feature_matrix,output,'k.',
simple_feature_matrix,predict_output(simple_feature_matrix, simple_weights_0_penalty),'b-',
simple_feature_matrix,predict_output(simple_feature_matrix, simple_weights_high_penalty),'r-')

```
Out[30]:
```

Compute the RSS on the TEST data for the following three sets of weights:

- The initial weights (all zeros)
- The weights learned with no regularization
- The weights learned with high regularization

Which weights perform best?

```
In [36]:
```def compute_RSS(feature_matrix, output, weights):
predictions = predict_output(feature_matrix, weights)
errors = predictions - output
return np.dot(errors, errors)

```
In [37]:
```print compute_RSS(simple_test_feature_matrix, test_output, initial_weights)

```
```

```
In [38]:
```print compute_RSS(simple_test_feature_matrix, test_output, simple_weights_0_penalty)

```
```

```
In [39]:
```print compute_RSS(simple_test_feature_matrix, test_output, simple_weights_high_penalty)

```
```

```
In [51]:
```print simple_weights_0_penalty
print simple_weights_high_penalty

```
```

*QUIZ QUESTIONS*

- What is the value of the coefficient for
`sqft_living`

that you learned with no regularization, rounded to 1 decimal place? What about the one with high regularization? - Comparing the lines you fit with the with no regularization versus high regularization, which one is steeper?
- What are the RSS on the test data for each of the set of weights above (initial, no regularization, high regularization)?

Let us now consider a model with 2 features: `['sqft_living', 'sqft_living15']`

.

First, create Numpy versions of your training and test data with these two features.

```
In [40]:
```model_features = ['sqft_living', 'sqft_living15'] # sqft_living15 is the average squarefeet for the nearest 15 neighbors.
my_output = 'price'
(feature_matrix, output) = get_numpy_data(train_data, model_features, my_output)
(test_feature_matrix, test_output) = get_numpy_data(test_data, model_features, my_output)

```
In [41]:
```initial_weights = np.array([0.0,0.0,0.0])
step_size = 1e-12
max_iterations = 1000

First, let's consider no regularization. Set the `l2_penalty`

to `0.0`

and run your ridge regression algorithm to learn the weights of your model. Call your weights:

`multiple_weights_0_penalty`

```
In [42]:
```multiple_weights_0_penalty = ridge_regression_gradient_descent(feature_matrix, output, initial_weights,
step_size, 0.0, max_iterations)

Next, let's consider high regularization. Set the `l2_penalty`

to `1e11`

and run your ridge regression algorithm to learn the weights of your model. Call your weights:

`multiple_weights_high_penalty`

```
In [43]:
```multiple_weights_high_penalty = ridge_regression_gradient_descent(feature_matrix, output, initial_weights,
step_size, 1e11, max_iterations)

Compute the RSS on the TEST data for the following three sets of weights:

- The initial weights (all zeros)
- The weights learned with no regularization
- The weights learned with high regularization

Which weights perform best?

```
In [50]:
```print multiple_weights_0_penalty
print multiple_weights_high_penalty

```
```

```
In [44]:
```print compute_RSS(test_feature_matrix, test_output, initial_weights)

```
```

```
In [45]:
```print compute_RSS(test_feature_matrix, test_output, multiple_weights_0_penalty)

```
```

```
In [46]:
```print compute_RSS(test_feature_matrix, test_output, multiple_weights_high_penalty)

```
```

```
In [47]:
```np.dot(test_feature_matrix[0, :], multiple_weights_0_penalty)

```
Out[47]:
```

```
In [48]:
```np.dot(test_feature_matrix[0, :], multiple_weights_high_penalty)

```
Out[48]:
```

```
In [49]:
```print test_output[0]

```
```

*QUIZ QUESTIONS*

- What is the value of the coefficient for
`sqft_living`

that you learned with no regularization, rounded to 1 decimal place? What about the one with high regularization? - What are the RSS on the test data for each of the set of weights above (initial, no regularization, high regularization)?
- We make prediction for the first house in the test set using two sets of weights (no regularization vs high regularization). Which weights make better prediction
__for that particular house__?

```
In [ ]:
```