Congratulations! You’ve now built a linear regression algorithm from scratch.
Luckily, we don’t have to do this every time we want to use linear regression. We can use Python’s scikit-learn library. Scikit-learn, or
sklearn, is used specifically for Machine Learning. Inside the
linear_model module, there is a
LinearRegression() function we can use:
from sklearn.linear_model import LinearRegression
You can first create a
LinearRegression model, and then fit it to your
line_fitter = LinearRegression() line_fitter.fit(X, y)
.fit() method gives the model two variables that are useful to us:
line_fitter.coef_, which contains the slope
line_fitter.intercept_, which contains the intercept
We can also use the
.predict() function to pass in x-values and receive the y-values that this line would predict:
y_predicted = line_fitter.predict(X)
num_iterations and the
learning_rate that you learned about in your own implementation have default values within scikit-learn, so you don’t need to worry about setting them specifically!
We have imported a dataset of soup sales data vs temperature.
Run the code to see the scatterplot. Can you envision the line that would fit this data?
sklearn linear regression model and call it
line_fitter object to
Create a list called
sales_predict that is the predicted sales values that
line_fitter would generate from the
temperature as a line, on the same plot as the scatterplot.