Now we have the training set and the test set, let’s use scikit-learn to build the linear regression model!
The steps for multiple linear regression in scikit-learn are identical to the steps for simple linear regression. Just like simple linear regression, we need to import
LinearRegression from the
from sklearn.linear_model import LinearRegression
Then, create a
LinearRegression model, and then fit it to your
mlr = LinearRegression() mlr.fit(x_train, y_train) # finds the coefficients and the intercept value
We can also use the
.predict() function to pass in x-values. It returns the y-values that this plane would predict:
y_predicted = mlr.predict(x_test) # takes values calculated by `.fit()` and the `x` values, plugs them into the multiple linear regression equation, and calculates the predicted y values.
We will start by using two of these columns to teach you how to predict the values of the dependent variable, prices.
LinearRegression from scikit-learn’s
Create a Linear Regression model and call it
Fit the model using
Use the model to predict y-values from
x_test. Store the predictions in a variable called
Now we have: