Machine Learning Foundations · Chapter 4 · ~15 min · Intermediate
Logistic Regression & Binary Classification
Transition from predicting continuous numbers to calculating probabilities. Master the Sigmoid activation function, derive Binary Cross-Entropy (Log Loss), and understand why tuning your classification threshold is vital for high-stakes decisions.
- Master the Sigmoid activation function squashing real numbers to (0, 1)
- Understand the log-odds (logit) formulation behind linear classification
- Derive Binary Cross-Entropy loss and why it guarantees convexity
- Tune operational decision thresholds to balance Precision vs Recall
Logistic Regression & Binary Classification: learn it visually
Transition from predicting continuous numbers to calculating probabilities. Master the Sigmoid activation function, derive Binary Cross-Entropy (Log Loss), and understand why tuning your classification threshold is vital for high-stakes decisions.
1The Sigmoid Squashing Function & Odds
While Linear Regression outputs any continuous value from -∞ to +∞, classification requires an output that represents a valid probability bounded between 0 and 1.
Logistic Regression solves this by running a linear score z = w · x + b through the Sigmoid function:
When z = 0, σ(0) = 0.5. When z → +∞, σ(z) → 1.0. When z → -∞, σ(z) → 0.0.
Wrong idea: Fitting a straight line to discrete 0/1 labels
Using standard linear regression (y = wx + b) produces outputs like -0.4 or +1.8, which are nonsensical as probabilities.
Real system: Squashing continuous logits through the Sigmoid curve
The logistic sigmoid function σ(z) = 1 / (1 + e^-z) maps any real number from negative infinity to positive infinity cleanly into (0, 1).
What is the output of the Sigmoid function when the linear combination z = w^T x + b equals exactly 0?
2Binary Cross-Entropy (Log Loss)
Why can't we use Mean Squared Error with Sigmoid? Because the S-curve saturates when predictions approach 0 or 1, flattening the gradient and creating a non-convex landscape littered with local traps.
Instead, we use Binary Cross-Entropy (Log Loss):
J(w, b) = - (1 / m) · ∑i=1m [ yi · log(ŷi) + (1 - yi) · log(1 - ŷi) ]
- If true label
y = 1: Loss is-log(ŷ). If ŷ = 1, loss is 0. If ŷ = 0.001, loss approaches infinity! - If true label
y = 0: Loss is-log(1 - ŷ). Confident false alarms are penalized exponentially.
If a sample has ground truth y = 1, but the model predicts a probability of y_pred = 0.0001, what happens to the Binary Cross-Entropy loss?
3Decision Threshold Tuning: Cost-Sensitive ML
Machine learning libraries default to a decision threshold of θ = 0.5: if P(y=1) ≥ 0.5, predict Positive; otherwise Negative.
In production systems, errors are rarely symmetrical:
Cancer Screening & Fraud Detection
Missing a malignant tumor (False Negative) is fatal. Calling a healthy patient in for a second test (False Positive) is minor. Lower the threshold to 0.15–0.30!
Spam Email & Account Bans
Moving a crucial tax document or client contract to Spam (False Positive) is terrible. Letting an occasional spam email through (False Negative) is tolerable. Raise the threshold to 0.70–0.85!
4Hands-On: Sigmoid S-Curve & Asymmetric Thresholds
Interact with the tumor classification dataset below. Adjust the Decision Threshold (θ) slider and watch how False Negatives vanish as you increase sensitivity:
Logistic Sigmoid S-Curve & Decision Threshold Tuner
5Bug Hunt: Fix Broken Logistic Regression Pipeline
A junior developer tried to implement logistic regression from scratch but forgot to apply the sigmoid activation function on raw linear logits and mistakenly computed linear regression gradients.
Repair the training function so it computes valid bounded probabilities in (0, 1):
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
- Apply the sigmoid function:
y_pred = 1 / (1 + np.exp(-z)). - With
y_predproperly activated,error = y_pred - yyields the exact Binary Cross-Entropy gradient. - Ensure the returned model predicts calibrated probabilities rather than unbounded linear numbers.
6Chapter Knowledge Challenge
Validate your understanding of logistic regression, log-odds, and decision boundaries:
What is the geometric shape of the decision boundary formed by Logistic Regression in a multi-dimensional feature space?
In medical diagnostic imaging, if you want to ensure almost zero sick patients are sent home undiagnosed, how should you adjust the decision threshold?
7Recap
- Logistic Regression models the probability of class membership by squashing linear logits through the Sigmoid function:
σ(z) = 1 / (1 + e-z). - Binary Cross-Entropy (Log Loss) forms a provably convex loss surface that punishes confident wrong predictions logarithmically.
- The Decision Boundary is the linear hyperplane where
wTx + b = 0, corresponding toP(y=1) = 0.5. - Threshold Tuning allows tailoring the model to real-world asymmetric costs, trading Precision for Recall in safety-critical domains.
Ready for Chapter 5? Mark this chapter as complete to proceed: