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.
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.
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.
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.
- 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.
- 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.
A student who passed. The model said their chance of passing was:
- 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.
Two groups that overlap a little, like most real data.
- Epoch
- –
- Log-loss
- –
- Accuracy
- –
z = 0·x + 0·y + 0
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.
- 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.
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
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
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
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
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
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
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
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
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
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
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?”.
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.