Professional Documents
Culture Documents
Introduction to Classification
Decision boundary
Confusion Matrix
Cost Function
Logistic Regression Example - Python
Yes “1”
Threshold
“0.5”
No “0”
Tumor size x
If fw,b(x) < 0.5 → y = 0 “Logistic Regression”
If fw,b(x) ≥ 0.5 → y = 1
DR. HAIDER ALI AI and Internet of Things 5
EXAMPLE Decision Want outputs between 0 and 1
boundary
Yes “1”
Threshold
“0.7”
No “0” z
Tumor size x
Sigmoid function
1 Logistic function
𝑔(𝑧) = −𝑧
0<𝑔(𝑧)<1 Output between 0 and 1
1+𝑒
DR. HAIDER ALI AI and Internet of Things 6
Want outputs between 0 and 1
𝑓𝑤,𝑏
ഥ (𝑥)
ҧ
𝑧 = 𝑤.
ഥ 𝑥ҧ + 𝑏
1
𝑔(𝑧) =
1 + 𝑒 −𝑧
1
𝑓𝑤,𝑏
ഥ 𝑥ҧ = 𝑔(𝑤.
ഥ 𝑥ҧ + 𝑏) = ҧ
ഥ 𝑥+𝑏)
z
1 + 𝑒 −(𝑤.
1 Logistic Regression
Sigmoid function
𝑔(𝑧) = −𝑧 0<𝑔(𝑧)<1 Logistic function
1+𝑒
DR. HAIDER ALI AI and Internet of Things 7
INTERPRETATION OF LOGISTIC REGRESSION OUTPUT
1
𝑓𝑤,𝑏
ഥ 𝑥ҧ = −( ഥ
𝑤. ҧ
𝑥+𝑏) 𝑓𝑤,𝑏
ഥ 𝑥ҧ = 𝑃(𝑦 = 1|𝑥;ҧ 𝑤,
ഥ 𝑏)
1+𝑒
“probability” that class is 1 Probability that y is 1,
given input 𝑥,ҧ parameters 𝑤,
ഥ 𝑏
Example
x is tumor size
y is 0 (tumor is not malignant) 𝑃 𝑦 =0 +𝑃 𝑦 =1 =1
y is 1 (tumor is malignant)
𝑓𝑤,𝑏
ഥ 𝑥ҧ = 0.7
70% chance that y is 1
DR. HAIDER ALI AI and Internet of Things 8
PLOT OF LOGISTIC FUNCTION - PYTHON
𝑓𝑤,𝑏
ഥ 𝑥ҧ = 𝑃(𝑦 = 1|𝑥;ҧ 𝑤,
ഥ 𝑏)
Is 𝑓𝑤,𝑏
ഥ 𝑥ҧ > 0.5? (threshold)
Yes: 𝑦 = 1 No: 𝑦 = 0
When is 𝑓𝑤,𝑏
ഥ 𝑥ҧ > 0.5?
𝑔 𝑧 ≥ 0.5
𝑧≥0
𝑤.
ഥ 𝑥ҧ + 𝑏 ≥ 0 𝑤.
ഥ 𝑥ҧ + 𝑏 < 0
𝑦=1
1 2 3 x1
DR. HAIDER ALI AI and Internet of Things 11
NON-LINEAR DECISION-BOUNDARY
x2
𝑓𝑤,𝑏
ഥ 𝑥ҧ = 𝑔(𝑧) = 𝑔(𝑤1 𝑥12 + 𝑤2 𝑥22 + 𝑏)
1
𝑧 = 𝑥12 + 𝑥22 − 1
x1
-1 1 𝑥12 + 𝑥22 = 1
-1
Actual
Positive Negative
Negative Positive
Ture False
Predicated
Positive Positive
False Ture
Negative Negative
Predicated
Positive Ture Positive (TP) False Positive(FP)
False Negative
Negative Ture Negative (TN)
(FN)
PRECISION
ACCURACY
𝑇𝑃 + 𝑇𝑁 𝑇𝑃
𝐴𝑐𝑐𝑢𝑟𝑎𝑐𝑦 = Precision =
𝑇𝑃 + 𝑇𝑁 + 𝐹𝑁 + 𝐹𝑃 𝑇𝑃 + 𝐹𝑃
DR. HAIDER ALI AI and Internet of Things 14
COST FUNCTION
j = 1,…,n Features
X1 Xn y Target y is 0 or 1
i=1
1
𝑓𝑤,𝑏
ഥ 𝑥ҧ = ҧ
ഥ 𝑥+𝑏)
1 + 𝑒 −(𝑤.
i=m
How to choose 𝑤
ഥ = [𝑤1 𝑤2 … 𝑤𝑛 ] and b?
Loss
log(f)
-log(f)
Loss → 0
0 then Loss → ∞
DR. HAIDER ALI AI and Internet of Things 18
COST FUNCTION