← 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

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

Open it in the Studio ▶

Check your understanding