Skip to content
mlmentorship

Logistic regression

Linear regression for binary classification: pass a linear combination through a sigmoid, train by maximum likelihood. Still the strongest non-trivial baseline for tabular classification.

Published · 5 min read ·Core ·Foundation

Visual quick review

Visual first · depth when needed

See that logistic regression is linear on the log-odds scale: equal steps in the linear score multiply the odds, while the sigmoid compresses those steps into unequal probability changes near zero and one.

Preparing the visual…

Summary

Logistic regression models where is the sigmoid. Trained by maximum likelihood = minimizing binary cross-entropy.

Logistic regression is the first model you should try on any tabular classification problem. It is interpretable, calibrated by default (when trained on representative data), fast to fit, and competitive with much fancier methods on high-quality features. Most “production tabular models” at large companies have a strong logistic baseline they need to beat.

It is also the canonical example of a generalized linear model and the building block for softmax regression, neural network output layers, and many fairness / calibration analyses.

The model

For binary :

The log-odds (logit) is linear in :

This is what “linear in the features” means here. Linear in the log-odds, not in the probability.

Learning objective

See how a linear score becomes multiplicative odds and a saturating probability.

Linear logits mapped to odds and sigmoid probabilities A sigmoid curve maps the linear score z from negative values to probabilities near zero and positive values to probabilities near one. Five equally spaced scores, minus two through two, are marked. Their probabilities are 0.12, 0.27, 0.50, 0.73, and 0.88, while their odds relative to the preceding unit scale by the constant factor e. A dashed vertical line at score zero marks even odds, probability one half, and the usual decision boundary. 0 0.5 1 probability p p=.12 p=.27 p=.50 p=.73 p=.88 decision boundary z=-2 -1 0 1 2 odds .14 .37 1 2.72 7.39
Read it this way: move one equal step along the linear score z = wTx + b. Each step multiplies the odds by the same factor e (0.14, 0.37, 1, 2.72, 7.39), but the sigmoid compresses the resulting probabilities near 0 and 1. At z = 0, odds are 1:1 and p = 0.5, so the usual threshold is the linear boundary wTx + b = 0. Original schematic checked against An Introduction to Statistical Learning and the Stanford CS229 notes.

Training

Negative log-likelihood (binary cross-entropy):

This loss is convex in , so any local minimizer is global. No closed form (unlike linear regression); standard solvers:

  • L-BFGS (default in scikit-learn): full-batch quasi-Newton.
  • SGD / Adam: for very large datasets.
  • Newton-Raphson / IRLS: classic statistical solver, fast for small problems.

Add L2 regularization (ridge) by appending to the loss; this is the default for most implementations.

Multinomial / softmax regression

Generalize to classes: . Loss is categorical cross-entropy. Output layer of every classification network is exactly this.

Properties

  • Calibration: when the linear log-odds assumption holds, predicted probabilities match empirical frequencies (well-calibrated by construction).
  • Interpretability: is the change in log-odds per unit change in (holding others constant). is the odds ratio.
  • Decision boundary: linear in feature space (). For non-linear boundaries, transform features first (interactions, polynomials, kernels). Equivalent to fitting in a transformed space.

When to use vs. alternatives

SettingLogistic regression vs. alternative
Small-medium tabular, high-quality featuresLogistic competitive with GBDT and neural nets
Sparse high-dimensional (text bag-of-words)Logistic with L1 is excellent
Non-linear interactions matterGBDT (xgboost, lightgbm) usually wins
Calibration matters, simple model requiredLogistic is the answer
Large numbers of categorical featuresField-aware factorization machines or GBDT
Production scoring with tight latencyLogistic is the cheapest option

Common pitfalls

  • Forgetting to scale features. Solvers converge faster and regularization is more meaningful when features are standardized.
  • Including the intercept in regularization. Most implementations exclude it by default; if not, your model is biased toward predicting the prior near the boundary.
  • Comparing logistic against tree models on the same features. Trees handle non-linear interactions automatically; logistic does not. Make features comparable (one-hot, target encoding) before claiming “X beats Y.”
  • Using probability threshold 0.5 by default. Pick the threshold from the precision-recall tradeoff at the deployment operating point.