← All simulations · Pillar 4: Predicting numbers
Logistic regression
What it is
Logistic regression is regression’s cousin that answers yes/no questions. Instead of predicting a number, it predicts a chance — a value between 0 and 1, like “78% likely to pass.” To keep that output a real probability, it bends a straight line into a smooth S-curve (the sigmoid): far to one side it flattens near 0, far to the other it flattens near 1, and it swings between them in the middle.
Go deeper: the curve has two knobs. The midpoint is the input where the chance is exactly ½ — a coin flip — and it acts as the yes/no decision line: predict “yes” on the high side, “no” on the low side. The steepness controls how sharply the S rises: a steep curve is very sure once you pass the midpoint; a gentle one stays unsure for longer. The midpoint changes accuracy; the steepness changes confidence.
Why care
Yes/no predictions are everywhere: pass or fail, spam or not, sick or healthy, will-click or won’t. Logistic regression is the simplest, most widely used tool for them — fast, easy to read, and it gives a calibrated probability rather than a bare guess. It’s also the output layer of most classifying neural networks, so the S-curve you bend here shows up at the end of far bigger models.
The idea, intuitively
Each dot is a student: failers sit along the bottom, passers along the top, spread out by how many hours they studied. Slide the midpoint to put the yes/no line where it best separates them, then set the steepness so the curve is confident where the data is clear and cautious where it overlaps. Try to match the computer’s best fit — and see why a plain straight line simply can’t do this job.
Peek at the data first
Each row is an input (hours studied) and a yes/no label (pass or fail). Because the answer is yes/no,
we predict a chance between 0 and 1 instead of a plain number — the same labelled shape
Spectra’s describe_data would show before training.
Try it
Drag Midpoint to move the dashed yes/no line (the model predicts “pass” to its right) and watch the number correct change. Drag Steepness to make the S sharper or softer and watch the log-loss change. Tick Show the computer’s best fit to chase the lowest log-loss, and Compare to a straight line to see it leave the 0–1 range.
Where it shows up
- Everyday yes/no calls. Credit approvals, medical screening, click prediction, churn — wherever you need a probability, not just a label.
- Neural networks. A sigmoid (or its multi-class cousin, softmax) usually sits at the output to turn raw scores into chances.
- A readable baseline. It’s often the first model tried, because each input’s effect on the odds is easy to explain.
Where it came from
The S-shaped logistic function was introduced by Pierre François Verhulst in the 1830s–40s to model population growth. Statisticians later adapted it for yes/no prediction, and by the mid-1900s — with work by Joseph Berkson, David Cox and others — logistic regression became a workhorse of statistics and, later, machine learning.
Try it in code
In the Studio, a classifier predicts a yes/no label and can report how confident it is — the same idea as bending this S-curve to a probability:
data = load "students" train, test = split data, hold_out: 20% model = make_model "classifier" train_model model, on: train, predict: "result", using: ["hours_studied"] check model, with: test show_model model
Check your understanding
- Why does logistic regression predict a chance between 0 and 1 instead of a plain number?
- What does moving the midpoint do, and what does changing the steepness do?
- Why can’t a plain straight line be used as a probability?