Logistic regression
Last updated
Linear regression predicts a number. Many questions want a probability instead: how likely is this tumour to be malignant, this message to be spam? A straight line can’t be a probability, since it runs past 0 and 1. Logistic regression bends the line through the sigmoid so that it can. It’s the simplest classifier that gives probabilities, and one neuron of a neural network is exactly this.
By the end of this lesson you can
- Explain why a straight line can’t output a probability
- Compute the sigmoid, and turn a probability into log-odds and back
- Fit LogisticRegression, and find and explain its decision boundary
- Explain why training a classifier on squared error can stall
Why a line can’t output a probability
Back to lesson 2.8's tumours. Take one feature, the mean radius of the cells' nuclei, and label malignant as 1 and benign as 0. Linear regression will happily fit a line to 0s and 1s:
Its predictions run from −0.34 to 1.77. If they're meant to be the probability of cancer, that's nonsense: a probability is between 0 and 1. A straight line keeps going forever in both directions, and the biggest tumours push it past 1 and the smallest below 0. Worse, those extreme tumours, which are the easiest to classify, pull the line around the most (lesson 2.7's squared error, punishing big misses).
The sigmoid
The fix is to keep the line, but pass its output through a function that squeezes any number into : lesson 1.7's sigmoid,
Large positive gives nearly 1; large negative gives nearly 0; gives exactly 0.5. Logistic regression is
a linear model whose output becomes a probability.
Here it is on lesson 3.1's kind of line, with penguins instead of tumours: Adelie (0) or Chinstrap (1), from bill length. Squeeze the line:
The pink curve is the straight line z: it runs below 0 and above 1, so it can't be a probability. Slide the squeeze to 1. The boundary, where p = 0.5, is at 45 mm: 210 of 219 penguins (96%) are on the right side of it.
Log-odds
What does , the straight-line part, mean? Undo the sigmoid. If , then
is the odds: a probability of 0.75 is odds of 3 to 1. Its log is the log-odds, or logit. So logistic regression is a linear model of the log-odds:
The second cell turns the probabilities back into the original values. That gives each weight a meaning: adding 1 to feature adds to the log-odds, which multiplies the odds by . In medicine and social science, logistic regression weights are often reported this way, as odds ratios.
The decision boundary
scikit-learn's LogisticRegression fits and (lesson 3.10 shows how):
and . So each extra unit of radius adds 1.025 to the log-odds of cancer, multiplying the odds by . predict_proba gives one column per class, benign then malignant: a radius of 10 is 0.8% likely to be malignant, 15 is 56%, 20 is 99.5%.
To turn probabilities into labels, predict calls everything with malignant. exactly when , so the decision boundary is where
Tumours with a radius over 14.76 are called malignant; 87.9% of them are classified correctly. With more features the boundary is a straight line, a flat plane, or a flat surface in more dimensions: logistic regression always splits the space with something flat. That's its strength (simple, fast, readable) and its limit (it can't draw curves without lesson 3.7's extra features). And the 0.5 threshold is just a default: lesson 2.8 showed how to move it.
Wrong way: squared error for classification
Why not train logistic regression by lesson 3.3's squared error? Here it is, on standardised radius, from a bad start (: "big tumours are benign"):
After 2,000 steps, has only crawled from −8 to −2.13, still the wrong sign, and accuracy is stuck at 0.627: it calls everything benign. The trouble is the sigmoid's flat ends. Squared error's gradient passes through the sigmoid's slope, (lesson 1.7), which is nearly 0 when the model is confidently wrong. So the worse the mistake, the smaller the push to fix it.
Lesson 3.10 replaces squared error with cross-entropy, whose gradient doesn't have this problem: from the same bad start, it gets to 0.879 in under 500 steps.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Cox, The regression analysis of binary sequences, Journal of the Royal Statistical Society B 20(2), 1958: logistic regression for yes-or-no outcomes
- scikit-learn: Logistic regression: LogisticRegression, predict_proba, and its default penalty
- scikit-learn: Breast cancer Wisconsin (diagnostic) dataset: the tumour measurements, as in lesson 2.8