Logistic Regression - Byimran
Logistic Regression - Byimran
Regression
Classification
Machine Learning
Lecture Slides by Andrew Ng
Classification
Yes | No
True | False
Binary Classification ⇒
1 | 0
Positive | Negative
Andrew Ng
Let’s make an email spam filter
From: [email protected]
Date: February 13, 2023
𝒙 Model 𝑦 ∈ {1, 0} Subject: URGENT!!!
Hello Dear,
- The training dataset is, I am a Nigerian prince, and I
have a business proposal for you
Partial ...
𝒟𝑡𝑟𝑎𝑖𝑛 = [("… CS−370 …", −1),
("… 10 million USD …", +1), Specifications
("… PVC pipes at redued …", +1)] of behaviour
In machine learning, input features are hand-crafted
Length>10 : True 1
fracOfAlphabets : 0.85 0.85
𝑎𝑏𝑐@𝑔𝑚𝑎𝑖𝑙. 𝑐𝑜𝑚 Feature
Extractor Contains_@ : True 1
endsWith_.com : True 1
endsWith_.edu : False 0
𝝓 𝑥 = [𝜙1 𝑥 , 𝜙2 𝑥 , … , 𝜙𝑑 (𝑥)]
04/18
A linear classifier calculates scores to predict classes
05/18
Relationship between data and weights can be visualised on 2D plan
- Let we have 𝑦
𝑓𝑤 𝑥 = 𝑠𝑖𝑔𝑛 𝒘. 𝜙 𝑥
5
𝑤 = 2, −1
4.5
3.5
2.5
1.5
0.5
06/18
Relationship between data and weights can be visualised on 2D plan
- Let we have 𝑦
𝑓𝑤 𝑥 = 𝑠𝑖𝑔𝑛 𝒘. 𝜙 𝑥
5
𝑤 = 2, −1
4.5
3.5
2.5
1.5
0.5
06/18
Relationship between data and weights can be visualised on 2D plan
- Let we have 𝑦
𝑓𝑤 𝑥 = 𝑠𝑖𝑔𝑛 𝒘. 𝜙 𝑥
5
𝑤 = 2, −1
4.5
3.5
2.5
2
−
1.5
0.5
+
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 𝑥
06/18
Relationship between data and weights can be visualised on 2D plan
- Let we have 𝑦
𝑓𝑤 𝑥 = 𝑠𝑖𝑔𝑛 𝒘. 𝜙 𝑥
5
𝑤 = 2, −1
4.5
3.5
2.5
2
−
1.5
0.5
+
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 𝑥
06/18
Relationship between data and weights can be visualised on 2D plan
- Let we have 𝑦
𝑓𝑤 𝑥 = 𝑠𝑖𝑔𝑛 𝒘. 𝜙 𝑥
5
𝑤 = 2, −1
4.5
3.5
2
−
- If 𝒙 ∈ ℝ2 : The hyperplane is a line 1.5
1
- If 𝒙 ∈ ℝ3 : The hyperplane is a plane 0.5
+
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 𝑥
06/18
(Yes)
1
Malignant ?
(No)
0 Tumor Size Tumor Size
Threshold will
change because of
this new point
Threshold classifier output at 0.5:
If , predict “y = 1”
If , predict “y = 0”
= fw,b (x)
Classification: y = 0 or 1
Logistic Regression:
Andrew Ng
Logistic
Regression
Hypothesis
Representation
Machine Learning
Logistic Regression Model
Want
Linear regression:
1
Sigmoid function
Logistic function
Andrew Ng
Interpretation of Hypothesis Output
= estimated probability that y = 1 on input x
Example: If
Machine Learning
Sigmoid function
Logistic regression 1
z
z = 100, z = -100,
e-z = e-100 (v small) e-z = e100 (v big)
g(z) ~ 1 g(z) ~ 0
predict “ or z > 0 z = 0,
“ if
e-z = e-0 = 1
predict “ “ if or z < 0 g(z) ~ 1/2 = 0.5
Andrew Ng
Decision Boundary
x2
-3 1 1
y=1
3
2
y=0 1
1 2 3 x1 Predict “ “ if
⇒ decision boundary
-3 + x1 + x2 = 0
x1 + x2 = 3
Andrew Ng
Non-linear decision boundaries
x2 -1 1 1
-1 1 x1 Predict “ ”if
-1
x2
x1
Andrew Ng
Logistic
Regression
Cost function
Machine Learning
Training set:
m examples
“convex” “non-convex”
Andrew Ng
Logistic regression cost function -log(1) = 0
-log(0) = ∞
If y = 1
cost
0 1
Andrew Ng
Logistic regression cost function -log(1) = 0
-log(0) = ∞
If y = 0
cost
0 1 Andrew Ng
Logistic
Regression
Simplified cost function
and gradient descent
Machine Learning
Logistic regression cost function
To fit parameters :
Want :
Repeat
Andrew Ng
Gradient Descent
Want :
Repeat
Andrew Ng
Logistic
Regression
Multi-class classification:
One-vs-all
Machine Learning
Multiclass classification
Email foldering/tagging: Work, Friends, Family, Hobby
Andrew Ng
Binary classification: Multi-class classification:
x2 x2
x1 x1
Andrew Ng
x2
One-vs-all (one-vs-rest):
x1
x2 x2
x1 x1
x2
Class 1:
Class 2:
Class 3:
x1
Andrew Ng
One-vs-all
Andrew Ng
Aspect Linear Regression Logistic Regression
Regression Classification
Purpose
(predicting continuous value) (predicting categories)