Program: B.
Tech VII Semester
CSL0777: Machine Learning
Unit No. 2
Supervised learning Part-1
Lecture No. 15
Simple Linear Regression
Mr. Praveen
Gupta
Assistant Professor,
CSA/SOET
Outlines
• Simple Linear Regression
• Simple Linear Regression Model
• Implementation of Simple Linear Regression Algorithm
using Python
• References
Student Effective Learning Outcomes(SELO)
01: Ability to understand subject related concepts clearly along with
contemporary issues.
02: Ability to use updated tools, techniques and skills for effective domain
specific practices.
03: Understanding available tools and products and ability to use it effectively.
Simple Linear
•SimpleRegression
Linear Regression is a type of Regression algorithms
that models the relationship between a dependent variable
and a single independent variable.
•The relationship shown by a Simple Linear Regression
model is linear or a sloped straight line, hence it is called
Simple Linear Regression.
•The key point in Simple Linear Regression is that
the dependent variable must be a continuous/real value.
However, the independent variable can be measured on
continuous or categorical values.
4 / 22
Simple Linear
SimpleRegression
Linear regression algorithm has mainly two
objectives:
•Model the relationship between the two variables. Such
as the relationship between Income and expenditure,
experience and Salary, etc.
•Forecasting new observations. Such as Weather
forecasting according to temperature, Revenue of a company
according to the investments in a year, etc.
4 / 22
Simple Linear Regression
Model
The Simple Linear Regression model can be represented
using the below equation:
y= a0+a1x+ ε
Where,
a0= It is the intercept of the Regression line (can be
obtained putting x=0)
a1= It is the slope of the regression line, which tells
whether the line is increasing or decreasing.
ε = The error term. (For a good model it will be
negligible)
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Problem Statement example for Simple Linear
Regression: Here we are taking a dataset that has two
variables: salary (dependent variable) and experience
(Independent variable). The goals of this problem is:
•We want to find out if there is any correlation between
these two variables
• We will find the best fit line for the dataset.
• How the dependent variable is changing by changing the
independent variable.
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
To implement the Simple Linear regression model in
machine learning using Python, we need to follow the below
steps:
Step-1: Data Pre-processing
The first step for creating the Simple Linear Regression
model is data pre-processing.
•First, we will import the three important libraries, which will
help us for loading the dataset, plotting the graphs, and
creating the Simple Linear Regression model.
import numpy as nm
import matplotlib.pyplot as mtp
import pandas as pd
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Next, we will load the dataset into our code:
data_set= pd.read_csv('Salary_Data.csv')
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
• After that, we need to extract the dependent and
independent variables from the given dataset.
•The independent variable is years of experience, and the
dependent variable is salary. Below is code for it:
x= data_set.iloc[:, :-1].values
y= data_set.iloc[:, 1].values
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
•Next, we will split both variables into the test set and
training set.
• We have 30 observations, so we will take 20 observations
for the training set and 10 observations for the test set.
•We are splitting our dataset so that we can train our model
using a training dataset and then test the model using a test
dataset. The code for this is given below:
# Splitting the dataset into training and test set.
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 1/3
, random_state=0)
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
•By executing the code, we will get x-test, x-train and y-test,
y-train dataset. Consider the below images:
Test-dataset 4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
• Training Dataset:
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Step-2: Fitting the Simple Linear Regression to the Training
Set:
Now the second step is to fit our model to the training
dataset. To do so, we will import the LinearRegression class
of the linear_model library from the scikit learn.
After importing the class, we are going to create an object of
the class named as a regressor. The code for this is given
below:
from sklearn.linear_model import LinearRegression
regressor= LinearRegression()
regressor.fit(x_train, y_train)
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
After executing the above lines of code, we will get the
below output.
Output:
Out[7]: LinearRegression(copy_X=True, fit_intercept=True,
n_jobs=None, normalize=False)
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Step: 3. Prediction of test set result:
•dependent (salary) and an independent variable
(Experience). So, now, our model is ready to predict the
output for the new observations.
•In this step, we will provide the test dataset (new
observations) to the model to check whether it can predict
the correct output or not.
•We will create a prediction vector y_pred, and x_pred,
which will contain predictions of test dataset, and prediction
of training set respectively.
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
#Prediction of Test and Training set result
y_pred= regressor.predict(x_test)
x_pred= regressor.predict(x_train)
On executing the above lines of code, two variables named
y_pred and x_pred will generate in the variable explorer
options that contain salary predictions for the training set
and test set.
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Step: 4. visualizing the Training set results:
Now in this step, we will visualize the training set result. To
do so, we will use the scatter() function of the pyplot library,
which we have already imported in the pre-processing step.
The scatter () function will create a scatter plot of
observations.
mtp.scatter(x_train, y_train, color="green")
mtp.plot(x_train, x_pred, color="red")
mtp.title("Salary vs Experience (Training Dataset)")
mtp.xlabel("Years of Experience")
mtp.ylabel("Salary(In Rupees)")
mtp.show()
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Output:
By executing the above lines of code, we will get the below
graph plot as an output.
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Step: 5. visualizing the Test set results:
The complete code will remain the same as the previous
code, except in this, we will use x_test, and y_test instead of
x_train and y_train.
#visualizing the Test set results
mtp.scatter(x_test, y_test, color="blue")
mtp.plot(x_train, x_pred, color="red")
mtp.title("Salary vs Experience (Test Dataset)")
mtp.xlabel("Years of Experience")
mtp.ylabel("Salary(In Rupees)")
mtp.show()
4 / 22
Implementation of Simple Linear Regression
Algorithm using Python
Output:
By executing the above lines of code, we will get the below
graph plot as an output.
4 / 22
Learning Outcomes
The students have learn and understand the followings
• Simple Linear Regression
• Simple Linear Regression Model
• Implementation of Simple Linear Regression Algorithm
using Python
References
1. Machine Learning for Absolute Beginners by Oliver Theobald. 2019
2. http://noracook.io/Books/Python/introductiontomachinelearningwithpyth
on.pdf
3. https://www.tutorialspoint.com/machine_learning_with_python/machine
_learning_with_python_tutorial.pdf
Thank you