Skip to content
Lambdian

Linear discriminant analysis

A generative linear classifier and supervised projection: Gaussian classes with a shared covariance.

Animated explanation

The idea

Logistic regression models the boundary directly. LDA takes the generative route: it assumes each class is a Gaussian cloud with its own mean but the same shape (covariance), estimates those clouds, and classifies a point by Bayes' rule: which cloud more probably produced it?

Because the clouds share a shape, the boundary comes out linear. The same mathematics also answers a geometric question first asked by Fisher in 1936: onto which direction should we project the data so that the classes are as separated as possible, relative to their spread? That makes LDA both a classifier and a supervised dimensionality reduction tool.

Fisher's projection

Projecting onto a direction w\mathbf{w} turns each point into one number. A good direction pushes the projected class means apart and keeps each class tightly bunched. Fisher's ratio J(w)J(\mathbf{w}) measures exactly that, and its maximiser is w∝SW−1(μ1−μ0)\mathbf{w} \propto S_W^{-1}(\boldsymbol\mu_1 - \boldsymbol\mu_0), where SWS_W is the pooled within-class scatter matrix. Multiplying by SW−1S_W^{-1} tilts the direction away from axes along which the classes are stretched.

With KK classes, LDA finds up to K−1K-1 such directions: a supervised alternative to PCA for visualisation.

LDA, QDA and logistic regression

  • LDA (shared covariance) → linear boundaries, few parameters, works well with small data, but is hurt when the Gaussian assumption is badly wrong.
  • QDA (one covariance per class) → quadratic boundaries, more parameters, needs more data.
  • Logistic regression fits the same linear form without assuming Gaussians: more robust, slightly less efficient when the assumptions hold.

With many features and few samples, SWS_W is singular; shrinkage LDA adds a multiple of the identity.

The algorithm, step by step

  1. 1
    Estimate class priors πk=nk/n\pi_k = n_k/n and class means μk\boldsymbol\mu_k.
  2. 2
    Compute the pooled covariance Σ=1n−K∑k∑i∈k(xi−μk)(xi−μk)⊤\Sigma = \frac{1}{n-K}\sum_k\sum_{i\in k}(\mathbf{x}_i - \boldsymbol\mu_k)(\mathbf{x}_i - \boldsymbol\mu_k)^\top.
  3. 3
    Discriminant for class kk: δk(x)=x⊤Σ−1μk−12μk⊤Σ−1μk+log⁡πk\delta_k(\mathbf{x}) = \mathbf{x}^\top\Sigma^{-1}\boldsymbol\mu_k - \tfrac12\boldsymbol\mu_k^\top\Sigma^{-1}\boldsymbol\mu_k + \log\pi_k.
  4. 4
    Predict arg⁡max⁡kδk(x)\arg\max_k \delta_k(\mathbf{x}).

The maths

Fisher criterion

J(w)=(w⊤(μ1−μ0))2w⊤SWwJ(\mathbf{w}) = \frac{(\mathbf{w}^\top(\boldsymbol\mu_1-\boldsymbol\mu_0))^2}{\mathbf{w}^\top S_W\mathbf{w}}

Optimal direction

w∗∝SW−1(μ1−μ0)\mathbf{w}^\ast \propto S_W^{-1}(\boldsymbol\mu_1 - \boldsymbol\mu_0)

Discriminant function

δk(x)=x⊤Σ−1μk−12μk⊤Σ−1μk+log⁡πk\delta_k(\mathbf{x}) = \mathbf{x}^\top\Sigma^{-1}\boldsymbol\mu_k - \tfrac12\boldsymbol\mu_k^\top\Sigma^{-1}\boldsymbol\mu_k + \log\pi_k

Strengths and weaknesses

Strengths

  • Closed form, no tuning, very fast
  • Works well with small samples when classes are roughly Gaussian
  • Doubles as supervised dimensionality reduction (K − 1 directions)

Weaknesses

  • Assumes Gaussian classes with equal covariance
  • Sensitive to outliers (means and covariances)
  • Needs shrinkage when features outnumber samples

Reach for it when…

Small to medium data with roughly elliptical classes, or when you want a supervised 2-D projection for plots.

Check your understanding

Question 1

Q1Why does LDA's best direction usually differ from the line joining the class means?

Question 2

Q2What does allowing each class its own covariance matrix change?

Question 3

Q3How many discriminant directions can LDA find for 4 classes?

Lambdian Lab

Fisher's discriminant

Two-class LDA from scratch.

  • fisher_direction(X, y): SW−1(μ1−μ0)S_W^{-1}(\mu_1 - \mu_0), normalised to unit length.
  • fit_lda(X, y): return (w, b) for the linear discriminant with equal-covariance Gaussians and class priors, so that class 1 is predicted when w⊤x+b>0w^\top x + b > 0. Use the pooled covariance with denominator n − 2.
  • predict(X, w, b).
Open the labPython + NumPy + scikit-learn runs in your browser. Sign-in keeps your code and attempts.