How to build a Simple Linear Regression Model with Sklearn

02.14.2021

Intro

Linear regression is the most common introduction to machine learning. The basic model fits a straight line. It assume a direct relationship. For example, you can have a list of heights and weight. If you assume the taller people are, the heavier they are, this would be a stright line/linear relationship. In this article, we will learn how to build a basic linear regression model with Sklearn.

The assumption is good, but doesn't capture everything. Modified linear regression models can help with these variations. We will look at these in the future.

Using Linear Regression

To build (fit) a linear regression model we use the LinearRegression class. We create an instance of the class, then we use the fit method. We pass the features (x) and target (y) to this method. The fit function then returns a model that is our linear regression model.

from sklearn.linear_model import LinearRegression
from sklearn.datasets import load_boston

boston = load_boston()
features = boston.data
target = boston.target

regression = LinearRegression()

model = regression.fit(features, target)