Machine Learning Algorithm With Python Implementation

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Machine Learning Algorithm with Python

Implementation
Faculty Name: Mrs. Reshma Gulwani
Assistant Professor
Department of Information Technology
DY Patil Deemed to be University
Ramrao Adik Institute Of Techchnology
Contents

Regression

Types of Regression

Linear Regression with Python Implementation

Uses of Linear Regression in COVID’19

2
Regression

 Regression analysis is statistical tool and form of predictive


modeling technique to maintain a relationship between two
variables.
 One of these variables is called as predictor variable or
independent variable.
 Other variable is called as Response variable or dependent
variable.

Machine Learning algorithm with


3
python implementation
Need of Regression

 Helps seller
 To predict the sale of products in the future based on past
buying behavior.

 Helps sports analyst


 To predict the number of runs or goals a player would score in
the coming matches based on previous performances.

 Helps organizations
 To figure out how much they would pay to a new joinee based
on the years of experience.

Machine Learning algorithm with


4
python implementation
Types of Regression

Machine Learning algorithm with


5
python implementation
Simple Linear Regression

 One of the easiest Statistical model.


 Two variables are related through following equation.
 The general mathematical form of equation is:

y = ax + b

 y is response variable
 x is predictor variable
 Power of both the variables is 1.
 a and b are constants which are called the coefficients.
 Mathematically a linear relationship represents a straight line
when plotted as a graph
Machine Learning algorithm with
6
python implementation
What is linear

 First, let’s say that you are shopping at Walmart. Whether


you buy goods or not, you have to pay $2.00 for parking
ticket.

 Each apple price $1.5, and you have to buy an (x) item of


apple.

 It’s easy to predict (or calculate) the Price based on Value and
vice versa using the equation.
y=2+1.5x 

Machine Learning algorithm with


7
python implementation
Price list of Apples

Machine Learning algorithm with


8
python implementation
Visualize the data

Machine Learning algorithm with


9
python implementation
Simple Linear Regression with Python Implementation

Problem Statement:

Predict salary of person based on years of experience they


have

Machine Learning algorithm with


10
python implementation
Data Set(Salary.CSV)=30 Records

Years of Experience Salary Years of Experience Salary


1.1 39343 4.9 67938
1.3 46205 5.1 66029
1.5 37731 5.3 83088
2 43525 5.9 81363
2.2 39891 6 93940
2.9 56642 6.8 91738
3 60150 7.1 98273
3.2 54445 7.9 101302
3.2 64445 8.2 113812
3.7 57189 8.7 109431
3.9 63218 9 105582
4 55794 9.5 116969
4 56957 9.6 112635
4.1 57081 10.3 122391
4.5 61111 10.5 121872

Machine Learning algorithm with


11
python implementation
Scenario

• If you are a HR officer, you got a candidate with 5


years of experience.
• Then what is the best salary you should offer to
him?”

Machine Learning algorithm with


12
python implementation
Plot the data (Scatter plot)

Machine Learning algorithm with


13
python implementation
Plot the data

All the points is not in a line BUT they are in a line-


shape! It’s linear!

How to pick the best number for him? It’s time to use
Machine Learning to predict the best salary for our
candidate.
Machine Learning algorithm with
14
python implementation
Step 1: Import Libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Machine Learning algorithm with


15
python implementation
Step 2:Importing the data set

dataset = pd.read_csv("Salary_Data.csv")

Independent variable: Years of Experience.


Dependent variable: Salary.
X axis represents Years of Experience.
Y axis represents Salary.

Machine Learning algorithm with


16
python implementation
Retrieve Data from data set

Retrieve years of experience column

X = dataset.iloc[:, :-1].values

Retrieve Salary Column

y = dataset.iloc[:, -1].values

Machine Learning algorithm with


17
python implementation
Step 3: Splitting the dataset into the Training set and Test set

Training dataset for training the model and then check the
performance of the model on the test dataset.
Use the train_test_split method from library model_selection

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)

 #If data set contains 30 observations then


 #Test set contains 10 observations
 #Training set contains 20 observations
Machine Learning algorithm with
18
python implementation
X_train, y_train, X_test and y_test

Machine Learning algorithm with


19
python implementation
Step 4: Fit Simple Linear Regression model to training set

 To use the LinearRegression class from the library


sklearn.linear_model.
 First we create an object of the LinearRegression class and
call the fit method passing the X_train and y_train.

from sklearn.linear_model import LinearRegression


regressor = LinearRegression()
regressor.fit(X_train, y_train)

Machine Learning algorithm with


20
python implementation
Step 5: Predicting the Test set results
regressor is used in previous step for trained the dataset.
Now we will use it to predict the results of the test set and
compare the predicted values with the actual values

y_pred = regressor.predict(X_test)

Machine Learning algorithm with


21
python implementation
Step 6:Visualising the Training set results

# plot the actual data points of training set


 plt.scatter(X_train, y_train, color = 'red')
# plot the regression line
 plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')

plt.xlabel('Years of Experience')

plt.ylabel('Salary')

plt.show()
Machine Learning algorithm with
22
python implementation
Output

Machine Learning algorithm with


23
python implementation
Step 7:Visualizing the Test set results

plt.scatter(X_test, y_test, color = 'red')


plt.plot(X_train, regressor.predict(X_train), color =
'blue')
 y_test, color = 'red')
 ()
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

Machine Learning algorithm with


24
python implementation
Output

Machine Learning algorithm with


25
python implementation
Step 8:Make new predictions

Make new predictions for data points that do not exist in the
dataset.
Like for a person with 12 years experience.
new_salary_pred = regressor.predict([[12]])

The predicted salary of a person with 12 years experience is


[138967.5015615].

Machine Learning algorithm with


26
python implementation
Advantages

 Due to its simplicity, It is most widely used for predictions.

 Easier to implement.

Machine Learning algorithm with


27
python implementation
Disadvantages

 performs well when the dataset is linearly separable


 very sensitive to outliers or anomalies. So, outliers should be
analyzed and removed before applying Linear Regression to
the dataset.
 Linear Regression should not be used, If the number of
observations are lesser than the number of features,
otherwise it may lead to overfit.

Machine Learning algorithm with


28
python implementation
Multiple Linear Regression

 Extension of Simple Linear Regression


 More than one predictor variable and one response variable
 The general Mathematical form of equation is

y = a + b1x1+b2x2+…bnxn
 y is the response variable.
 a, b1, b2...bn are the coefficients.
 x1, x2, ...xn are the predictor variables.

Machine Learning algorithm with


29
python implementation
Application

 The selling price of a house depend on multiple factors


 location
 The number of bedrooms
 The number of bathrooms
 The year the house was built
 The square footage of the lot etc

 The height of a child can depend on multiple factors


 The height of parents,
 Nutrition,
 Environmental factors.
Machine Learning algorithm with
30
python implementation
Recent Uses of Linear Regression in COVID-19

• The article named Linear Regression Analysis to predict


the number of deaths in India due to SARS-CoV-2 at 6
weeks from day 0 (100 cases - March 14th 2020) published
online on April 2,2020 in Elsevier Public Health Emergency
Collection.

• Website link:
www.ncbi.nlm.nih.gov/pmc/articles/PMC7128942/

Machine Learning algorithm with


31
python implementation
Recent Uses of Linear Regression in COVID-19

 Cuts in spending on durable goods- 

 Due to reduced supply and shortage of components,


promotional offers and discounts are also being cut on
finished products which is leading to cuts in spending on
durable goods such as refrigerators, air conditioners,
LCDs etc.
 This indicator can be  monitored with the help
of Regression algorithms where the “outcome variable”
of downfall in demand can be analyzed based on the
“input features” of cutting promotional offers and
discounts.
Machine Learning algorithm with
32
python implementation
Recent Uses of Linear Regression in COVID-19

 Utilization of hospital beds- 


 Utilization forecasting uses linear regression models to
extrapolate and make predictions based on available data.

 This prediction will help to let the active cases remain


below the threshold capacity of hospitals to treat the
infected people.

Machine Learning algorithm with


33
python implementation
Thank You

You might also like