← chapter

Logistic regression

Keep the straight boundary. Let every feature vote. Hand back a probability.

The default linear classifier — and the model you'll actually ship.

The move up from a threshold

The data

Two classes, some overlap. A straight line can mostly separate them — but it has to tilt, because both features carry signal.

The model

Linear score, then squash it to a probability:

z=wx+bp=σ(z)=11+ezz = w^{\top}x + b \qquad p = \sigma(z) = \frac{1}{1 + e^{-z}}

Train by minimizing cross-entropy — the loss the sigmoid was built for:

L=1Ni[yilogpi+(1yi)log(1pi)]\mathcal{L} = -\frac{1}{N}\sum_i \big[\, y_i \log p_i + (1 - y_i)\log(1 - p_i)\,\big]

The gradient collapses to the error times the features:

Lw=1Ni(piyi)xi\frac{\partial \mathcal{L}}{\partial w} = \frac{1}{N}\sum_i (p_i - y_i)\,x_i

Clean (p - y). That cancellation is why you don't use squared error here.

Watch it train

Each frame is one real gradient step. The orange line is where p = 0.5; red rings are current mistakes; the loss drops underneath.

Zero weights → no line → a crude cut → rotate and slide into the gap. 96% on the toy set.

The sigmoid

Flat at the ends (confident), steep in the middle (uncertain), crossing 0.5 at z = 0.

Scratch vs. library vs. the week-1 stump

Pima diabetes, 537 train / 231 test. Our gradient descent lands on sklearn's answer; both beat the one-feature threshold.

Scratch 0.7662 · sklearn 0.7662 · week-1 stump 0.7316. Heaviest weights: glucose, BMI, pregnancies — in that order.

Takeaways