Equations · Standard statistics; Cox, “The Regression Analysis of Binary Sequences” (1958)

The logistic regression formula, explained

The simplest classifier: a linear score passed through the sigmoid to get a probability between 0 and 1. It is a one-layer neural network, and the last layer of most binary classifiers is exactly this.

P(y=1∣x)=σ(w⊤x+b)=11+e−(w⊤x+b)P(y = 1 \mid \mathbf{x}) = \sigma(\mathbf{w}^\top \mathbf{x} + b) = \frac{1}{1 + e^{-(\mathbf{w}^\top \mathbf{x} + b)}}

Open in the decoder →

Term by term

  1. P(y=1∣x)P(y = 1 \mid \mathbf{x})

    The probability that the label is 1, given the features x\mathbf{x}.

  2. w⊤x+b\mathbf{w}^\top \mathbf{x} + b

    A linear score (the logit): the dot product of the weights with the features, plus a bias bb. Its sign decides the predicted class; w⊤x+b=0\mathbf{w}^\top \mathbf{x} + b = 0 is the decision boundary.

  3. σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

    The sigmoid, squashing any real number into (0,1)(0, 1), with σ(0)=0.5\sigma(0) = 0.5. Here σ\sigma is a function, not a standard deviation.

Shapes

A small worked example

w=(2,−1)\mathbf{w} = (2, -1), b=0.5b = 0.5, x=(1,3)\mathbf{x} = (1, 3): the score is 2−3+0.5=−0.52 - 3 + 0.5 = -0.5, so P(y=1∣x)=σ(−0.5)≈0.378P(y = 1 \mid \mathbf{x}) = \sigma(-0.5) \approx 0.378, and the prediction is class 0. Training minimizes the binary cross-entropy, whose gradient is simply X⊤(y^−y)/NX^\top(\hat{\mathbf{y}} - \mathbf{y})/N.

In code

import numpy as np

def predict_proba(X, w, b):
    return 1 / (1 + np.exp(-(X @ w + b)))     # (N,)

def gradients(X, y, w, b):
    p = predict_proba(X, w, b)
    return X.T @ (p - y) / len(y), np.mean(p - y)   # d loss / dw, d loss / db

Every symbol in it

p(x),  q(z)p(\mathbf{x}), \; q(\mathbf{z})Probability p(x)p(y∣x)p(y \mid x)Given (conditional bar)x∈Rd\mathbf{x} \in \mathbb{R}^{d}Bold lowercase (vector)x⊤y\mathbf{x}^\top \mathbf{y}Transpose⟨x,y⟩\langle \mathbf{x}, \mathbf{y} \rangleInner product (angle brackets)σ(z)\sigma(z)Sigma (lowercase)ex=exp⁡(x)e^{x} = \exp(x)Exponentialab\frac{a}{b}Fraction

Other equations