How to perform linear regression using sklearn

This recipe helps you perform linear regression using sklearn. Linear Regression is a supervised learning algorithm used for continuous variables. It is the relationship between the dependent and independent variable.

Recipe Objective - How to perform linear regression using sklearn?

Linear 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 section, we will see how the Python Scikit-Learn library for machine learning can be used to implement regression functions. We will start with simple linear regression.

Master the Art of Classification in Machine Learning to Become a Pro

Links for the more related projects:-

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

Step:1 Importing Libraries:-

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

score_df = pd.read_csv('student_scores.csv')
print(score_df.shape)

(25, 2)

score_df.head()


score_df.describe()

Step:2 Plotting the 2-d graph

score_df.plot(x='Hours', y='Scores', style='o')
plt.title('Hours vs Percentage')
plt.xlabel('Hours Studied')
plt.ylabel('Percentage Score')
plt.show()

 

Step:3 Data Preparation

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

Step:4 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:5 Training the Algorithm

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

<LinearRegression()

# retrieve the intercept
print(regressor.intercept_)

# retrieving the slope (coefficient of x)
print(regressor.coef_)

2.0181600414346974
[9.91065648]

Step:6 Predictions

y_pred = regressor.predict(X_test)

pred_df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
pred_df

Step:7 Evaluating the Algorithm

from sklearn import metrics
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

Mean Absolute Error: 4.183859899002975
Mean Squared Error: 21.598769307217406 
Root Mean Squared Error: 4.647447612100367 

Download Materials

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.