A Small Example of Linear Regression
00:00 A small example of linear regression. In this example, you’ll apply what you’ve learned so far to solve a small regression problem. You’ll learn how to create datasets, split them into training and test subsets, and use them for linear regression.
Now that you’ve imported everything you need, you can create two small arrays,
y, to represent the observations and then split them into training and test sets, just as you have done previously.
The dataset has twenty observations, or
y pairs. You specify the argument
test_size=8 so that the dataset is divided into a training set with twelve observations and a test set with eight observations.
Now you can use the training set to fit the model. Linear regression creates the object that represents the model, while
.fit() trains, or fits, the model and returns it. With linear regression, fitting the model means determining the best intercept and slope values of the regression line, and you can see those values by querying the attributes as seen onscreen.
.score() returns the coefficient of determination, or R squared, for the data passed. Its maximum is 1. The higher the R-squared value, the better the fit. In this case, the training data yields a slightly higher coefficient.
Become a Member to join the conversation.