Chapter 4 of 13
Course Progress0 of 13 completed (0%)

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.

Logistic Regression & Binary ClassificationTransition 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.UNDERSTANDLogistic RegressionFOLLOW THE FLOWBinaryClassificationAPPLY ITTransitionpredictingcontinuousLogistic Regression & Binary ClassificationTransition 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.UNDERSTANDLogistic RegressionFOLLOW THE FLOWBinaryClassificationAPPLY ITTransitionpredictingcontinuous

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:

σ(z) = 1 / (1 + e-z)

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

y_pred = 2.45 → Impossible probability > 100%Linear models cannot bound outputs to the valid [0, 1] probability range

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

σ(z) ∈ (0, 1) · P(y=1|x) = 1 / (1 + e^-z)Mathematically rigorous, continuous probability output

The logistic sigmoid function σ(z) = 1 / (1 + e^-z) maps any real number from negative infinity to positive infinity cleanly into (0, 1).

Concept Check

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.
Concept Check

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:

High Cost of False Negative

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!

High Cost of False Positive

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

Threshold: 0.50
Threshold θ = 0.50Feature X (Tumor Size in cm)P(Malignant | x)
True Positives (TP):4
False Negatives (FN):1 ⚠️
Recall Sensitivity:80%
Precision:80%
0.50
0.10 (High Sensitivity / Catch All)0.50 (Default)0.90 (High Precision / Strict)
2.0
3.0 cm

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):

Python Bug Hunt · Logistic Classifier Activation
Python

🎯 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_pred properly activated, error = y_pred - y yields 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:

Concept Check 1 of 2
1

What is the geometric shape of the decision boundary formed by Logistic Regression in a multi-dimensional feature space?

Concept Check 2 of 2
2

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 to P(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:

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us