Linear models · lesson 9 of 12
Parts in this lesson
A1 / (1 + np.exp(-z))1.7: the sigmoid
Bnp.log(p / (1 - p))log-odds, the sigmoid undone
CLogisticRegression().fit(X, y)scikit-learn’s version
Dmodel.predict_proba(X)probabilities, not just labels

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:

logistic.ipynb

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 (0,1): lesson 1.7's sigmoid,

σ(z)=11+e−z.

Large positive z gives nearly 1; large negative z gives nearly 0; z=0 gives exactly 0.5. Logistic regression is

p=σ(w⋅x+b),

a linear model whose output z=w⋅x+b becomes a probability.

logistic.ipynb

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:

Sigmoid squeezeAdelie (0) or Chinstrap (1), from bill length
00.5130354045505560bill length (mm)ChinstrapAdelie

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 z, the straight-line part, mean? Undo the sigmoid. If p=σ(z), then

z=log⁡p1−p.

p/(1−p) 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:

logistic.ipynb

The second cell turns the probabilities back into the original z values. That gives each weight a meaning: adding 1 to feature j adds wj to the log-odds, which multiplies the odds by ewj. In medicine and social science, logistic regression weights are often reported this way, as odds ratios.

The decision boundary

scikit-learn's LogisticRegression fits w and b (lesson 3.10 shows how):

logistic.ipynb

w=1.025 and b=−15.127. So each extra unit of radius adds 1.025 to the log-odds of cancer, multiplying the odds by e1.025≈2.8. 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 p≥0.5 malignant. p=0.5 exactly when z=0, so the decision boundary is where

wx+b=0⟹x=−bw=14.76.

Tumours with a radius over 14.76 are called malignant; 87.9% of them are classified correctly. With more features the boundary w⋅x+b=0 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 (w=−8: "big tumours are benign"):

logistic.ipynb

After 2,000 steps, w 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, σ(z)(1−σ(z)) (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

What does logistic regression’s sigmoid do?
Predict, then check with the sigmoid figure: you double w and leave b at 0. What happens to the boundary and to the curve?
A model has w = 1.025 for tumour radius. What does adding 1 to the radius do?

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.

All sources for Simple Machine Learning