Skip to content
Lambdian

Logistic regression

The workhorse classifier: a linear score turned into a calibrated probability by the sigmoid.

Animated explanation

The idea

To classify, we want a probability: how likely is this email spam, this loan to default? Linear regression's output can be any number, so we feed it through the sigmoid σ(z)=1/(1+e−z)\sigma(z) = 1/(1+e^{-z}), an S-curve that squeezes (−∞,∞)(-\infty, \infty) into (0,1)(0, 1).

The model is still a weighted sum of features, so the decision boundary (p=0.5p = 0.5, i.e. w⊤x+b=0\mathbf{w}^\top\mathbf{x}+b = 0) is a straight line, plane or hyperplane. What is learned is the direction and position of that boundary, and how sharply probability changes across it.

Fitting by maximum likelihood

Treat each label as a coin flip with probability pi=σ(w⊤xi+b)p_i = \sigma(\mathbf{w}^\top\mathbf{x}_i + b). The likelihood of all labels is ∏ipiyi(1−pi)1−yi\prod_i p_i^{y_i}(1-p_i)^{1-y_i}. Maximising it is the same as minimising the log-loss (binary cross-entropy).

There is no closed form, but the loss is convex, so gradient descent (or Newton's method, which scikit-learn's solvers use) reliably finds the single best w\mathbf{w}. The gradient is X⊤(p−y)/nX^\top(\mathbf{p}-\mathbf{y})/n: the same shape as linear regression's, with predicted probabilities in place of predictions.

Reading the weights

Logistic regression is linear in the log-odds: log⁡p1−p=w⊤x+b\log\frac{p}{1-p} = \mathbf{w}^\top\mathbf{x} + b. Increasing xjx_j by one multiplies the odds by ewje^{w_j}. A weight of 0.69 doubles the odds. That interpretability is why it remains the default in medicine, credit scoring and A/B analysis.

Regularisation and separable data

If the classes can be perfectly separated by a line, the likelihood keeps improving as ∥w∥→∞\lVert\mathbf{w}\rVert \to \infty (ever sharper steps): unregularised weights blow up. In practice always add an L2 (or L1) penalty. scikit-learn does this by default, with C=1/λC = 1/\lambda.

The algorithm, step by step

  1. 1
    Standardise features; add a bias term.
  2. 2
    Initialise w=0\mathbf{w} = 0.
  3. 3
    Repeat: p=σ(Xw)\mathbf{p} = \sigma(X\mathbf{w}), then w←w−η(1nX⊤(p−y)+λw)\mathbf{w} \leftarrow \mathbf{w} - \eta\big(\frac1n X^\top(\mathbf{p}-\mathbf{y}) + \lambda\mathbf{w}\big).
  4. 4
    Predict class 1 when p≥p \ge threshold (0.5 by default; tune it for your costs).

The maths

Model

p(y=1∣x)=σ(w⊤x+b)p(y=1\mid\mathbf{x}) = \sigma(\mathbf{w}^\top\mathbf{x}+b)

Log-loss

J(w)=−1n∑i=1n[yilog⁡pi+(1−yi)log⁡(1−pi)]J(\mathbf{w}) = -\frac1n\sum_{i=1}^n\big[y_i\log p_i + (1-y_i)\log(1-p_i)\big]

Gradient

∇J=1nX⊤(p−y)\nabla J = \frac1n X^\top(\mathbf{p}-\mathbf{y})

Sigmoid derivative

σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z)\big(1-\sigma(z)\big)

Strengths and weaknesses

Strengths

  • Outputs probabilities, usually well calibrated
  • Interpretable: odds ratios per feature
  • Convex training, fast, scales to huge sparse data
  • Strong baseline for text and tabular data

Weaknesses

  • Linear boundary only (unless you engineer features)
  • Weights diverge on separable data without regularisation
  • Sensitive to correlated features and outliers in x

Reach for it when…

You need a fast, explainable classifier with probabilities, especially as a first baseline.

Check your understanding

Question 1

Q1What shape is the decision boundary of logistic regression on raw features?

Question 2

Q2Weight wj=ln⁡2≈0.69w_j = \ln 2 \approx 0.69. Increasing xjx_j by one unit…

Question 3

Q3Your data is perfectly separable and training never converges. Why?

Lambdian Lab

Logistic regression by gradient descent

Train a binary classifier from scratch.

  • sigmoid(z): numerically safe (no overflow warnings for large |z|).
  • log_loss(y, p): binary cross-entropy (clip p to [1e-12, 1 − 1e-12]).
  • fit_logistic(X, y, lr, steps): gradient descent on log-loss with a bias column; return w with the bias first.
  • predict_proba(X, w) and predict(X, w, threshold=0.5).
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.