Fitting a line through data with machine learning
Linear regression with machine learning involves testing the linear regression algorithm on unseen data. Here, we will perform 10-fold cross-validation:
- Split the set into 10 parts
- Train on 9 of the parts and test on the one left over
- Repeat this 10 times so that every part gets to be a test set once
Getting ready
As in the previous section, load the dataset you want to apply linear regression to, in this case, the Boston housing dataset:
from sklearn import datasets boston = datasets.load_boston()
How to do it...
The steps involved in performing linear regression are as follows:
- Import the
LinearRegression
object and create an object:
from sklearn.linear_model import LinearRegression lr = LinearRegression()
- Pass the independent and dependent variables to the
fit
method ofLinearRegression
:
lr.fit(boston.data, boston.target) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
- Now, to get the 10-fold cross-validated predictions...