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 , an S-curve that squeezes into .
The model is still a weighted sum of features, so the decision boundary (, i.e. ) 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 . The likelihood of all labels is . 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 . The gradient is : 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: . Increasing by one multiplies the odds by . 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 (ever sharper steps): unregularised weights blow up. In practice always add an L2 (or L1) penalty. scikit-learn does this by default, with .
The algorithm, step by step
- 1Standardise features; add a bias term.
- 2Initialise .
- 3Repeat: , then .
- 4Predict class 1 when threshold (0.5 by default; tune it for your costs).
The maths
Model
Log-loss
Gradient
Sigmoid derivative
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…
Check your understanding
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).