Skip to content

Logistic regression

A weighted sum becomes a probability through the S-shaped sigmoid; the boundary at one half is a straight line, and the size of the weights sets how sharp it is.

Helps to know first
This topic

First, the picture

A score says which side of a line a dot is on, not how sure to be. Below, scores become chances. Before it plays: where will the boundary run?

From a score to a probability

100 labelled dots in two crescents; the plane is shaded by the probability of class A.

100 training dots in two crescents. Every weight starts at 0, so every score is 0 and every point of the plane has probability one half: the plane is neutral.

mistakes on the 100 training dots
–
mistakes on 100 new dots
–
steps of gradient descent
0
0.00 / 19.70 s
Describe this picture

Two panels. The first is the plane of Nearest neighbours, feature 1 across and feature 2 up: 100 training dots in two crescents that overlap at their tips, 50 round dots of class A and 50 squares of class B. The plane is shaded by the probability of class A that the model gives each point, from the class B colour through the plain ground at one half to the class A colour. The second panel is the S-shaped curve of that probability against the score, with every dot placed on the curve at its own score. In the clip every weight starts at 0, so every score is 0 and the whole plane is plain, every probability one half. Then the shading settles in stages, after 5, 10, 20, 50 and 200 steps of gradient descent: the line where the probability is one half fades in, turns as the weights grow, and comes to rest between the crescents, rising gently from left to right, while the band of uncertain shading narrows. Last, the dots on the wrong side of the final line get a thin ring; they sit at the tips where the crescents reach into each other. Readouts count the steps of gradient descent and the mistakes on the 100 training dots and on 100 new dots from the same crescents, never drawn: 13 and 12 after 200 steps. After the clip a slider, “Steepness, a”, multiplies every score by a=0.25a=0.25, 0.5, 1, 2 or 4 before the S-curve, so the shading is σ(az)\sigma(az); it starts at 1, and the line never moves.

Squashing a score

A line rule, as in What machine learning is, gives each point a score,

z=w⊤x+b,z=\mathbf{w}^\top\mathbf{x}+b,

positive on one side of the line and negative on the other. Here zz is only the score; it is not the variable of a z-transform or a standardised value. A score can be any number, so it is not a chance. Turn it into one with the sigmoid,

σ(z)=11+e−z,\sigma(z)=\frac1{1+e^{-z}},

an S-shaped curve that runs from 0 far to the left to 1 far to the right, through one half at z=0z=0. I always write it with its argument, σ(z)\sigma(z), because a bare σ\sigma means a spread elsewhere. The model’s chance of class A is

p^=σ(w⊤x+b).\hat p=\sigma(\mathbf{w}^\top\mathbf{x}+b).

This is logistic regression: a linear score, squashed into a probability. Despite its name it predicts a class, through the chance of that class, not a number on a scale. The curve is symmetric: σ(−z)=1−σ(z)\sigma(-z)=1-\sigma(z), so the chance of class B is σ(−z)\sigma(-z).

The score is the log-odds

Undo the sigmoid. From p^=1/(1+e−z)\hat p=1/(1+e^{-z}), the ratio of the chance of class A to the chance against it is

p^1−p^=ez,soln⁡p^1−p^=z.\frac{\hat p}{1-\hat p}=e^{z},\qquad\text{so}\qquad \ln\frac{\hat p}{1-\hat p}=z.

That ratio is the odds, and the score is the logarithm of the odds, the log-odds or logit. Each unit added to the score multiplies the odds by e≈2.718e\approx2.718.

In probability and statistics · odds

Bayes’ theorem in the statistics course writes a chance as odds, so many for to so many against, and updates them by multiplying. A logistic model adds to the log-odds, which is the same as multiplying the odds.

The boundary is a line

To turn a chance into a class, compare it with a threshold. At the usual threshold, one half, the rule is class A when p^>0.5\hat p>0.5. Since σ(z)>0.5\sigma(z)>0.5 exactly when z>0z>0, the decision boundary is

w⊤x+b=0,\mathbf{w}^\top\mathbf{x}+b=0,

a straight line, whatever the S-curve looks like. The curve bends the chances, not the boundary.

Now the answer to the question. The boundary runs straight between the crescents, rising gently. A straight line cannot follow the curved gap between them, so the dots at the tips of each crescent, where they reach into the other, stay on the wrong side; those are the ringed dots, 13 of the 100. Decision trees and Multilayer networks draw boundaries that can bend.

Watch out

A curved S-curve does not mean a curved boundary. Logistic regression draws a straight line between the classes.

Steepness

Multiply the weights and the bias term together by a positive number aa, the steepness the slider sets. Every score is multiplied by aa, so the points where z=0z=0 do not move: the boundary stays exactly where it is. What changes is how fast the chance rises across it. With a=4a=4 a point a little way from the line already gets a chance near 0 or 1, and the uncertain band is narrow. With a=0.25a=0.25 the band is wide, and even points far from the line get modest chances.

So the length of the weight vector says how sure the model is, and the direction of w\mathbf{w} and bb together says where the boundary lies. However steep, the line still cannot bend round the tips of the crescents.

Choosing the weights

The weights in the clip were fitted by Gradient descent, 200 steps with learning rate 0.5 from all weights zero, on a loss that punishes confident mistakes: a dot of class A given a chance near 0 costs a great deal. Cross-entropy loss builds that loss and its gradient. Because the crescents overlap, no line separates them, and the fitted weights stay finite. Two hundred steps bring them close to the bottom of that loss, not onto it: run to the end, the second weight would be 2.473 instead of 2.437, a line a little steeper in its shading and almost exactly where it is.

What 0.9 means

A chance of 0.9 does not mean the model is right 90 % of the time on that one dot. One dot is either class A or not, and one outcome cannot prove a chance right or wrong. A chance is a statement about many cases: among all the dots the model gives about 0.9, about 9 in 10 should be class A. A model that keeps to that is well calibrated. Whether it does is something to check on new data.

600 new dots from the crescents, sorted by the probability of class A the model gives them. Each bar is the share of a bin's dots that really are class A; on the dashed diagonal it would equal the probability. Of the 230 dots given 0.8 to 1, 208 are class A: about nine in ten. A probability is a rate over many cases, and a straight-line model can misstate it: of the dots given 0.6 to 0.8 (0.7 on average), only 61 % are class A.
Describe this picture

A bar chart: across, the probability of class A the model gives, from 0 to 1 in five bins of 0.2; up, the share of each bin’s dots that really are class A. The dots are 600 new dots from the same crescents, 300 of each class, sorted into the bins by the probability the fitted model gives them, and each bar is labelled with its count, such as 208/230. A dashed diagonal marks where the share would equal the probability. Of the 230 dots given 0.8 to 1, 208 are class A: about nine in ten. The caption adds that a straight-line model can misstate a probability: of the dots given 0.6 to 0.8, 0.70 on average, only 61 % are class A. There is no control.

The bars show how far a straight-line model’s chances can be taken as rates on these crescents: the closer each bar to the diagonal, the more its chances mean what they say. At the top they nearly do. Between 0.6 and 0.8 they overstate: the model says about 7 in 10 and the dots say 6. A line that cannot follow the crescents’ curve cannot get every rate right either.

Thresholds

One half is a choice, not a law. If missing a faulty part costs far more than a needless inspection, flag a part when its chance of a fault passes 0.1. A lower threshold catches more of the class and wrongly flags more of the rest. Measuring a classifier counts both kinds of mistake.

Worked example

One feature. A component’s chance of a fault from its temperature, with weight w=0.5w=0.5 per °C and b=−30b=-30. The score is z=0.5×temperature−30z=0.5\times\text{temperature}-30, zero at 60 °C: the boundary.

Temperature and the chance of a fault

The worked example: score z = 0.5 × temperature − 30, probability σ(z), boundary at 60 °C.

24 components and their temperatures: a dot at the top failed, a dot at the bottom did not.

temperature
–
score z
–
probability of a fault
–
odds of a fault
–
0.00 / 15.90 s
Describe this picture

One panel: component temperature across, from 40 to 80 °C, and fault up, with 24 made-up components as dots, at the top if the component failed and at the bottom if it did not. In the clip the components appear, then the S-curve of the probability of a fault for the model of this example, score z=0.5×temperature−30z=0.5\times\text{temperature}-30, draws from the left, and the point where the probability is one half, at 60 °C, is marked. A marker then visits 64, 70 and 56 °C. Readouts give the temperature, the score zz, the probability of a fault and the odds of a fault. There is a play button and no other control.

  • At 64 °C, z=32−30=2z=32-30=2 and p^=1/(1+e−2)=0.8808\hat p=1/(1+e^{-2})=0.8808. The odds are e2=7.39e^2=7.39 to 1.
  • At 56 °C, z=−2z=-2 and p^=0.1192\hat p=0.1192, which is 1−0.88081-0.8808.
  • At 70 °C, z=5z=5 and p^=0.9933\hat p=0.9933.
  • Each extra degree adds 0.5 to the score, so it multiplies the odds by e0.5=1.649e^{0.5}=1.649.

Two features. Take w=(2,−1)\mathbf{w}=(2,-1) and b=1b=1. The boundary is 2x1−x2+1=02x_1-x_2+1=0, the line x2=2x1+1x_2=2x_1+1. The point (1,1)(1,1) scores 2−1+1=22-1+1=2, so p^=0.8808\hat p=0.8808; the point (0,3)(0,3) scores −3+1=−2-3+1=-2, so p^=0.1192\hat p=0.1192.

Steeper. Multiply everything by 3: w=(6,−3)\mathbf{w}=(6,-3), b=3b=3. Now (1,1)(1,1) scores 6 and p^=0.9975\hat p=0.9975, but the boundary, 6x1−3x2+3=06x_1-3x_2+3=0, is the same line x2=2x1+1x_2=2x_1+1.

Backwards. At what temperature is p^=0.1192\hat p=0.1192? At z=−2z=-2, so 0.5×temperature=280.5\times\text{temperature}=28: 56 °C. And σ(0)=0.5\sigma(0)=0.5.

Where you’ll meet this

Medical risk scores. Measurements such as age and blood pressure are weighted, summed and turned into a chance of illness within some years.

Credit scoring. The chance that a loan is repaid, from the applicant’s records.

Clicks. The chance that a person clicks a link, from what is known about the link and the page.

Machine failure. The chance that a machine fails before its next service, from its temperature and vibration.

Reference card

QuantityFormulaNotes
Scorez=w⊤x+bz=\mathbf{w}^\top\mathbf{x}+bthe log-odds, or logit
Sigmoidσ(z)=1/(1+e−z)\sigma(z)=1/(1+e^{-z})0 to 1, one half at z=0z=0
Chance of class Ap^=σ(z)\hat p=\sigma(z)σ(−z)=1−σ(z)\sigma(-z)=1-\sigma(z)
Log-oddsln⁡p^1−p^=z\ln\frac{\hat p}{1-\hat p}=zone unit of score multiplies the odds by ee
Boundaryw⊤x+b=0\mathbf{w}^\top\mathbf{x}+b=0a straight line, where p^=0.5\hat p=0.5
Steepnessscale w\mathbf{w} and bb togetherthe boundary stays; the band narrows as aa grows
Thresholdclass A when p^\hat p passes it0.5 is a choice
Calibrationamong chances near 0.9, about 9 in 10checked on new data

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look