LESSON 01
Machine
Learning
CLASSIFICATION
SERIES
Logistic
Regression
Basics of Classification
Understanding how machines learn to categorize
data into discrete classes using probabilities
Introduction to Machine Learning | Logistic Regression
What We'll Cover Today
01
What is Classification?
Binary vs multi-class, real-world examples
02
Linear vs Logistic Regression
Key differences & why linear fails for classification
03
The Sigmoid Function
Mapping outputs to probabilities [0, 1]
04
Decision Boundary
How the model separates classes
05
Cost Function & Training
Log-loss and gradient descent basics
06
Evaluation Metrics
Accuracy, precision, recall, confusion matrix
What is Classification?
Classification: Predicting which category (class) a data point belongs to.
Binary Classification Examples
๐ง
Spam
Not Spam
Output: 0 or 1
๐ฅ
Diagnosis
Disease
Healthy
Output: 0 or 1
๐ณ
Transaction
Fraud
Legit
Output: 0 or 1
โ
Loan
Approved
Denied
Output: 0 or 1
๐ก Key Insight: Unlike regression (predicting a number), classification predicts a discrete label.
Linear vs Logistic Regression
Linear Regression
Logistic Regression
Output Type
Any real number (โโ to +โ)
Probability (0 to 1)
Goal
Predict continuous values
Predict class probability
Output Function
y = mx + b (linear)
y = ฯ(mx + b) (sigmoid)
Example Use
Predict house price ($)
Predict spam? (yes/no)
Decision
No threshold needed
Threshold at 0.5 โ class
Loss Function
Mean Squared Error (MSE)
Binary Cross-Entropy
โ ๏ธ Why linear fails for classification: It can predict values outside [0,1], which can't represent probabilities!
The Sigmoid Function
ฯ(z) =
1
1 + e
-z
where z = wx + b
Key Properties:
Probability โ Class
P โฅ 0.5 โ Class 1 โ
P < 0.5 โ Class 0 โ
Threshold can be adjusted
based on use case
Decision Boundary
Feature Space Visualization
Feature Xโ โ
Feature Xโ
Decision
Boundary
ฯ(z) = 0.5
Class 0 (Negative)
Class 1 (Positive)
How It Works
1
Model learns weights
(w) and bias (b)
2
Computes z = wยทx + b
for each data point
3
Applies sigmoid:
P = ฯ(z)
4
If P โฅ 0.5 โ Class 1
If P < 0.5 โ Class 0
Cost Function & Training
โ Why not use MSE (Mean Squared Error)? With sigmoid, MSE creates a non-convex loss โ prone to many local minima!
Binary Cross-Entropy (Log Loss):
J(w,b) = โ(1/m) ฮฃ [ yยทlog(ลท) + (1โy)ยทlog(1โลท) ]
y = 1 (positive class)
Loss = โlog(ลท)
Push ลท โ 1 to minimize loss
y = 0 (negative class)
Loss = โlog(1โลท)
Push ลท โ 0 to minimize loss
Gradient Descent
w := w โ ฮฑ ยท โJ/โw
Iteratively find optimal weights
Evaluation Metrics
Confusion Matrix
Predicted โ
Predicted 0
Predicted 1
Actual โ
Actual 0
Actual 1
TN
True Neg
FP
False Pos
FN
False Neg
TP
True Pos
Key Metrics
Accuracy
(TP + TN) / Total
Overall correctness
Precision
TP / (TP + FP)
Of predicted positives, how many are correct
Recall
TP / (TP + FN)
Of actual positives, how many detected
F1-Score
2 ร (P ร R) / (P + R)
Harmonic mean of precision & recall
Visual Comparison: Linear vs Logistic
โ Linear Regression for Classification
Problem: values go outside [0,1]!
โ Logistic Regression for Classification
Always stays between 0 and 1 โ
Key Takeaways
Logistic Regression โ Lecture 05 Summary
๐ฏ
#01
Logistic regression predicts class probabilities (0 to 1), not continuous values.
ใฝ๏ธ
#02
The sigmoid function maps any real number to a probability between 0 and 1.
โ ๏ธ
#03
Linear regression fails for classification โ it can output values outside [0, 1].
๐
#04
Decision boundary at threshold 0.5 separates the two classes in feature space.
๐
#05
Binary cross-entropy (log loss) is the correct loss function for logistic regression.
๐
#06
Accuracy, precision, recall & F1-score are used to evaluate classification models.
Next Lesson โ Multiclass Classification & Softmax | Practice: Implement logistic regression from scratch!