How to plot regression line of sklearn model in matplotlib?

This recipe helps you plot regression line of sklearn model in matplotlib.

Recipe Objective - How to plot regression line of Scikit-Learn model in matplotlib?

Regression is a supervised learning algorithm used for continuous variables. It is the relationship between the dependent and independent variable, where the dependent variable is the response variable denoted as "y" and the independent variable is denoted as "x". y = mx + c. Let's understand how it is implemented in Sci-kit learn.

In this recipe, we will see how to plot a regression line in Python using the Scikit-Learn library for machine learning. We will start by creating the linear regression model.

Access Linear Regression ML Project for Beginners with Source Code

Links for the more related projects:-

  1. https://www.projectpro.io/projects/data-science-projects/deep-learning-projects

  2. https://www.projectpro.io/projects/data-science-projects/neural-network-projects 

Steps Showing How To Plot A Linear Regression In Python

The following steps show how to use sklearn for linear regression plot in Python.

Step 1: Import Libraries

import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

from sklearn.linear_model import LinearRegression
score_df = pd.read_csv('student_scores.csv')
score_df.head()

Import Libraries
score_df.describe()

score_df.describe()

Step 2: Data preparation

X = score_df.iloc[:, :-1].values
y = score_df.iloc[:, 1].values

Step 3: Splitting our data

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

Step 4: Training the model

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

LinearRegression()

Step 5: Predictions

y_pred = regressor.predict(X_test)

Step 6: Plot Linear regression Python

plt.scatter(X_train, y_train,color='g')
plt.plot(X_test, y_pred,color='k')

plt.show()

Plot Linear regression Python

 

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.