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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.