Logistic Regression
Logistic Regression
Logistic Regression is a simple yet powerful supervised machine learning algorithm mainly used for binary classification problems.
It predicts the probability of an outcome (such as success/failure or yes/no) based on input features.
It is named ‘Logistic Regression’ because its underlying technique is quite the same as Linear Regression. The term “Logistic” is taken from the Logit function that is used in this method of classification.
The logistic function, also called the sigmoid function was developed by statisticians to describe properties of population growth in ecology, rising quickly and maxing out at the carrying capacity of the environment. It’s an S-shaped curve that can take any real-valued number and map it into a value between 0 and 1, but never exactly at those limits.
๐ข Sigmoid (Logistic) Function
This is the heart of Logistic Regression:
๐น What it does:
- Takes any real number → converts it into a value between 0 and 1
- Produces an S-shaped curve
๐น Interpretation:
- Output ≈ 0 → Class 0 (Negative)
- Output ≈ 1 → Class 1 (Positive)
๐ The Core Idea of Logistic Regression
Logistic Regression:
- First applies a linear model
- Then passes the result through a sigmoid (logistic) function
๐งฎ Mathematical Model ( Binary )
Step 1: Linear Combination
- = intercept
- = coefficient
- = input feature
Step 2: Apply Sigmoid Function
๐ This gives a probability value
๐ฏ Decision Boundary
-
A threshold (usually 0.5) is used:
- If probability ≥ 0.5 → Class 1
- If probability < 0.5 → Class 0
๐ Example:
- 0.8 → “Yes”
- 0.3 → “No”
⚖️ Why Logistic Regression Handles Outliers Better
- Linear regression tries to fit a straight line → heavily affected by outliers
-
Logistic regression:
- Uses sigmoid curve
- Output is bounded (0–1)
- Extreme values get compressed
๐ So outliers don’t distort predictions as much
๐ง How Training Works in Logistic Regression
Training a logistic regression model means:
Finding the best parameters (weights) so that predicted probabilities match the actual class labels as closely as possible.
๐ Step-by-Step Training Process
1. Start with the Model
We compute a linear combination of inputs:
Then apply the sigmoid function:
- = predicted probability
- Output is always between 0 and 1
❗ Why Not Use Mean Squared Error (MSE)?
In linear regression, we use MSE. But in logistic regression:
- The sigmoid function makes the model non-linear
- MSE leads to a non-convex loss function
- This makes optimization difficult (multiple local minima)
๐ So instead, we use Log Loss (Cross-Entropy Loss)
๐ Cost Function in Logistic Regression
๐น Binary Cross-Entropy (Log Loss)
๐ Understanding the Formula
Terms:
- = number of training examples
- = actual label (0 or 1)
- = predicted probability
๐ Intuition Behind Log Loss
Case 1: When
- If → loss = 0 ✅ (perfect)
- If is small → loss becomes very large ❌
Case 2: When
- If
- If is close to 1 → large penalty ❌
๐ก Key Insight
Log Loss:
- Heavily penalizes wrong confident predictions
- Encourages the model to output accurate probabilities
⚙️ How the Model Learns (Optimization)
Goal:
Minimize the cost function
Method:
๐ Gradient Descent
๐ Gradient Descent Update Rule
- = model parameters
- = learning rate
- = gradient (direction of steepest increase)
๐ We move in the opposite direction to reduce error
๐ Training Loop
- Initialize weights randomly
- Compute predictions using sigmoid
- Calculate log loss
- Compute gradients
- Update weights
- Repeat until convergence
๐ Real-Life Examples
- ๐ง Spam detection → Spam / Not Spam
- ๐ฅ Tumor detection → Malignant / Benign
- ๐ณ Fraud detection → Fraud / Genuine
๐ Advantages
✔ Simple and easy to implement
✔ Works well for linearly separable data
✔ Outputs probabilities (useful for decision-making)
✔ Fast and efficient
⚠️ Limitations
❌ Cannot capture complex non-linear relationships
❌ Assumes linear relationship between input and log-odds
❌ Sensitive to irrelevant features
๐ Summary
⚖️ Key Differences
| Feature | Linear Regression | Sigmoid (Logistic) |
|---|---|---|
| Shape | Straight line | S-shaped curve |
| Output Range | −∞ to +∞ | 0 to 1 |
| Use Case | Regression | Classification |
| Handles Probability | ❌ No | ✅ Yes |
Appendix :Deriving the gradient descent update rule
๐น Cost Function (Log Loss)
- Penalizes wrong predictions heavily
- Ensures convex optimization
๐น Gradient Descent Update Rule
Where:
- ฮฑ = learning rate
- m= number of samples
Look at ONE training example
Instead of the full sum, take just one data point:
where:
Think of it like layers (very important)
We don’t differentiate everything at once.
We go step by step:
So we use chain rule:
Compute each part (simple pieces)
✅ 1. Derivative of cost w.r.t h
✅ 2. Derivative of sigmoid
✅ 3. Derivative of z
Multiply them
Now multiply all three:
Simplification
The cancels:
For all data points
Average over all samples:




Comments
Post a Comment