Find Fisher's linear discriminant direction for two classes
Why it matters LDA is a fast linear classifier and a supervised dimensionality reduction step, still common in credit scoring, face recognition baselines and EEG classification.
Fisher's linear discriminant finds the projection that pushes two class means apart while keeping each class tight.
Write fisher_lda(X, y). X is (n, d), y has labels 0 and 1. Let mu0, mu1 be the class means and S_w = sum over both classes of (X_c - mu_c)^T (X_c - mu_c), the within-class scatter. The direction is w = S_w^-1 (mu1 - mu0), scaled to unit length. Return (w, threshold) where threshold = w . (mu0 + mu1) / 2, the midpoint of the projected means, so points with w . x > threshold are predicted as class 1.
def fisher_lda(X, y)Example
X [[1, 2], [2, 3], [3, 3], [6, 5], [7, 8], [8, 7]], y [0, 0, 0, 1, 1, 1] gives w [0.997688, 0.067964] and threshold 4.807021 (rounded).
Understand the idea
Imagine shining a torch on two clouds of points and looking at their shadows on a wall. Fisher's discriminant picks the wall direction where the two shadows overlap the least: far-apart centres, narrow spreads.
The naive choice is the line joining the two class means. But if the clouds are long thin ovals tilted at an angle, that line can make them overlap a lot. Multiplying by the inverse within-class scatter, S_w^-1, turns the direction away from the stretched axes.
Worked example: suppose the classes are stretched along the diagonal and the means differ by (1, 0). The mean-difference direction (1, 0) cuts across the stretched axis and the shadows overlap. S_w^-1 (1, 0) points closer to the anti-diagonal, where both ovals are thin, so the shadows separate.
Rule: w = S_w^-1 (mu1 - mu0), normalised; predict class 1 when w . x > w . (mu0 + mu1) / 2.
LDA is used as a quick linear classifier and to reduce features to the few most class-separating directions.
Shapes and data flow
X (n, d) -> means (d,) -> S_w (d, d) -> w (d,)