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 , and is the variance of the projected values of the points in class c.
- 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, .
- 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 . For C classes it measures how far the class means spread around the overall mean instead, and its rank is at most C − 1.
- 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.
- 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 with the largest eigenvalues, and the reduced dimension is at most min(C − 1, d).
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.
- PCA (principal component analysis) ignores the labels. It picks the direction where all the points are spread the widest. That keeps the most information, but nothing says it will separate the classes. In the lab, the PCA direction (20°) runs along the two long, side-by-side clouds, so the classes overlap.
- LDA (linear discriminant analysis) uses the labels. It picks the direction where, on the line, the two class means are far apart and each class bunches together. The statistician Fisher first proposed it, so it is also called Fisher's linear discriminant.
"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.
- 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. - 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. - 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. - The direction that makes it largest:
w ∝ S_W⁻¹(μ₁ − μ₂).S_Wis 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.
- Class 1: (0, 0), (2, 2), (1, 0), (1, 2)
- Class 2: (2, 0), (4, 2), (3, 0), (3, 2)
- Means. μ₁ = (1, 1), μ₂ = (3, 1). Difference μ₁ − μ₂ = (−2, 0). Looking only at the means, the classes are split left and right.
- 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. - 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]. - Direction.
S_W⁻¹(μ₁ − μ₂) = (2·(−2) + (−1)·0, (−1)·(−2) + 1·0) = (−4, 2). As a direction that is (−2, 1). Not flat, but tilted. - Compare. Measure J in both directions.
| Direction w | (m₁ − m₂)² | s₁² + s₂² | J |
|---|---|---|---|
| Flat (1, 0): mean-difference direction | (−2)² = 4 | 0.5 + 0.5 = 1 | 4 |
| (−2, 1) ÷ √5: LDA direction | (4 ÷ √5)² = 3.2 | 0.2 + 0.2 = 0.4 | 8 |
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
| Property | In plain words | In the lab |
|---|---|---|
| It uses the labels | You need class labels (supervised learning). PCA works without them | The PCA button (20°) and the LDA button (122°) point different ways |
| Only the direction matters | Scaling 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 dimensions | With C classes there are at most C − 1 useful directions (section 5) | Two classes, so one line |
| Changing the units does not change the result | Stretch, 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 exception | Not in the lab. Put Ax in place of x in the answer from section 2 and A cancels out |
| Best when its assumption holds | If 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
- Fewer dimensions. Squeeze many inputs into at most C − 1 numbers where the classes separate well. With three or more classes, plot the first two directions as a 2D picture to see how the classes group. The reduced numbers can also feed another model.
- A classifier. With two classes, drop onto the line and cut at one spot. When both classes have the same number of points and the assumption below holds, the cut is halfway between the two dropped means (the orange dashed line in the lab), and the boundary is a straight line (a flat plane with more inputs). Training is just computing means and covariances and solving one inverse, with no repeated steps.
# 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.
- The classes spread differently. If one class is narrow and the other is wide, the best boundary is curved. Use quadratic discriminant analysis (QDA), which gives each class its own covariance.
- The means are the same. If one class surrounds the other like a ring, the two means land on the same spot in every direction, and J is 0.
- One class comes in several clumps. Its mean falls in the empty space between the clumps and no longer stands for the class.
- There are outliers. The means and covariances get pulled by a few far-off points.
- There are more inputs than examples. S_W cannot be inverted, or the inverse is unstable. Use shrinkage, which pulls the covariance toward a simpler shape, or reduce with PCA first.
8. When to use which
| Option | Good | Bad | Use it when |
|---|---|---|---|
| PCA | Needs no labels. You choose how many dimensions to keep. Keeps the most information | Ignores the classes, so it can throw away the direction that separates them (20° in the lab). The answer depends on the units of the inputs | There are no labels, you want to plot or compress the data, or you want fewer inputs before another model |
| LDA | Uses the labels to find the separating direction. Solved by one formula, no repeated training. Reduces dimensions and classifies at once. Works with many classes | At most C − 1 dimensions. Assumes bell shapes with the same spread. The boundary can only be straight. Sensitive to outliers | You have labels and little data, you want to see many classes in a 2D plot, or you need a fast, simple baseline classifier |
| Logistic regression | Learns the boundary directly without assuming how the inputs are distributed. Gives the probability of each class | Needs repeated training such as gradient descent. Does not reduce dimensions | Classification 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 here | What books call it |
|---|---|
| The method on this page | linear discriminant analysis (LDA), Fisher's linear discriminant |
| Class, the label with the right answer | class, label |
| Drop onto a line | projection |
| J(w) | Fisher criterion |
| Spread inside the classes, S_W | within-class scatter matrix |
| Spread between the classes, S_B | between-class scatter matrix |
| Table of how inputs spread together, Σ | covariance matrix |
| Learning with labels / without labels | supervised / unsupervised learning |
| Direction of widest spread | principal component |
| Bell shapes with the same spread | Gaussians with a shared covariance |
| The version with a separate spread per class | quadratic discriminant analysis (QDA) |
| Pulling the covariance toward a simpler shape | shrinkage |
| The other LDA with the same name | latent Dirichlet allocation |
Sources
- R. A. Fisher, The use of multiple measurements in taxonomic problems, Annals of Eugenics. The original paper on the linear combination that best separates two groups.
- scikit-learn docs: Linear and Quadratic Discriminant Analysis (shared covariance assumption, dimensionality reduction, shrinkage), LinearDiscriminantAnalysis (n_components is at most the number of classes − 1).
- Wikipedia, Linear discriminant analysis. Fisher's criterion, the answer w ∝ (Σ₀ + Σ₁)⁻¹(μ₁ − μ₀), and the multiclass version.