SlideShare a Scribd company logo
1 of 34
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

Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine LearningKuppusamy P
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reductionmrizwan969
 
Machine Learning With Logistic Regression
Machine Learning  With Logistic RegressionMachine Learning  With Logistic Regression
Machine Learning With Logistic RegressionKnoldus Inc.
 
Principal component analysis and lda
Principal component analysis and ldaPrincipal component analysis and lda
Principal component analysis and ldaSuresh Pokharel
 
Feature Extraction
Feature ExtractionFeature Extraction
Feature Extractionskylian
 
Linear regression in machine learning
Linear regression in machine learningLinear regression in machine learning
Linear regression in machine learningShajun Nisha
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality ReductionSaad Elbeleidy
 
Classification and Regression
Classification and RegressionClassification and Regression
Classification and RegressionMegha Sharma
 
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | Edureka
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | EdurekaSVM Algorithm Explained | Support Vector Machine Tutorial Using R | Edureka
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | EdurekaEdureka!
 
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!
 
Principal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesPrincipal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesAbhishekKumar4995
 
NAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERNAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERKnoldus Inc.
 
Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.Rohit Kumar
 
Understanding Bagging and Boosting
Understanding Bagging and BoostingUnderstanding Bagging and Boosting
Understanding Bagging and BoostingMohit Rajput
 

What's hot (20)

Machine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-offMachine Learning: Bias and Variance Trade-off
Machine Learning: Bias and Variance Trade-off
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reduction
 
Machine Learning With Logistic Regression
Machine Learning  With Logistic RegressionMachine Learning  With Logistic Regression
Machine Learning With Logistic Regression
 
Principal component analysis and lda
Principal component analysis and ldaPrincipal component analysis and lda
Principal component analysis and lda
 
Feature Extraction
Feature ExtractionFeature Extraction
Feature Extraction
 
Linear regression in machine learning
Linear regression in machine learningLinear regression in machine learning
Linear regression in machine learning
 
Dimensionality Reduction
Dimensionality ReductionDimensionality Reduction
Dimensionality Reduction
 
Classification and Regression
Classification and RegressionClassification and Regression
Classification and Regression
 
Supervised Machine Learning
Supervised Machine LearningSupervised Machine Learning
Supervised Machine Learning
 
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | Edureka
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | EdurekaSVM Algorithm Explained | Support Vector Machine Tutorial Using R | Edureka
SVM Algorithm Explained | Support Vector Machine Tutorial Using R | Edureka
 
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...
 
Principal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesPrincipal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT Slides
 
Principal Component Analysis
Principal Component AnalysisPrincipal Component Analysis
Principal Component Analysis
 
NAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIERNAIVE BAYES CLASSIFIER
NAIVE BAYES CLASSIFIER
 
Presentation on K-Means Clustering
Presentation on K-Means ClusteringPresentation on K-Means Clustering
Presentation on K-Means Clustering
 
Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.Pattern recognition and Machine Learning.
Pattern recognition and Machine Learning.
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Clustering
ClusteringClustering
Clustering
 
Understanding Bagging and Boosting
Understanding Bagging and BoostingUnderstanding Bagging and Boosting
Understanding Bagging and Boosting
 

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 examplesGaurav Kamboj
 
IBM401 Lecture 12
IBM401 Lecture 12IBM401 Lecture 12
IBM401 Lecture 12saark
 
Stat 1163 -correlation and regression
Stat 1163 -correlation and regressionStat 1163 -correlation and regression
Stat 1163 -correlation and regressionKhulna 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).pdfMuhammadAftab89
 
Logistic regression
Logistic regressionLogistic regression
Logistic regressionRupak 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 regressionDrZahid Khan
 
Regression analysis ppt
Regression analysis pptRegression analysis ppt
Regression analysis pptElkana Rorio
 
Multiple Regression.ppt
Multiple Regression.pptMultiple Regression.ppt
Multiple Regression.pptTanyaWadhwani4
 
Regression Analysis.pptx
Regression Analysis.pptxRegression Analysis.pptx
Regression Analysis.pptxShivankAggatwal
 
Logistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptxLogistic Regression power point presentation.pptx
Logistic Regression power point presentation.pptxharshasawa2003
 
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 spssDr Nisha Arora
 

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
 
Linear regression theory
Linear regression theoryLinear regression theory
Linear regression theory
 
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
 

Recently uploaded

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

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