Scikit-Learn Linear Regression

In this tutorial, you will learn


1.  How to import the Scikit-Learn libraries?

2.  How to import the dataset from Scikit-Learn?

3.  How to explore dataset?

4.  How to split the data using Scikit-Learn train_test_split?

5.  How to implement a Linear Regression Model in Scikit-Learn?

6.  How to predict the output using a trained Linear Regression Model?

7.  How to calculate Mean Squared Error (MSE)?

Linear Regression


In linear regression, we try to build a relationship between the training dataset (X) and the output variable (y). We predict the output variable (y) based on the relationship we have implemented.

1. Import the Libraries

from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import mean_squared_error
import numpy as np

2. Import the Dataset

data = load_iris()

3. Explore the Dataset                                      

how to explore the dataset in scikit-learn machine learning?
how to explore the shape pf the data in scikit-learn machine learning?

4. Splitting the Dataset     

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)

5. Model Implementation and Fitting                                               

how to fit linear regression model in scikit-learn?

6. Model Prediction                                               

predict output in linear regression in scikit-learn machine learning

7. Calculate Mean Squared Error                                               

mean_squared_error(predictions, y_test)
how to calculate mse in scikit learn?



1.  datasets : To import the Scikit-Learn datasets.

2.  shape : To get the size of the dataset.

3.   train_test_split : To split the data using Scikit-Learn.

4.  LinearRegression( ) : To implement a Linear Regression Model in Scikit-Learn.

5.  predict( ) : To predict the output using a trained Linear Regression Model.

6.  mean_squared_error( ) : To calculate Mean Squared Error (MSE).

 You can find the Github link here.