Search⌘ K

Linear Regression in Scikit Learn

Explore how to implement Linear Regression in Scikit Learn by applying it to a real dataset, learning about model training, prediction, and error evaluation. This lesson helps you build foundational skills necessary for regression tasks and sets the stage for advanced machine learning concepts.

Linear Regression in Scikit Learn

We will be looking into how to use Scikit Learn, the famous library for classical Machine Learning, for Linear Regression.

Implementation 1

The following code snippet illustrates how 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)))
  • Line 1-4 imports the necessary modules.

  • Line 6 loads the dataset from the github repository. ...