Logistic Regression

See why a straight line fails for yes/no answers and how the sigmoid fixes it. Fit a curve by hand, compare log-loss with squared error, train a decision boundary and read the weights as odds.

10above 1?below 0?

1A straight line is the wrong shape

Pass or fail is 1 or 0. A straight line fitted to that runs below 0 and above 1, so it can't be a probability, and a few extreme points drag it about.

10p = 0.5

2The sigmoid bends it into a probability

Logistic regression still computes a straight-line score, z = w·x + b, then squashes it with the sigmoid σ(z) into a number between 0 and 1: the chance of passing.

p = 0.5

3With two features, the boundary is a line

Where the score is 0 the probability is exactly 0.5. With two features those points form a straight line: one side is predicted 1, the other 0, and the shading shows how sure the model is.

Start here

Why not just fit a straight line?

Sixteen students: how many hours each one studied, and whether they passed (1) or failed (0). We want the chance that a new student passes. Linear regression would draw a straight line through the 0s and 1s, and call “above 0.5” a pass. Look at what goes wrong, then add three keen students who studied 16–20 hours. They passed, as anyone would expect.

024681000.51line crosses 0.5p = 0.50.5 hours: failed, model says 1% chance of passing1 hours: failed, model says 2% chance of passing1.5 hours: failed, model says 3% chance of passing2 hours: failed, model says 6% chance of passing2.5 hours: failed, model says 10% chance of passing3 hours: failed, model says 17% chance of passing4 hours: failed, model says 42% chance of passing5.5 hours: failed, model says 82% chance of passing3.5 hours: passed, model says 28% chance of passing4.5 hours: passed, model says 57% chance of passing5 hours: passed, model says 72% chance of passing6 hours: passed, model says 90% chance of passing6.5 hours: passed, model says 94% chance of passing7 hours: passed, model says 97% chance of passing8 hours: passed, model says 99% chance of passing9 hours: passed, model says 100% chance of passing
passedfailedstraight line (linear regression)sigmoid (logistic regression)
Students
Line says pass from
4.34 h
Sigmoid says pass from
4.26 h
Line: correct
14 / 16
Sigmoid: correct
14 / 16

The line goes below 0 for students who barely studied (-0.16 at 0 hours) and above 1 for the keenest (1.36 at 10 hours). Neither is a probability. The sigmoid bends to stay between 0 and 1.

The model

The sigmoid: a score squashed into 0 to 1

Logistic regression keeps the straight-line score z = w × hours + b, then passes it through the sigmoid σ(z) = 1 / (1 + e^(−z)). Big positive scores give nearly 1, big negative scores nearly 0, and a score of 0 gives exactly 0.5. Move the sliders: w sets how steep the S is, b slides it left and right. The red bars are each student's miss, and thicker means it costs more.

024681000.51p = 0.50.5 hours: failed, model says 15% chance of passing1 hours: failed, model says 20% chance of passing1.5 hours: failed, model says 25% chance of passing2 hours: failed, model says 31% chance of passing2.5 hours: failed, model says 38% chance of passing3 hours: failed, model says 45% chance of passing4 hours: failed, model says 60% chance of passing5.5 hours: failed, model says 79% chance of passing3.5 hours: passed, model says 52% chance of passing4.5 hours: passed, model says 67% chance of passing5 hours: passed, model says 73% chance of passing6 hours: passed, model says 83% chance of passing6.5 hours: passed, model says 87% chance of passing7 hours: passed, model says 90% chance of passing8 hours: passed, model says 94% chance of passing9 hours: passed, model says 97% chance of passing
0.60
-2.0
Your log-loss
0.403
Best possible
0.317
50% point
3.33 h
Correct
14 / 16

Try w = 0: the curve goes flat and says the same thing for everyone. Try a negative w: more study now means less chance of passing, and the loss shoots up.

Measuring mistakes

Log-loss: confident and wrong costs the most

For each student, look at the probability the model gave to what actually happened, and take −ln of it. Right and sure (p near 1): almost free. Unsure (0.5): 0.69. Sure and wrong: huge, and it has no ceiling. Squared error, by comparison, never charges more than 1, so it barely notices a model that is badly overconfident. Log-loss also gives a bowl-shaped loss with no false valleys, so gradient descent can't get stuck.

00.250.50.75101234
log-loss −ln psquared error (1 − p)²

A student who passed. The model said their chance of passing was:

0.20
Log-loss −ln p
1.609
Squared error (1 − p)²
0.640

Wrong side of 0.5. Log-loss charges noticeably more than squared error.

loss = −[y ln p + (1 − y) ln(1 − p)]

With y = 1 only the first half counts, −ln p; with y = 0 only the second, −ln(1 − p).

Watch it learn

Training a decision boundary

Now two features (say, hours studied and hours slept) and two classes. Each epoch, the model predicts a probability for every point, averages the gradient (p − y) × feature and takes one step. The shading is the model's probability; the line is where it's exactly 0.5. Points it gets wrong are ringed in red.

02468100246810(3.79, 2.63): class 0(5.04, 6.4): class 1(3.38, 4.38): class 0(4.12, 5.7): class 1(2.5, 4.92): class 0(7.13, 6.84): class 1(3.4, 3.93): class 0(6.85, 5.06): class 1(5.59, 5.46): class 0(8.85, 5.78): class 1(5.01, 3.87): class 0(5.24, 7.73): class 1(2.8, 3.05): class 0(6.89, 4.99): class 1(3.01, 4.98): class 0(8.2, 7.09): class 1(5.33, 3.14): class 0(7.91, 8.1): class 1(5.37, 0.43): class 0(8.89, 5.53): class 1(1.54, 6.04): class 0(7.16, 5.4): class 1(1.5, 3.9): class 0(6.58, 7.92): class 1(5.5, 2.25): class 0(8.54, 7.39): class 1(4.26, 4.71): class 0(7.14, 4.83): class 1(2.96, 4.16): class 0(6.16, 8.28): class 1
class 1class 0p = 0.5misclassified
Data

Two groups that overlap a little, like most real data.

Epoch
–
Log-loss
–
Accuracy
–

z = 0·x + 0·y + 0

step 050
Log-loss by epoch
Step 0 / 54
350 ms

Press Start to train on 30 points for 50 epochs, or Step to go one stage at a time.

From probability to decision

Moving the threshold

The model gives a probability; you decide where to cut. At 0.5 the cut is the line where z = 0. Raise it and the line slides toward the class-1 side: fewer false alarms, more misses. Lower it for the opposite. Which way to go depends on which mistake costs more.

02468100246810(3.79, 2.63): class 0, p = 0.000(5.04, 6.4): class 1, p = 0.971(3.38, 4.38): class 0, p = 0.004(4.12, 5.7): class 1, p = 0.465(2.5, 4.92): class 0, p = 0.006(7.13, 6.84): class 1, p = 1.000(3.4, 3.93): class 0, p = 0.001(6.85, 5.06): class 1, p = 0.899(5.59, 5.46): class 0, p = 0.808(8.85, 5.78): class 1, p = 1.000(5.01, 3.87): class 0, p = 0.011(5.24, 7.73): class 1, p = 1.000(2.8, 3.05): class 0, p = 0.000(6.89, 4.99): class 1, p = 0.883(3.01, 4.98): class 0, p = 0.015(8.2, 7.09): class 1, p = 1.000(5.33, 3.14): class 0, p = 0.002(7.91, 8.1): class 1, p = 1.000(5.37, 0.43): class 0, p = 0.000(8.89, 5.53): class 1, p = 0.999(1.54, 6.04): class 0, p = 0.040(7.16, 5.4): class 1, p = 0.977(1.5, 3.9): class 0, p = 0.000(6.58, 7.92): class 1, p = 1.000(5.5, 2.25): class 0, p = 0.000(8.54, 7.39): class 1, p = 1.000(4.26, 4.71): class 0, p = 0.046(7.14, 4.83): class 1, p = 0.872(2.96, 4.16): class 0, p = 0.001(6.16, 8.28): class 1, p = 1.000
0.50
True positives
14
False positives
1
False negatives
1
True negatives
14
Precision
93%
Recall
93%

A cancer screen wants a low threshold (don't miss anyone); a spam filter wants a high one (don't bin real mail). More on precision, recall and ROC curves →

Why people like it

Reading the weights: odds

The score z is the log of the odds, ln(p / (1 − p)). So every weight has a plain meaning: one more unit of that feature adds w to the log-odds, which multiplies the odds by e^w. For the students, w = 1.24: each extra hour of study multiplies the odds of passing by 3.45. That readability is why logistic regression is still used in medicine, credit scoring and anywhere a model has to explain itself.

5 h

Score (log-odds)

z = 1.24 × 5 − 5.28

0.92

Odds

e^z

2.51

Probability

odds ÷ (1 + odds)

72%

Odds of 2.51 to 1: in the long run, about 2.5 students like this pass for every one that fails. Add an hour and the odds go up 3.45×, wherever you start.

No magic, just arithmetic

The maths, step by step

One student studied 5.5 hours and failed (y = 0). The model currently has w = 1 and b = -4. Here is everything that happens for this one student during training.

  1. 1

    Score

    Exactly what linear regression would compute: a weighted sum plus a bias. It can be any number.

    z = w × x + b = 1 × 5.5 + (-4) = 1.5000

  2. 2

    Squash it into a probability

    The sigmoid turns any score into a number between 0 and 1. A score of 0 gives exactly 0.5.

    p = 1 ÷ (1 + e^(−z)) = 1 ÷ (1 + e^(−1.5)) = 0.8176

    The model gives this student a 82% chance of passing. They failed, so it is wrong, and fairly sure of itself.

  3. 3

    Log-loss

    Take −ln of the probability the model gave to what really happened. Confident and wrong is punished hard.

    loss = −[y ln p + (1 − y) ln(1 − p)] = −ln(1 − 0.8176) = 1.7014

  4. 4

    Gradient

    The calculus works out to something very simple: the error (p − y) times the input. It's the same shape as linear regression's gradient, which is no accident.

    ∂loss/∂w = (p − y) × x = 0.8176 × 5.5 = 4.4967

    ∂loss/∂b = p − y = 0.8176 − 0 = 0.8176

  5. 5

    Update

    Step against the gradient, with learning rate 0.1. Training does this for every student, averaged, many times over.

    w = w − η × ∂loss/∂w = 1 − 0.1 × 4.4967 = 0.5503

    b = b − η × ∂loss/∂b = -4 − 0.1 × 0.8176 = -4.0818

    new p = σ(w × x + b) = 0.2583

    One small step and the chance of passing this student drops from 82% to 26%: the model moved toward the truth.

Where it leads

Beyond yes or no

More than two classes

Softmax generalises the sigmoid: one score per class, turned into probabilities that add up to 1. It's how a language model picks its next token. Next-Token Sampling →

A single neuron

A neuron with a sigmoid activation is exactly logistic regression. Stack many and you get a neural network that can bend its boundary. Neural Network →

Regularisation

Adding λ·|w|² to the loss keeps the weights small: it stops the runaway on separable data and helps with many features.

Where you'll meet it

Spam filters, churn and click prediction, credit scoring, medical risk scores, and as the baseline every fancier classifier has to beat.

Keep going

Resources

Hand-picked places to go deeper. They're all free to read or watch, and they open in a new tab.

How it works: Logistic Regression

Step by step

  • Class 1
  • Class 0
  • Misclassified

Press Start or Step: the step the animation is on lights up below.

  1. 1

    Start with zero weights

    With every weight at 0, z = 0 everywhere and every prediction is 0.5. The log-loss is ln 2 ≈ 0.693.

    w, b = 0, 0
  2. 2

    Predict a probability for each point

    Weighted sum, then the sigmoid. Features are standardised first so gradient descent treats them evenly.

    p = sigmoid(X @ w + b)
  3. 3

    Compute the gradient

    Average the error (p − y) times each feature. Points the model gets wrong contribute the most.

    dw = mean((p - y) * x); db = mean(p - y)
  4. 4

    Step and repeat

    Move the weights against the gradient. Each epoch tilts and shifts the boundary a little.

    w -= lr * dw; b -= lr * db
  5. 5

    Stop when the loss levels off

    On overlapping data the boundary settles. On perfectly separable data the weights keep growing: add regularisation to stop that.

    until loss stops falling

Logistic regression answers yes-or-no questions with a probability. It computes the same weighted score as linear regression, z = w·x + b, then passes it through the sigmoid σ(z) = 1 / (1 + e^(−z)), which squashes any number into the range 0 to 1.

The score z is the log-odds: each weight says how much one unit of that feature adds to the log of the odds, so e^w is how much it multiplies the odds by. Where z = 0 the probability is exactly 0.5, and those points form the decision boundary: a straight line, or a flat plane with more features.

Training minimises the log-loss: −ln of the probability the model gave to what actually happened. Its gradient is simply the error (p − y) times the input, so gradient descent nudges the boundary toward the points it gets wrong.

Where you'll meet it

  • Yes/no predictions where you want a probability: spam or not, will a customer churn, will someone click, is a transaction fraud.
  • As the first model to try on any classification problem: fast, hard to overfit with few features, and its weights can be read and explained.
  • Interview favourites: “why not use linear regression for classification?”, “why log-loss and not squared error?”, “what do the coefficients mean?”, “how is it related to a neural network?”.
time O(epochs × n × d)space O(n × d)

Every epoch computes one score per point (d multiplications each) and the same again for the gradient. Predicting a new point costs just O(d). Libraries usually use faster second-order solvers (such as L-BFGS or Newton's method) instead of plain gradient descent.