Search⌘ K
AI Features

Linear Regression in Scikit-Learn

Explore how to apply linear regression models in scikit-learn by learning data preparation, model training, prediction, and evaluation with mean squared error. Understand key regression classes and how they fit into broader machine learning workflows to build foundational skills for real-world data projects.

Linear regression in scikit-learn

We will explore how to use scikit-learn, a popular library for classical machine learning, for linear regression.

Example 1

The following code snippet illustrates how the LinearRegression() class is used in the implementation:

Python 3.5
import pandas as pd
from sklearn import linear_model
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
dataset = pd.read_csv("/usr/local/notebooks/datasets/tips.csv")
X = dataset[["total_bill"]]
y = dataset[["tip"]]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0, train_size=0.7)
reg = linear_model.LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
print("The MSE on test set is {0:.4f}".format(mean_squared_error(y_test, y_pred)))
  • On lines 1–4, we import the necessary modules.

  • On line 6, we load the dataset from the GitHub ...