Linear Regression in Scikit Learn

Scikit Learn provides an easy-to-use implementation of Linear Regression. You can discover more about that implementation here.

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.

Press + to interact
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. ...