Logistic Regression

Intuition

To predict a probability, squash a linear score into (0, 1) with the S-shaped sigmoid. Train by rewarding confident correct predictions and heavily penalising confident wrong ones.

Precise explanation

p=σ(z)=11+e−zp = \sigma(z) = \frac{1}{1+e^{-z}}, z=Xw+bz = Xw + b. Log-loss (binary cross-entropy):

L=−1n∑[ylog⁡p+(1−y)log⁡(1−p)].L = -\frac1n\sum \big[y\log p + (1-y)\log(1-p)\big].

Its gradient is remarkably simple: ∇wL=1nX⊤(p−y)\nabla_w L = \frac1n X^\top (p - y), ∂L/∂b=1n∑(p−y)\partial L/\partial b = \frac1n\sum (p-y).

Numerical stability: for z=−1000z = -1000, np.exp(1000) overflows. A stable sigmoid uses e−∣z∣e^{-|z|}: for z≥0z\ge0, 1/(1+e−z)1/(1+e^{-z}); for z<0z<0, ez/(1+ez)e^{z}/(1+e^{z}). Clip pp into [ϵ,1−ϵ][\epsilon, 1-\epsilon] before taking logs (ε = 1e−12) so log⁡0\log 0 never occurs.

Predict class 1 when p≥0.5p \ge 0.5 (threshold adjustable). The decision boundary Xw+b=0Xw + b = 0 is linear.

Worked example

w = [2], b = −1, x = 1: z = 1, p = 0.731; if y = 1 the loss is −log 0.731 = 0.313.

Common mistakes

  • Using MSE for classification (non-convex with sigmoid, slow learning).
  • Overflow warnings from a naive sigmoid.
  • log(0) → −inf loss.

Why this matters

Logistic regression is the standard baseline classifier and is exactly a one-neuron network with sigmoid output — the bridge to Deep Learning.

Library implementation

sklearn.linear_model.LogisticRegression fits the same model with a second-order solver (L-BFGS) instead of fixed-step gradient descent, and adds L2 regularisation by default (strength 1/C). With a very large C the regularisation vanishes, and after enough epochs your gradient descent reaches nearly the same probabilities. The comparison runs when scikit-learn is installed.

References