PPristone AcademyDiagnose · Build · Transfer

Part of the Pristone Academy AI Technical Track

A straight line is about to make a medical mistake.

It's the most natural tool to reach for, it looks reasonable, and on a real yes/no question it quietly gets people killed. Logistic regression is the fix — and the reason why is worth actually seeing.

200,000 FT
The QuestionYes or no?

Some questions aren't “how much?” They're “which one?”

“How much will this house sell for?” has a number for an answer, and a straight line through the data — linear regression — handles it beautifully. But look at the questions a hospital, a bank, or an email provider actually asks: Is this tumor malignant? Will this loan default? Is this message spam? The answer isn't a quantity. It's a side: yes or no, this class or that one.

That's classification, and it's one of the most useful things a machine can do. The trick is that we still want a number back — not a flat “yes,” but a probability: “92% likely malignant” tells a doctor far more than a bare label. Logistic regression is the workhorse that turns a row of numbers into exactly that: a calibrated probability of “yes.”

A linear model is a thermometer — it reads a quantity off a scale. A logistic model is a judge — it weighs the evidence and returns how confident it is in a verdict. You can force a thermometer to act like a judge, but you shouldn't, and the next lab shows you why.
Where you meet it every day

The spam filter on your inbox, the “fraud?” check when your card is swiped, whether an ad gets shown to you, the first pass on a loan application, a first read on a medical scan — a huge share of these are logistic regression or its direct descendants. It is often the first model a data scientist tries, because it's fast, it rarely overfits, and its answers are easy to explain.

60,000 FT
The Case AgainstWhy not a line?

Watch one extreme case break the straight line.

Here's the honest experiment. Take tumors, plot each one by size, and label it benign (0) or malignant (1). Fit two models to the exact same points: a straight line (linear regression) and a logistic sigmoid. On clean data they agree. Then press “Add an extreme case” — a very large, very obviously malignant tumor — and watch what each model does.

Live · line vs. sigmoid
both agree

Two models fit the same tumor data. The purple line and the amber sigmoid agree: everything left of the marker reads benign, everything right reads malignant. Now press “Add an extreme case.”

Misclassified cases
STRAIGHT LINE0
SIGMOID0
LINE 0.5 AT x =
EXTREME CASES0
least-squares line
logistic sigmoid
benign (0)
malignant (1)
line got it wrong

The line tilts. That far-out point is technically “more than correct,” and because a line is judged on squared distance, it tips the whole line to get closer to the outlier — which slides its 0.5 crossing to the right. Tumors it used to call malignant now fall below the threshold and get a red ring: newly misclassified, by adding a point everyone agrees on. The sigmoid just flattens toward 1 and ignores the tug.

For an ambitious reader, it's worth naming the three separate things going wrong — because they map onto three real reasons the field moved past the line.

1 · Impossible answers

A line has no ceiling or floor. Feed it a huge tumor and it happily predicts 1.4 — a 140% probability. Feed it a tiny one and it says −0.3. Probabilities live in [0, 1]; the line doesn't know that. The sigmoid can only ever output a number between 0 and 1.

2 · Outliers move the verdict

You just watched it. Because the line minimizes squared error, a point far in the “correct” direction still yanks it, moving the decision threshold and breaking easy cases. The sigmoid saturates — once a point is clearly on one side, pushing it further changes almost nothing.

3 · Wrong thing rewarded

Squared error treats a 0/1 label like a measured quantity, so it punishes a confident-correct 0.9 for not being a perfect 1.0. Log loss — coming up — rewards confidence when you're right and only punishes it when you're wrong.

The one-line summary of this whole section

Linear regression predicts an unbounded quantity and lets far-away points vote loudest. Classification needs a bounded probability that ignores the obvious cases and focuses on the border. That's precisely what the sigmoid buys you.

30,000 FT
The MechanicsHow it decides

A weighted sum, an S-curve, and a line in the sand.

Logistic regression starts exactly like linear regression: multiply each feature by a weight, add them up, add a bias. Call that sum z. If a bigger z should mean “more likely yes,” then every weight is just how much that feature pushes toward yes.

z = w₁x₁ + w₂x₂ + … + bthe same weighted sum a line uses — the evidence score

The one new idea is what you do with z. Instead of reporting it raw, you pass it through the sigmoid — an S-shaped squasher that maps any number, from −∞ to +∞, into the range 0 to 1. A big positive z comes out near 1 (“almost certainly yes”), a big negative one near 0, and z = 0 comes out at exactly 0.5 — dead uncertain.

σ(z) = 1 / (1 + e−z)the sigmoid: any score in, a probability out

The decision boundary is just where z = 0

Since the model predicts “yes” whenever the probability clears 0.5, and the probability is 0.5 exactly when z = 0, the frontier between the two decisions is the set of points where w₁x₁ + w₂x₂ + b = 0. That is the equation of a straight line (a flat plane, with more features). This is the decision boundary: cross it and the verdict flips. It's why plain logistic regression always carves the world with a straight edge — the weights can rotate and shift that edge, but never bend it.

Below, two clouds of points and a boundary that begins as a random guess. Press Run and watch the line swing into position while the background — the model's predicted probability everywhere — sharpens from a muddy blur into two confident regions. (How it knows which way to swing is the next section.)

Live · logistic regression
ready

Two classes, and a boundary that starts as a random guess. Press Run: gradient descent nudges the three numbers (w₁, w₂, b) downhill on the cost, and the line swings until it separates the colors.

Training
EPOCH0
LOG LOSS (COST)
ACCURACY
class 0
class 1
decision boundary
shading = predicted probability

Two things to notice. The boundary is always straight — that's the model's honest limit. And the shading is soft: right next to the line the model is barely 55% sure, far from it the model is 99% sure. Logistic regression never just points; it tells you how strongly it's pointing.

UNDER THE HOOD
The LearningCost + descent

How the line knows which way to move.

“Training” means finding the weights that make the model right. To search for them you need one number that scores how wrong the current weights are — a cost function — and a way to walk it downhill.

The cost: reward honesty, punish confident lies

Before a machine can get better, it needs to know how wrong it is right now, as a single number. The clever part is how logistic regression counts wrongness — it doesn't just tally right and wrong answers, it weighs how confident each one was.

Picture the model as a weather forecaster who reports probabilities. You don't grade them on rain-or-shine alone — you grade their nerve. Say “90% chance of rain” and it pours: excellent. Say “1% chance” and it pours anyway: that forecaster should lose their job. Log loss grades your model in exactly that spirit.

For each example, log loss asks one question — what probability did you put on the answer that actually happened? — and turns it into a penalty. Land near 1 and the penalty is almost nothing. Drift toward 0 and it climbs, gently at first, then straight off a cliff:

The truth was “yes.” What the model's confidence cost it:
p = 0.99
cost 0.01 · confident & right
p = 0.70
cost 0.36 · leaning right
p = 0.50
cost 0.69 · a pure coin flip
p = 0.20
cost 1.61 · leaning wrong
p = 0.01
cost 4.61 · confident & WRONG

That cliff on the bottom row is the whole personality of log loss: being confidently wrong is the cardinal sin. And notice it never lets the model off the hook with a lazy 0.5 either — a permanent coin flip still carries a real penalty, so the model is always pushed to commit to a side when the evidence is there.

cost = −[ y·log(p) + (1−y)·log(1−p) ]y is the true 0/1 label; p is the model's predicted probability

Only one of the two terms is ever alive: if y = 1 the cost is −log(p), if y = 0 it's −log(1−p). Averaged over every example, that's the single number the training tries to shrink — the LOG LOSS you saw ticking down in the lab.

The descent: walk downhill in the fog

So we have a wrongness score. Now — how does the model actually lower it, when there are millions of possible weight settings and no map telling it which is best?

Picture the cost as a hilly landscape. Every possible setting of the weights is a spot on the ground, and the height there is how wrong the model is at that spot. You're standing on it blindfolded, in thick fog, trying to reach the lowest valley. You can't see the map — but you can feel which way the ground tilts under your feet. So you step downhill. Then you feel again, and step again.

That is gradient descent, and the “tilt under your feet” has a name: the gradient, the direction of steepest uphill. Step the opposite way and the cost drops a little; feel, step, repeat. Here's the gift log loss gave us — its landscape has just one valley, no false dips to get trapped in, so every downhill path arrives at the same bottom. It barely matters where you start. And each weight gets nudged in proportion to how much it was to blame for the error — a beautifully simple rule falls out of the math:

wⱼ ← wⱼ − α · (p − y) · xⱼerror (p − y) times the input, scaled by the learning rate α

Read it in plain English: the update is the error times the input. If the model predicted 0.9 but the truth was 0 (error +0.9), any feature that was large and pushed toward “yes” gets its weight pulled down. The α, the learning rate, is your step size — too small and training crawls, too big and it overshoots the valley and bounces. That loop, run a few hundred times, is every “Step once” you clicked stitched together — and it's the same engine, scaled up, that trains the giant neural networks behind modern AI. The idea that a single error-times-input rule underlies all of it is exactly the kind of thread a 1:1 session can pull all the way through.

10,000 FT
The TrapOverfitting

A model that memorizes has learned nothing.

A straight boundary can be too rigid — miss an obvious curve in the data (underfitting). The tempting fix is to give the model more power: add squared and cross terms so the boundary can bend. Push that too far and you get the opposite disease. The boundary starts contorting to capture every last point, including the noise — the mislabeled scan, the fluke. That's overfitting: brilliant on the data it studied, useless on anything new.

UNDERFITtoo simpleGOOD FITgeneralizesOVERFITmemorizes noise

The tell is the gap between two scores: high accuracy on the training data, low accuracy on data it hasn't seen. The overfit boundary on the right detours just to swallow that one stray point — and that detour is exactly what will misfire on the next patient. The main defenses are worth knowing by name:

Regularization

Add a penalty on large weights to the cost. The model now pays for complexity, so it keeps the boundary as simple as the data allows. This is the standard, and it's built into logistic regression by default in most libraries.

Fewer features

Every extra engineered feature is another way to bend around noise. Fewer, well-chosen inputs are harder to overfit and easier to explain.

A held-out test

Never trust the training score. Judge the model on data it never saw during training — that number is the only honest estimate of how it behaves in the real world.

0 FT
What to Carry OffThe through-line

Five ideas, one chain.

Everything here links up into a single sentence you can reconstruct the whole model from:

Say it back in one breath

Take a weighted sum of the inputs, squash it with the sigmoid into a probability, call the 0.5 line the decision boundary, score the weights with log loss, and let gradient descent walk them downhill — while regularization keeps the boundary from memorizing the noise.

It's a line, wearing a probability

The math starts identical to linear regression. The sigmoid is the one twist that turns an unbounded score into a bounded, outlier-resistant probability — which is the entire reason it wins at yes/no.

Simple on purpose

A straight boundary and a one-valley cost make logistic regression fast, stable, and hard to fool. That's why it's the baseline serious practitioners beat before reaching for anything fancier.

Frequently asked questions

Logistic regression answers yes/no questions with a probability. It takes a weighted sum of the inputs — the same starting point as linear regression — and then squashes that sum through the sigmoid function so the output always lands between 0 and 1. That number is read as the probability of the 'yes' class, and anything above 0.5 is predicted yes.
A straight line can output 1.3 or -0.4, which is meaningless as a probability, and a single extreme data point tilts the whole line and drags its decision threshold, causing it to misclassify obvious cases. Logistic regression fixes both: the sigmoid keeps every prediction in [0, 1] and saturates, so far-away points stop pulling on it, and it is trained with a cost function built for probabilities rather than squared error.
The decision boundary is the set of inputs where the model is exactly 50/50 — where the weighted sum equals zero, so the sigmoid outputs 0.5. With two features it is a straight line; with more features it is a flat plane. On one side the model predicts one class, on the other side the other class. Logistic regression always produces a straight (linear) boundary unless you add engineered features.
It uses log loss (also called binary cross-entropy). Log loss rewards confident correct answers and punishes confident wrong ones very harshly — predicting 0.99 for a case that was actually 'no' costs a large amount. Squared error is avoided because, paired with the sigmoid, it produces a bumpy cost surface with local traps; log loss produces a single smooth valley that gradient descent can always roll down to.
Gradient descent starts with random weights and repeatedly measures the slope of the cost with respect to each weight, then nudges every weight a small step in the downhill direction. Each step lowers the log loss a little. Because the log-loss surface has one valley, the process reliably converges to the best weights regardless of where it started.
Overfitting is when the model bends itself around the noise in the training data — often after adding many engineered features — so it scores well on data it has seen but poorly on new data. The main defenses are regularization (penalizing large weights so the boundary stays simple), using fewer features, and checking performance on a held-out validation set rather than the training set.

Curious who's behind these lessons? See the proof of work.

Personalized instruction

Not sure what to learn or practice next?

A focused diagnostic turns your current work into a prioritized plan instead of adding another generic checklist.

Book a paid diagnostic