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
=2)
np.set_printoptions(precision'./deeplearning.mplstyle') plt.style.use(
Scikit-Learn
A useful library that contains implementations of many of the algorithms we work with
.
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’.
Load the Data
= load_house_data()
X_train, y_train = ['size(sqft)','bedrooms','floors','age'] X_features
Scale the Data
= StandardScaler()
scaler = scaler.fit_transform(X_train)
X_norm print(f"Peak to Peak range by column in Raw X:{np.ptp(X_train,axis=0)}")
print(f"Peak to Peak range by column in Normalized X:{np.ptp(X_norm,axis=0)}")
Create Regression Model
= SGDRegressor(max_iter=1000)
sgdr
sgdr.fit(X_norm, y_train)print(sgdr)
print(f"number of iterations completed: {sgdr.n_iter_}, number of weight updates: {sgdr.t_}")
View Parameters
= sgdr.intercept_
b_norm = sgdr.coef_
w_norm 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
We will use both the predict routine and compute using w and b
# make a prediction using sgdr.predict()
= sgdr.predict(X_norm)
y_pred_sgd # make a prediction using w,b.
= np.dot(X_norm, w_norm) + b_norm
y_pred print(f"prediction using np.dot() and sgdr.predict match: {(y_pred == y_pred_sgd).all()}")
print(f"Prediction on training set:\n{y_pred[:4]}" )
print(f"Target values \n{y_train[:4]}")
Plot Results
# plot predictions and targets vs original features
=plt.subplots(1,4,figsize=(12,3),sharey=True)
fig,axfor i in range(len(ax)):
= 'target')
ax[i].scatter(X_train[:,i],y_train, label
ax[i].set_xlabel(X_features[i])=dlc["dlorange"], label = 'predict')
ax[i].scatter(X_train[:,i],y_pred,color0].set_ylabel("Price"); ax[0].legend();
ax["target versus prediction using z-score normalized model")
fig.suptitle( plt.show()