SlideShare a Scribd company logo
Linear Regression and Logistic
Regression
Compiled by : Dr. Kumud Kundu
Linear Regression
• In statistical modeling, linear regression analysis is a set of
statistical process that is utilized for estimating the relationships
between a dependent variable and one or more independent
variables. * (Wikipedia)
• Or
• It analyzes the influence of one or more independent variables on a
dependent variable
• Dependent variable — The attribute that is to be predicted
• Independent variables — The factors under consideration which
influence the prediction of dependent variable.
Income (₹)
Expenditure(₹)
Expenditure(₹)
Income (₹)
If the relationship between Y and X is
believed to be linear, then the equation for a
line may be appropriate: Y = β1 + β2X, where
β1 is an intercept term and β2 is a slope
coefficient.
• y is termed as the dependent or study variable and X is termed as the independent or explanatory
• variable. The terms are the parameters of the model.
• These parameters are usually called as regression coefficients.
• The unobservable error component accounts for the difference between the true and observed
realization of y
• For each point the differences between the predicted point and the actual observation is the residue
• For simple linear regression we choose sum squared error (SSE)
• S (predictedi – actuali)2 = S (residuei)2. Thus, find the line which minimizes the sum of the squared
residues (e.g. least squares)
Obtain Linear Regression for the following dataset
Finding Learning parameters of
Linear Regression line
https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
Obtain Linear Regression for Data
https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
Multiple Linear Regression
8
Y = 𝛽0 + 𝛽1 𝑋1 + 𝛽2 𝑋2 + ⋯ + 𝛽N 𝑋N + 𝜀
https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
Logistic Regression
• Logistic regression is a form of regression analysis in which the
dependent or outcome variable is binary or dichotomous.
• It is the form of regression practically used for label classification
rather than prediction of continuous value
Logistic Regression?
What is the “Logistic” component?
• The term “Logistic” is taken from the Logit function that is used in this
method of classification. The logit function is log of odds and is equivalent
to Sigmoid function, which takes any real value between zero and one.
Sigmoid function:
What is the “Regression” component?
Methods used to quantify association between an outcome and
predictor variables.
So, Logistic Regression is defined as technique which instead of modeling
the outcome (Y) directly, models the log odds(Y) using the logistic function.
 
 
exp
( ) ln
(1 ) 1 exp
zp
LOGIT p z p
p z
 
    
  
Odds of an Event
• Odds are used to describe the chance of an event occurring.
• The odds are the ratios that compare the number of ways the event
can occur with the number of ways the event cannot occur.
• The odds of an event is the ratio of the probability of an event to the
probability of its complement.
• In other words, it is the ratio of favorable outcomes to unfavorable
outcomes.
• We say the odds are "3 to 2," which means 3 favorable outcomes to every
2 unfavorable outcomes, and we write 3 : 2.
( ) log
(1 )
p
LOGIT p z
p
 

exp( )
1 exp( )
z
p
z


 
 
exp
( ) ln
(1 ) 1 exp
zp
LOGIT p z p
p z
 
    
  
The Logistic Curve
z (log odds)
p(probability)
*https://www.machinelearningplus.com/wp-content/uploads/2017/09/linear_vs_logistic_regression.jpg
Linear Regression Vs. Logistic Regression
The Logistic Regression Model
Logistic Regression:
ln
P Y
1−P Y
= 𝛽0 + 𝛽1 𝑋1 + 𝛽2 𝑋2 + ⋯ + 𝛽K 𝑋K
Linear Regression:
The Logistic Regression Model
 
  0 1 1 2 2 K K
P Y
ln
1-P Y
X X X   
 
      
 
K
Independent variables
 
 





 YP1
YP
ln is the log(odds) of the outcome.
Binary outcome
The Logistic Regression Model
 
  0 1 1 2 2 K K
P Y
ln
1-P Y
X X X   
 
      
 
K
 
 





 YP1
YP
ln is the log(odds) of the outcome.
Regression coefficients
Form for Predicted Probabilities
 
 
 
 
 
0 1 1 2 2 K K
0 1 1 2 2 K K
0 1 1 2 2 K K
P Y
ln
1-P Y
exp
P Y
1 exp
X X X
X X X
X X X
   
   
   
 
      
 
   

    
K
c
K
K
In this latter form, the logistic regression model directly relates the
probability of Y to the predictor variables.
Relationship between
Odds & Probability
 
 
 
 
 
 
Probability event
Odds event =
1-Probability event
Odds event
Probability event
1+Odds event

The Logistic Regression Model
ln
Pr Y
1 − Pr Y
= 2.67 − 0.13 ∗ X1
⇕
Pr Y =
exp 2.67 − 0.13 ∗ X1
1 + exp 2.67 − 0.13 ∗ X1
What is the effect of X1 on Y?
= exp −0.13 = 0.88
This implies that for every 1 unit increase in X1, the odds of Y decrease by 12%.
Regression of Log Odds
22
Medication
Dosage
#
Cured
Total
Patients
Probability:
# Cured/Total
Patients
Odds:
p/(1-p) =
# cured/
# not cured
Log
Odds:
ln(Odds)
20 1 5 .20 .25 -1.39
30 2 6 .33 .50 -0.69
40 4 6 .67 2.0 0.69
50 6 7 .86 6.0 1.79
0 10 20 30 40 50 60
+2
-2
0
• y = .11x – 3.8 - Regression equation
• We transform that log odds point to a probability: p = elogit(x)/(1+elogit(x))
• For example assume we want p for dosage = 10
Logit(10) = .11(10) – 3.8 = -2.7
p(10) = e-2.7/(1+e-2.7) = .06
prob.
Cured
0
1
Linear Regression Regression Model Implementation using
sklearn
from sklearn.linear_model import LinearRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
linreg = LinearRegression()
linreg.fit(X_train, y_train)
linreg.predict(X_test)
Logistic Regression Model Implementation using sklearn
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
logreg.predict(X_test)
Evaluation Metrics of Regression models
from sklearn import metrics
• print('Absolute Mean Error',metrics.mean_absolute_error(y_pred,y_test))
• print('Mean Squared Error', metrics.mean_squared_error(y_pred,y_test))
• print('Root Mean Square Error', metrics.r2_score(y_pred, y_test))
Confusion Matrix
A confusion matrix is a table that is often used to describe the performance
of a classification model on a set of test data for which the true values are
known.
• True Positives (TP) - These are the
correctly predicted positive values which
means that the value of actual class is yes
and the value of predicted class is also
yes.
• True Negatives (TN) - These are the
correctly predicted negative values which
means that the value of actual class is no
and value of predicted class is also no.
• False Positives (FP) Type 1 error–
When actual class is no and predicted
class is yes.
• False Negatives (FN) Type 2 error–
When actual class is yes but predicted
class in no.
https://blog.exsilio.com/all/accuracy-precision-recall-f1-score-interpretation-of-performance-measures/
• Accuracy, Precision, and
Recall
• Accuracy is the proportion of
true results among the total
number of cases examined.
• Accuracy =
(TP+TN)/(TP+FP+FN+TN)
• Accuracy is a valid choice of
evaluation for classification
problems which have no class
imbalance.
Evaluation Metrics of Classification models
Recall (Senstivity)
What proportion of Actual Positives is correctly
classified?
Recall = (TP)/(TP+FN)
Any Recall value above 0.5 is considered as good.
Precision
Precision is the ratio of correctly predicted positive
observations to the total predicted positive
observations.
High precision relates to the low false positive rate.
Precision = TP/TP+FP
F1 Score: It is the harmonic mean of precision and
recall.
The F1 score is a number between 0 and 1
Always aim to have a model with both good precision and recall.
Compute Accuracy, Precision, Recall, F1-Score
• The total outcome values are:
• TP = 30, TN = 930, FP = 30,
FN = 10
https://www.analyticsvidhya.com/blog/2020/04/confusion-matrix-machine-learning/
• The accuracy for a model turns out to
be: 96%
https://www.analyticsvidhya.com/blog/2020/04/confusion-matrix-machine-learning/
50% percent of the correctly predicted
cases turned out to be positive cases.
Whereas 75% of the positives were
successfully predicted by the model.
https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
Calculate the F1 score for binary prediction
problems using:
from sklearn.metrics import f1_score
y_true = [0, 1, 1, 0, 1, 1]
y_pred = [0, 0, 1, 0, 0, 1]
f1_score(y_true, y_pred)
Linear Regression and Logistic Regression in ML

More Related Content

What's hot

Polynomial regression
Polynomial regressionPolynomial regression
Polynomial regression
naveedaliabad
 
Machine Learning With Logistic Regression
Machine Learning  With Logistic RegressionMachine Learning  With Logistic Regression
Machine Learning With Logistic Regression
Knoldus Inc.
 
Module 3: Linear Regression
Module 3:  Linear RegressionModule 3:  Linear Regression
Module 3: Linear Regression
Sara Hooker
 
Classification and regression trees (cart)
Classification and regression trees (cart)Classification and regression trees (cart)
Classification and regression trees (cart)
Learnbay Datascience
 
Bias and variance trade off
Bias and variance trade offBias and variance trade off
Bias and variance trade off
VARUN KUMAR
 
Logistic Regression Analysis
Logistic Regression AnalysisLogistic Regression Analysis
Logistic Regression Analysis
COSTARCH Analytical Consulting (P) Ltd.
 
Feature selection concepts and methods
Feature selection concepts and methodsFeature selection concepts and methods
Feature selection concepts and methods
Reza Ramezani
 
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Simplilearn
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
Venkata Reddy Konasani
 
Lda
LdaLda
Feature selection
Feature selectionFeature selection
Feature selection
Dong Guo
 
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
Edureka!
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning Algorithms
Md. Main Uddin Rony
 
ML - Simple Linear Regression
ML - Simple Linear RegressionML - Simple Linear Regression
ML - Simple Linear Regression
Andrew Ferlitsch
 
Simple Linear Regression (simplified)
Simple Linear Regression (simplified)Simple Linear Regression (simplified)
Simple Linear Regression (simplified)
Haoran Zhang
 
Linear regression
Linear regressionLinear regression
Linear regression
vermaumeshverma
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
Abhimanyu Dwivedi
 
Linear regression
Linear regressionLinear regression
Linear regression
Karishma Chaudhary
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
DrZahid Khan
 

What's hot (20)

Polynomial regression
Polynomial regressionPolynomial regression
Polynomial regression
 
Machine Learning With Logistic Regression
Machine Learning  With Logistic RegressionMachine Learning  With Logistic Regression
Machine Learning With Logistic Regression
 
Module 3: Linear Regression
Module 3:  Linear RegressionModule 3:  Linear Regression
Module 3: Linear Regression
 
Classification and regression trees (cart)
Classification and regression trees (cart)Classification and regression trees (cart)
Classification and regression trees (cart)
 
Bias and variance trade off
Bias and variance trade offBias and variance trade off
Bias and variance trade off
 
Logistic Regression Analysis
Logistic Regression AnalysisLogistic Regression Analysis
Logistic Regression Analysis
 
Feature selection concepts and methods
Feature selection concepts and methodsFeature selection concepts and methods
Feature selection concepts and methods
 
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
Linear Regression Analysis | Linear Regression in Python | Machine Learning A...
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Lda
LdaLda
Lda
 
Feature selection
Feature selectionFeature selection
Feature selection
 
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
Scikit Learn Tutorial | Machine Learning with Python | Python for Data Scienc...
 
Classification Based Machine Learning Algorithms
Classification Based Machine Learning AlgorithmsClassification Based Machine Learning Algorithms
Classification Based Machine Learning Algorithms
 
ML - Simple Linear Regression
ML - Simple Linear RegressionML - Simple Linear Regression
ML - Simple Linear Regression
 
Simple Linear Regression (simplified)
Simple Linear Regression (simplified)Simple Linear Regression (simplified)
Simple Linear Regression (simplified)
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Machine learning session4(linear regression)
Machine learning   session4(linear regression)Machine learning   session4(linear regression)
Machine learning session4(linear regression)
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 

Similar to Linear Regression and Logistic Regression in ML

Logistic regression with SPSS examples
Logistic regression with SPSS examplesLogistic regression with SPSS examples
Logistic regression with SPSS examples
Gaurav Kamboj
 
IBM401 Lecture 12
IBM401 Lecture 12IBM401 Lecture 12
IBM401 Lecture 12
saark
 
Stat 1163 -correlation and regression
Stat 1163 -correlation and regressionStat 1163 -correlation and regression
Stat 1163 -correlation and regression
Khulna University
 
Logistic regression vs. logistic classifier. History of the confusion and the...
Logistic regression vs. logistic classifier. History of the confusion and the...Logistic regression vs. logistic classifier. History of the confusion and the...
Logistic regression vs. logistic classifier. History of the confusion and the...
Adrian Olszewski
 
regression-130929093340-phpapp02 (1).pdf
regression-130929093340-phpapp02 (1).pdfregression-130929093340-phpapp02 (1).pdf
regression-130929093340-phpapp02 (1).pdf
MuhammadAftab89
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
Rupak Roy
 
Logistic regression (blyth 2006) (simplified)
Logistic regression (blyth 2006) (simplified)Logistic regression (blyth 2006) (simplified)
Logistic regression (blyth 2006) (simplified)
MikeBlyth
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
DrZahid Khan
 
Regression
RegressionRegression
Regression
Buddy Krishna
 
Regression analysis ppt
Regression analysis pptRegression analysis ppt
Regression analysis ppt
Elkana Rorio
 
Research Methodology-Chapter 14
Research Methodology-Chapter 14Research Methodology-Chapter 14
Research Methodology-Chapter 14
Javed Iqbal Kamyana
 
working with python
working with pythonworking with python
working with python
bhavesh lande
 
LINEAR REGRESSION.pptx
LINEAR REGRESSION.pptxLINEAR REGRESSION.pptx
LINEAR REGRESSION.pptx
neelamsanjeevkumar
 
Multiple Regression.ppt
Multiple Regression.pptMultiple Regression.ppt
Multiple Regression.ppt
TanyaWadhwani4
 
Forecasting
ForecastingForecasting
Forecasting
Nilanjan Bhaumik
 
Regression Analysis.pptx
Regression Analysis.pptxRegression Analysis.pptx
Regression Analysis.pptx
ShivankAggatwal
 
Logistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptxLogistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptx
harshasawa2003
 
Linear Regression
Linear Regression Linear Regression
Linear Regression
Rupak Roy
 
7. logistics regression using spss
7. logistics regression using spss7. logistics regression using spss
7. logistics regression using spss
Dr Nisha Arora
 
Supervised Learning.pdf
Supervised Learning.pdfSupervised Learning.pdf
Supervised Learning.pdf
gadissaassefa
 

Similar to Linear Regression and Logistic Regression in ML (20)

Logistic regression with SPSS examples
Logistic regression with SPSS examplesLogistic regression with SPSS examples
Logistic regression with SPSS examples
 
IBM401 Lecture 12
IBM401 Lecture 12IBM401 Lecture 12
IBM401 Lecture 12
 
Stat 1163 -correlation and regression
Stat 1163 -correlation and regressionStat 1163 -correlation and regression
Stat 1163 -correlation and regression
 
Logistic regression vs. logistic classifier. History of the confusion and the...
Logistic regression vs. logistic classifier. History of the confusion and the...Logistic regression vs. logistic classifier. History of the confusion and the...
Logistic regression vs. logistic classifier. History of the confusion and the...
 
regression-130929093340-phpapp02 (1).pdf
regression-130929093340-phpapp02 (1).pdfregression-130929093340-phpapp02 (1).pdf
regression-130929093340-phpapp02 (1).pdf
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Logistic regression (blyth 2006) (simplified)
Logistic regression (blyth 2006) (simplified)Logistic regression (blyth 2006) (simplified)
Logistic regression (blyth 2006) (simplified)
 
Logistic regression
Logistic regressionLogistic regression
Logistic regression
 
Regression
RegressionRegression
Regression
 
Regression analysis ppt
Regression analysis pptRegression analysis ppt
Regression analysis ppt
 
Research Methodology-Chapter 14
Research Methodology-Chapter 14Research Methodology-Chapter 14
Research Methodology-Chapter 14
 
working with python
working with pythonworking with python
working with python
 
LINEAR REGRESSION.pptx
LINEAR REGRESSION.pptxLINEAR REGRESSION.pptx
LINEAR REGRESSION.pptx
 
Multiple Regression.ppt
Multiple Regression.pptMultiple Regression.ppt
Multiple Regression.ppt
 
Forecasting
ForecastingForecasting
Forecasting
 
Regression Analysis.pptx
Regression Analysis.pptxRegression Analysis.pptx
Regression Analysis.pptx
 
Logistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptxLogistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptx
 
Linear Regression
Linear Regression Linear Regression
Linear Regression
 
7. logistics regression using spss
7. logistics regression using spss7. logistics regression using spss
7. logistics regression using spss
 
Supervised Learning.pdf
Supervised Learning.pdfSupervised Learning.pdf
Supervised Learning.pdf
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 

Linear Regression and Logistic Regression in ML

  • 1. Linear Regression and Logistic Regression Compiled by : Dr. Kumud Kundu
  • 2. Linear Regression • In statistical modeling, linear regression analysis is a set of statistical process that is utilized for estimating the relationships between a dependent variable and one or more independent variables. * (Wikipedia) • Or • It analyzes the influence of one or more independent variables on a dependent variable • Dependent variable — The attribute that is to be predicted • Independent variables — The factors under consideration which influence the prediction of dependent variable.
  • 4. Expenditure(₹) Income (₹) If the relationship between Y and X is believed to be linear, then the equation for a line may be appropriate: Y = β1 + β2X, where β1 is an intercept term and β2 is a slope coefficient.
  • 5. • y is termed as the dependent or study variable and X is termed as the independent or explanatory • variable. The terms are the parameters of the model. • These parameters are usually called as regression coefficients. • The unobservable error component accounts for the difference between the true and observed realization of y • For each point the differences between the predicted point and the actual observation is the residue • For simple linear regression we choose sum squared error (SSE) • S (predictedi – actuali)2 = S (residuei)2. Thus, find the line which minimizes the sum of the squared residues (e.g. least squares)
  • 6. Obtain Linear Regression for the following dataset Finding Learning parameters of Linear Regression line https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
  • 7. Obtain Linear Regression for Data https://news.vidyaacademy.ac.in/wp-content/uploads/2018/10/NotesOnMachineLearningForBTech-1.pdf
  • 8. Multiple Linear Regression 8 Y = 𝛽0 + 𝛽1 𝑋1 + 𝛽2 𝑋2 + ⋯ + 𝛽N 𝑋N + 𝜀
  • 11. Logistic Regression • Logistic regression is a form of regression analysis in which the dependent or outcome variable is binary or dichotomous. • It is the form of regression practically used for label classification rather than prediction of continuous value
  • 12. Logistic Regression? What is the “Logistic” component? • The term “Logistic” is taken from the Logit function that is used in this method of classification. The logit function is log of odds and is equivalent to Sigmoid function, which takes any real value between zero and one. Sigmoid function: What is the “Regression” component? Methods used to quantify association between an outcome and predictor variables. So, Logistic Regression is defined as technique which instead of modeling the outcome (Y) directly, models the log odds(Y) using the logistic function.     exp ( ) ln (1 ) 1 exp zp LOGIT p z p p z          
  • 13. Odds of an Event • Odds are used to describe the chance of an event occurring. • The odds are the ratios that compare the number of ways the event can occur with the number of ways the event cannot occur. • The odds of an event is the ratio of the probability of an event to the probability of its complement. • In other words, it is the ratio of favorable outcomes to unfavorable outcomes. • We say the odds are "3 to 2," which means 3 favorable outcomes to every 2 unfavorable outcomes, and we write 3 : 2.
  • 14. ( ) log (1 ) p LOGIT p z p    exp( ) 1 exp( ) z p z       exp ( ) ln (1 ) 1 exp zp LOGIT p z p p z           The Logistic Curve z (log odds) p(probability)
  • 16. The Logistic Regression Model Logistic Regression: ln P Y 1−P Y = 𝛽0 + 𝛽1 𝑋1 + 𝛽2 𝑋2 + ⋯ + 𝛽K 𝑋K Linear Regression:
  • 17. The Logistic Regression Model     0 1 1 2 2 K K P Y ln 1-P Y X X X               K Independent variables           YP1 YP ln is the log(odds) of the outcome. Binary outcome
  • 18. The Logistic Regression Model     0 1 1 2 2 K K P Y ln 1-P Y X X X               K           YP1 YP ln is the log(odds) of the outcome. Regression coefficients
  • 19. Form for Predicted Probabilities           0 1 1 2 2 K K 0 1 1 2 2 K K 0 1 1 2 2 K K P Y ln 1-P Y exp P Y 1 exp X X X X X X X X X                                  K c K K In this latter form, the logistic regression model directly relates the probability of Y to the predictor variables.
  • 20. Relationship between Odds & Probability             Probability event Odds event = 1-Probability event Odds event Probability event 1+Odds event 
  • 21. The Logistic Regression Model ln Pr Y 1 − Pr Y = 2.67 − 0.13 ∗ X1 ⇕ Pr Y = exp 2.67 − 0.13 ∗ X1 1 + exp 2.67 − 0.13 ∗ X1 What is the effect of X1 on Y? = exp −0.13 = 0.88 This implies that for every 1 unit increase in X1, the odds of Y decrease by 12%.
  • 22. Regression of Log Odds 22 Medication Dosage # Cured Total Patients Probability: # Cured/Total Patients Odds: p/(1-p) = # cured/ # not cured Log Odds: ln(Odds) 20 1 5 .20 .25 -1.39 30 2 6 .33 .50 -0.69 40 4 6 .67 2.0 0.69 50 6 7 .86 6.0 1.79 0 10 20 30 40 50 60 +2 -2 0 • y = .11x – 3.8 - Regression equation • We transform that log odds point to a probability: p = elogit(x)/(1+elogit(x)) • For example assume we want p for dosage = 10 Logit(10) = .11(10) – 3.8 = -2.7 p(10) = e-2.7/(1+e-2.7) = .06 prob. Cured 0 1
  • 23. Linear Regression Regression Model Implementation using sklearn from sklearn.linear_model import LinearRegression X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) linreg = LinearRegression() linreg.fit(X_train, y_train) linreg.predict(X_test)
  • 24. Logistic Regression Model Implementation using sklearn from sklearn.linear_model import LogisticRegression X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) logreg = LogisticRegression() logreg.fit(X_train, y_train) logreg.predict(X_test)
  • 25. Evaluation Metrics of Regression models
  • 26. from sklearn import metrics • print('Absolute Mean Error',metrics.mean_absolute_error(y_pred,y_test)) • print('Mean Squared Error', metrics.mean_squared_error(y_pred,y_test)) • print('Root Mean Square Error', metrics.r2_score(y_pred, y_test))
  • 27. Confusion Matrix A confusion matrix is a table that is often used to describe the performance of a classification model on a set of test data for which the true values are known. • True Positives (TP) - These are the correctly predicted positive values which means that the value of actual class is yes and the value of predicted class is also yes. • True Negatives (TN) - These are the correctly predicted negative values which means that the value of actual class is no and value of predicted class is also no. • False Positives (FP) Type 1 error– When actual class is no and predicted class is yes. • False Negatives (FN) Type 2 error– When actual class is yes but predicted class in no. https://blog.exsilio.com/all/accuracy-precision-recall-f1-score-interpretation-of-performance-measures/
  • 28. • Accuracy, Precision, and Recall • Accuracy is the proportion of true results among the total number of cases examined. • Accuracy = (TP+TN)/(TP+FP+FN+TN) • Accuracy is a valid choice of evaluation for classification problems which have no class imbalance. Evaluation Metrics of Classification models
  • 29. Recall (Senstivity) What proportion of Actual Positives is correctly classified? Recall = (TP)/(TP+FN) Any Recall value above 0.5 is considered as good. Precision Precision is the ratio of correctly predicted positive observations to the total predicted positive observations. High precision relates to the low false positive rate. Precision = TP/TP+FP F1 Score: It is the harmonic mean of precision and recall. The F1 score is a number between 0 and 1 Always aim to have a model with both good precision and recall.
  • 30. Compute Accuracy, Precision, Recall, F1-Score • The total outcome values are: • TP = 30, TN = 930, FP = 30, FN = 10 https://www.analyticsvidhya.com/blog/2020/04/confusion-matrix-machine-learning/
  • 31. • The accuracy for a model turns out to be: 96% https://www.analyticsvidhya.com/blog/2020/04/confusion-matrix-machine-learning/ 50% percent of the correctly predicted cases turned out to be positive cases. Whereas 75% of the positives were successfully predicted by the model.
  • 33. Calculate the F1 score for binary prediction problems using: from sklearn.metrics import f1_score y_true = [0, 1, 1, 0, 1, 1] y_pred = [0, 0, 1, 0, 0, 1] f1_score(y_true, y_pred)

Editor's Notes

  1. Note target really is the point being regressed to each time. Now makes better sense since net is the output and we want to minimize error of the target.
  2. yellow point is a generalization example