Path: blob/main/C1 - Supervised Machine Learning: Regression and Classification/week2/Optional Labs/C1_W2_Lab05_Sklearn_GD_Soln.ipynb
3283 views
Optional Lab: Linear Regression using Scikit-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:
Utilize scikit-learn to implement linear regression using Gradient Descent
Tools
You will utilize functions from scikit-learn as well as matplotlib and NumPy.
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 set
Scale/normalize the training data
Create and fit the regression model
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.
Make predictions
Predict the targets of the training data. Use both the predict
routine and compute using and .
Plot Results
Let's plot the predictions versus the target values.
Congratulations!
In this lab you:
utilized an open-source machine learning toolkit, scikit-learn
implemented linear regression using gradient descent and feature normalization from that toolkit