Linear discriminant analysis (LDA): the direction that best separates classes

Drop points onto a line and each one becomes a single number. Pick one line and two classes pile on top of each other. Pick another and they come cleanly apart. Turn the line, find the direction that separates the classes best, and then build the formula for it.

Short answer. Linear discriminant analysis (LDA) looks at the labels (classes) and finds the direction where, after dropping the points onto a line, the classes sit far apart and each class stays tight. That direction makes J(w) = (m₁ − m₂)² ÷ (s₁² + s₂²) as large as possible, and the answer is w ∝ S_W⁻¹(μ₁ − μ₂). This is different from PCA, which ignores the labels and finds the widest spread. You can use LDA to shrink the number of inputs, or as a classifier.

Turn the line with the slider or the number box. Each point drops straight down onto the line and lands in the histogram below. J measures, with one number, how well the two histograms come apart. The 40 points are made up by a seeded random generator, so they are the same every time you open the page. Numbers are shown rounded to two decimals, and J is computed before rounding.

Try this. ① It starts in the PCA direction (20°). The histograms overlap, J = 0.56, and a cut in the middle puts 14 of the 40 points on the wrong side. The direction where the points spread the most is not the direction that separates the classes. ② Press "LDA direction". The line turns to 122°, J = 8.06, and 0 points are on the wrong side. That is almost at a right angle to the long direction of the clouds (about 30°). ③ Set the angle to 80°. The two means land at almost the same spot on the line, J = 0, and 21 points are on the wrong side. ④ Sweep the slider all the way. No angle gives a J above 8.06.

Definitions

The text below explains with pictures and experiments. These are the textbook definitions. A point is a vector x with d inputs, and there are C classes. For class c, Dc is the set of its points, nc is the number of points, and μc is their mean.

projection
The coordinate of the spot where point x lands when you drop it straight onto the line along a direction vector w of length 1. The projected mean of class c is mc=𝐰𝖳μc, and sc2 is the variance of the projected values of the points in class c.

y=𝐰𝖳𝐱

within-class scatter matrix
The d × d matrix you get by adding up the covariance matrix Σc of every class. This page adds covariances (averaged), and some books add sums of squares that are not averaged. If every class has the same number of points, the two differ only by a constant factor. With this choice, sc2=𝐰𝖳Σc𝐰.

Σc=1nc∑𝐱∈Dc(𝐱−μc)(𝐱−μc)𝖳

SW=∑c=1CΣc

between-class scatter matrix
A d × d matrix that collects how far the class means are from each other. For two classes it is the matrix below, and then (m1−m2)2=𝐰𝖳SB𝐰. For C classes it measures how far the class means spread around the overall mean instead, and its rank is at most C − 1.

SB=(μ1−μ2)(μ1−μ2)𝖳

Fisher criterion
After projecting onto direction w, the between-class variance divided by the within-class variance. For two classes it is the ratio below. Multiplying w by any nonzero number does not change it.

J(𝐰)=(m1−m2)2s12+s22=𝐰𝖳SB𝐰𝐰𝖳SW𝐰

linear discriminant analysis (LDA)
A supervised method that looks at the class labels and projects the points onto the direction that makes the Fisher criterion largest. It is used for dimensionality reduction and also as a classifier that splits the projected values at one spot. For two classes, if SW has an inverse, that direction and the largest J are as below. For C classes it uses the eigenvectors of SW−1SB with the largest eigenvalues, and the reduced dimension is at most min(C − 1, d).

𝐰∝SW−1(μ1−μ2)

max𝐰J(𝐰)=(μ1−μ2)𝖳SW−1(μ1−μ2)

1. What LDA is, and how it differs from PCA

Each point is one example with several inputs (here two: across and up). Each point also has a class, a label with the right answer. For email it might be "spam" or "normal". For flowers it might be the species. Many inputs are hard to work with, so you want to squeeze them into fewer numbers. The simplest way is to pick one direction and drop the points onto that line. The dot product of a point with the direction arrow w, y = wᵀx, is where it lands.

There are two ways to pick the direction.

"Spread between groups ÷ spread within groups" is the same idea as the F in ANOVA. ANOVA uses that ratio to check whether given groups really differ. LDA looks for the direction where that ratio is largest.

Latent Dirichlet allocation, which finds topics in documents, is also called LDA. It is a different method that only shares the name.

2. How the formula is built

These are the same points as in the lab above. Press "Next" and the line turns while one piece is added to the formula (the orange background marks the new piece).

The symbols: μ₁ and μ₂ are the two class means (points). m₁ and m₂ are those means dropped onto the line (numbers). s₁² and s₂² are the variances of each class's values on the line. w is a direction of length 1.

  1. Drop onto a line: m₁ − m₂ = wᵀ(μ₁ − μ₂). Dropping the mean gives the mean of the dropped values. So the gap between the two means on the line is the mean difference μ₁ − μ₂, dropped onto the line. In the PCA direction (20°), m₁ = −0.84, m₂ = 0.81, and the gap is 1.65.
  2. Make only the means far apart: w ∝ μ₁ − μ₂. With w of length 1, the gap is largest when w points the same way as μ₁ − μ₂. In the lab that is 170.7°, and the gap grows to 1.89. But both clouds are also spread wide along this direction, so the histograms overlap, and a cut in the middle puts 11 points on the wrong side. Why: even with far-apart means, a wide class spills its edges over to the other side.
  3. Divide by the spread inside each class: J(w) = (m₁ − m₂)² ÷ (s₁² + s₂²). Along this direction, s₁² = 1.1 and s₂² = 2.01, so J ≈ 1.16. The top grows when the means are far apart, and the bottom shrinks when each class bunches together. The top is squared, so scaling w or flipping it does not change J. Only the direction matters.
  4. The direction that makes it largest: w ∝ S_W⁻¹(μ₁ − μ₂). S_W is the two classes' covariance matrices (tables of how the inputs spread together) added up. In the lab, Σ₁ = [1.37 0.77; 0.77 0.55] and Σ₂ = [2.46 1.28; 1.28 0.78], so S_W = [3.82 2.05; 2.05 1.33], and its inverse is S_W⁻¹ = [1.46 −2.24; −2.24 4.19]. Multiply by μ₁ − μ₂ = (−1.87, 0.3) to get (−3.42, 5.47). Scaled to length 1 that is (−0.53, 0.85), which is 122°. The gap shrinks to 1.25, but the summed spread shrinks much more, from 3.11 to 0.19, so J = 8.06. Setting the derivative of J with respect to w to zero and solving gives this formula. We do not prove it here.

Multiplying by S_W⁻¹ is like first shrinking the wide directions and stretching the narrow ones until each class cloud is round, and then taking the direction of the mean difference there. That is why the answer turns away from the mean-difference direction (170.7°) toward where the classes are narrow. You can also get the largest J directly as (μ₁ − μ₂)ᵀ S_W⁻¹ (μ₁ − μ₂), which is 8.06 in the lab. If you redo the math by hand with the rounded numbers, the last digit may differ a little.

Some books write S_W as a sum of squares over the points instead of covariances (which are averages). When both classes have the same number of points, the two differ by a constant factor, so the best direction is the same and only the size of J changes.

Try this. Press "Next" three times and watch J: 0.56 (PCA direction), then 1.16 (mean-difference direction), then 8.06 (LDA direction). Points on the wrong side go 14, 11, 0. From the second step to the third, the line stays put and only the formula changes.

3. Work it out by hand

A tiny data set with four points per class. Class 2 is class 1 moved 2 to the right.

  1. Means. μ₁ = (1, 1), μ₂ = (3, 1). Difference μ₁ − μ₂ = (−2, 0). Looking only at the means, the classes are split left and right.
  2. Covariance of each class. Take the mean away from class 1: (−1, −1), (1, 1), (0, −1), (0, 1). Variance across (1 + 1 + 0 + 0) ÷ 4 = 0.5, variance up (1 + 1 + 1 + 1) ÷ 4 = 1, covariance (1 + 1 + 0 + 0) ÷ 4 = 0.5. So Σ₁ = [0.5 0.5; 0.5 1]. Class 2 was only moved, so Σ₂ is the same.
  3. Add and invert. S_W = Σ₁ + Σ₂ = [1 1; 1 2]. A 2×2 inverse is [a b; c d]⁻¹ = [d −b; −c a] ÷ (ad − bc), and ad − bc = 2 − 1 = 1, so S_W⁻¹ = [2 −1; −1 1].
  4. Direction. S_W⁻¹(μ₁ − μ₂) = (2·(−2) + (−1)·0, (−1)·(−2) + 1·0) = (−4, 2). As a direction that is (−2, 1). Not flat, but tilted.
  5. Compare. Measure J in both directions.
Direction w(m₁ − m₂)²s₁² + s₂²J
Flat (1, 0): mean-difference direction(−2)² = 40.5 + 0.5 = 14
(−2, 1) ÷ √5: LDA direction(4 ÷ √5)² = 3.20.2 + 0.2 = 0.48

In the LDA direction, s₁² is wᵀΣ₁w = ((−2)²·0.5 + 2·(−2)·1·0.5 + 1²·1) ÷ 5 = (2 − 2 + 1) ÷ 5 = 0.2. The squared gap dropped a little, from 4 to 3.2, but the spread inside the classes dropped a lot, from 1 to 0.4, so J doubled. Each class leans up and to the right, like the points (0, 0) and (2, 2), so the best direction tilts away from that spread. The direct formula for the largest J agrees: (−2, 0) · (−4, 2) = 8.

4. Properties worth knowing

PropertyIn plain wordsIn the lab
It uses the labelsYou need class labels (supervised learning). PCA works without themThe PCA button (20°) and the LDA button (122°) point different ways
Only the direction mattersScaling w or flipping it leaves J the sameθ and θ + 180° are the same line, so angles go from 0 to 180° only
At most C − 1 dimensionsWith C classes there are at most C − 1 useful directions (section 5)Two classes, so one line
Changing the units does not change the resultStretch, shrink, or mix the inputs in a way you can undo, and the largest J and the classification stay the same. PCA is different: its answer depends on the units. LDA with a penalty (shrinkage) is an exceptionNot in the lab. Put Ax in place of x in the answer from section 2 and A cancels out
Best when its assumption holdsIf every class is bell-shaped with the same shape of spread, the LDA boundary is the boundary that makes the fewest mistakes (section 7)Both clouds are long in the same direction

5. With C classes, at most C − 1 dimensions

With three or more classes, the top of J becomes "how far the class means spread from the overall mean" (the between-class spread S_B), the bottom becomes the within-class spread S_W, and you make the same ratio large. The answer is not one direction but several: the eigenvectors of S_W⁻¹S_B.

But the between-class spread only lives in directions where the means sit apart. Two points lie on one line, and three points lie on one plane. C means lie in at most C − 1 dimensions, so at most C − 1 directions give a J above 0. It also cannot be more than the number of inputs d, so LDA reduces to at most min(C − 1, d) dimensions. Handwritten digits with ten classes go to at most 9 dimensions, and two classes give just one line.

6. Where it is used: fewer dimensions, and a classifier

# scikit-learn: pass the labels y as well
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
lda = LinearDiscriminantAnalysis(n_components=1)   # at most number of classes − 1
x_small = lda.fit_transform(x_train, y_train)      # fewer dimensions
y_pred = lda.predict(x_test)                       # classifier

7. The assumption, and when it breaks

The direction that makes J largest is defined without assuming any distribution. But that direction gives the boundary with the fewest mistakes only when each class follows a normal distribution (bell shape) and the shape of the spread (the covariance matrix) is the same for every class. The two clouds in the lab, long in the same direction, are that case. Here is how it breaks.

8. When to use which

OptionGoodBadUse it when
PCANeeds no labels. You choose how many dimensions to keep. Keeps the most informationIgnores the classes, so it can throw away the direction that separates them (20° in the lab). The answer depends on the units of the inputsThere are no labels, you want to plot or compress the data, or you want fewer inputs before another model
LDAUses the labels to find the separating direction. Solved by one formula, no repeated training. Reduces dimensions and classifies at once. Works with many classesAt most C − 1 dimensions. Assumes bell shapes with the same spread. The boundary can only be straight. Sensitive to outliersYou have labels and little data, you want to see many classes in a 2D plot, or you need a fast, simple baseline classifier
Logistic regressionLearns the boundary directly without assuming how the inputs are distributed. Gives the probability of each classNeeds repeated training such as gradient descent. Does not reduce dimensionsClassification is the goal and you need probabilities, or the data is far from bell-shaped

All three give straight boundaries or directions. If the boundary has to bend, move to QDA, logistic regression with extra inputs, or a neural network.

Frequently asked questions

Is this the same LDA as in topic models?

No. Latent Dirichlet allocation, which finds topics in a pile of documents, is also shortened to LDA. It is a probability model that finds topics from which words show up together, with no labels. It only shares the name with linear discriminant analysis on this page.

Why is there only one line when there are two classes?

The directions LDA finds come from where the class means sit apart. Two means always lie on one line, so there is one useful direction. With C classes, the C means lie in at most C − 1 dimensions, so LDA gives at most C − 1 dimensions.

Can LDA separate two classes with the same mean?

No. In every direction the two means land on the same spot, so the top of J is 0. Data where one class surrounds the other like a ring is like this. Use quadratic discriminant analysis (QDA), which gives each class its own spread, or a model that learns a curved boundary.

Words

Words used hereWhat books call it
The method on this pagelinear discriminant analysis (LDA), Fisher's linear discriminant
Class, the label with the right answerclass, label
Drop onto a lineprojection
J(w)Fisher criterion
Spread inside the classes, S_Wwithin-class scatter matrix
Spread between the classes, S_Bbetween-class scatter matrix
Table of how inputs spread together, Σcovariance matrix
Learning with labels / without labelssupervised / unsupervised learning
Direction of widest spreadprincipal component
Bell shapes with the same spreadGaussians with a shared covariance
The version with a separate spread per classquadratic discriminant analysis (QDA)
Pulling the covariance toward a simpler shapeshrinkage
The other LDA with the same namelatent Dirichlet allocation

Sources

← Principal component analysist-SNE and UMAP →