C1 W2 Lab05 Sklearn GD Soln
C1 W2 Lab05 Sklearn GD Soln
Learn
There is an open-source, commercially usable machine learning toolkit called scikit-learn. This
toolkit contains implementations of many of the algorithms that you will work with in this
course.
Goals
In this lab you will:
Tools
You will utilize functions from scikit-learn as well as matplotlib and NumPy.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import SGDRegressor
from sklearn.preprocessing import StandardScaler
from lab_utils_multi import load_house_data
from lab_utils_common import dlc
np.set_printoptions(precision=2)
plt.style.use('./deeplearning.mplstyle')
Gradient Descent
Scikit-learn has a gradient descent regression model sklearn.linear_model.SGDRegressor. Like
your previous implementation of gradient descent, this model performs best with normalized
inputs. sklearn.preprocessing.StandardScaler will perform z-score normalization as in a previous
lab. Here it is referred to as 'standard score'.
View parameters
Note, the parameters are associated with the normalized input data. The fit parameters are very
close to those found in the previous lab with this data.
b_norm = sgdr.intercept_
w_norm = sgdr.coef_
print(f"model parameters: w: {w_norm}, b:{b_norm}")
print( "model parameters from previous lab: w: [110.56 -21.27 -32.71 -
37.97], b: 363.16")
Make predictions
Predict the targets of the training data. Use both the predict routine and compute using w and
b.
Plot Results
Let's plot the predictions versus the target values.
Congratulations!
In this lab you: