Skip to content
BIASED

Logistic regression

Block 1 of 8~2 min

From a score to a probability

The topic Linear classifiers and surrogate losses ended with a table of what each surrogate loss estimates. The hinge loss recovers only the side of the boundary. The logistic loss recovers the log-odds, and the probability itself follows from the log-odds.

Many decisions need that probability, not just the side. The previous topic's example was the chance of default against the price of a loan. Here take the churn task: a telecom offers a discount to customers who are likely to leave. Whether the discount pays off depends on how likely each customer is to leave, compared with what the discount costs. Logistic regression is the linear model built to output that probability.

In this topic the labels are y∈{0,1}y \in \{0, 1\}, with 11 for a customer who left. They suit the Bernoulli distribution. The margin form of the previous topic uses labels ±1\pm 1, which here are 2y−12y - 1. The score is z=⟨w,x⟩+bz = \langle w, x \rangle + b. To shorten the formulas, add a constant feature 11 to every xx, so that the bias becomes one of the weights and z=⟨w,x⟩z = \langle w, x \rangle.

Why not model the probability as a linear function? A linear score takes every real value, while a probability lives in [0,1][0, 1], so a linear model would happily predict 1.31.3 or −0.2-0.2. The odds p/(1−p)p/(1 - p) live in (0,∞)(0, \infty), and the log-odds in (−∞,∞)(-\infty, \infty). So the log-odds is a quantity a linear score can describe without leaving its range. Logistic regression sets it equal to the score:

log⁡P(y=1∣x)1−P(y=1∣x)=⟨w,x⟩⟺P(y=1∣x)=σ(⟨w,x⟩)=11+e−⟨w,x⟩.\log\frac{P(y = 1 \mid x)}{1 - P(y = 1 \mid x)} = \langle w, x \rangle \quad\Longleftrightarrow\quad P(y = 1 \mid x) = \sigma(\langle w, x \rangle) = \frac{1}{1 + e^{-\langle w, x \rangle}} .

The sigmoid is just the inverse of the log-odds. A score of 00 means p=0.5p = 0.5; a score of 22 gives σ(2)=0.881\sigma(2) = 0.881, and a score of −2-2 gives 0.1190.119. Two properties of σ\sigma from the topic Derivatives and Taylor series in Mathematics for ML are used below: σ(−z)=1−σ(z)\sigma(-z) = 1 - \sigma(z) and σ′(z)=σ(z) (1−σ(z))\sigma'(z) = \sigma(z)\,(1 - \sigma(z)).

Answers are saved on the server and tied to this browser until you sign in.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Sigmoid and logistic NLLoptional

    σ(z), NLL and ∇w on a tiny dataset. Self-check in the notebook.

    ~20 min · easier on a computer · Logistic regression topic read.

    Self-check in the notebook — not part of mastery or the certificate.