HomeProjectsNotes
Theme
Back to Notes

Linear Discriminant Analysis (LDA)

2026-04-23


Table of Contents

  1. What is LDA?
  2. LDA vs PCA
  3. How LDA Works
  4. LDA Mathematically
  5. General Rules and Key Formulas
  6. Step-by-Step Algorithm
  7. Worked Example — From Lecture (6 Samples, 2 Features)
  8. Worked Example — Quiz Problem (4 Samples, 2 Features)
  9. Geometric Intuition
  10. LDA as a Classifier
  11. Limitations of LDA
  12. Summary

What is LDA?

Linear Discriminant Analysis (LDA) is a supervised linear dimensionality reduction technique that finds the directions (called linear discriminants) maximizing the separation between multiple classes, while minimizing the spread within each class.

LDA finds the projection that maximizes between-class variance and minimizes within-class variance simultaneously.

Core Idea

Given a dataset with n n n observations, p p p features, and C C C classes, LDA produces up to C − 1 C - 1 C−1 new features (linear discriminants) that are:

  • Class-aware — they use label information to guide the projection
  • Ordered by the amount of class separation they provide
  • Linear combinations of the original features

LDA vs PCA

Property PCA LDA
Type Unsupervised Supervised
Goal Maximize total variance Maximize class separation
Uses class labels? ❌ No ✅ Yes
Criterion Maximize variance of projected data Maximize ratio: between / within variance
Key matrix Covariance matrix Σ \Sigma Σ S W − 1 S B S_W^{-1} S_B SW−1​SB​
Max components p p p (number of features) C − 1 C - 1 C−1 (number of classes minus 1)
Best when No labels, exploring structure Labels available, classification needed

Analogy: PCA finds the direction of greatest spread in the data. LDA finds the direction of greatest separation between classes.


How LDA Works

LDA seeks a projection direction w \mathbf{w} w (a vector in the original feature space) such that, when data is projected onto it, the classes are as separated as possible.

The Two Objectives

1. Maximize between-class distance — the projected class means should be far apart.

2. Minimize within-class scatter — data points of the same class should cluster tightly around their projected mean.

Why Both Are Needed

Maximizing only the distance between means is insufficient — two classes can have well-separated means but heavily overlapping spreads, making classification unreliable. By also minimizing within-class scatter, LDA ensures a clean, non-overlapping separation.

Goal: Maximize J ( w ) = Between-Class Variation Within-Class Variation \text{Goal: Maximize} \quad J(\mathbf{w}) = \frac{\text{Between-Class Variation}}{\text{Within-Class Variation}} Goal: MaximizeJ(w)=Within-Class VariationBetween-Class Variation​

LDA Mathematically

Projection

For a data point x \mathbf{x} x, its scalar projection onto direction w \mathbf{w} w is:

z = w T x z = \mathbf{w}^T \mathbf{x} z=wTx

where:

  • x = [ x 1 , x 2 , … , x p ] T \mathbf{x} = [x_1, x_2, \ldots, x_p]^T x=[x1​,x2​,…,xp​]T is the original feature vector
  • w = [ w 1 , w 2 , … , w p ] T \mathbf{w} = [w_1, w_2, \ldots, w_p]^T w=[w1​,w2​,…,wp​]T is the projection vector (analogous to PCA's eigenvector)
  • z z z is the scalar value after projection

Projected Means

For class i i i, the projected class mean is:

m i = w T μ i m_i = \mathbf{w}^T \boldsymbol{\mu}_i mi​=wTμi​

The overall projected mean is:

m overall = w T μ overall m_{\text{overall}} = \mathbf{w}^T \boldsymbol{\mu}_{\text{overall}} moverall​=wTμoverall​

Between-Class Scatter Matrix S B S_B SB​

The between-class variance measures how far apart the projected class means are from the overall projected mean:

σ between 2 = ∑ i = 1 C n i ( m i − m overall ) 2 \sigma_{\text{between}}^2 = \sum_{i=1}^{C} n_i (m_i - m_{\text{overall}})^2 σbetween2​=i=1∑C​ni​(mi​−moverall​)2

Substituting m i = w T μ i m_i = \mathbf{w}^T \boldsymbol{\mu}_i mi​=wTμi​ and m overall = w T μ overall m_{\text{overall}} = \mathbf{w}^T \boldsymbol{\mu}_{\text{overall}} moverall​=wTμoverall​, and using the matrix identity ( a T b ) 2 = a T b b T a (a^T b)^2 = a^T b b^T a (aTb)2=aTbbTa:

σ between 2 = w T ( ∑ i = 1 C n i ( μ i − μ overall ) ( μ i − μ overall ) T ) w \sigma_{\text{between}}^2 = \mathbf{w}^T \left( \sum_{i=1}^{C} n_i (\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})(\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})^T \right) \mathbf{w} σbetween2​=wT(i=1∑C​ni​(μi​−μoverall​)(μi​−μoverall​)T)w

The term in parentheses is the between-class scatter matrix:

S B = ∑ i = 1 C n i ( μ i − μ overall ) ( μ i − μ overall ) T \boxed{S_B = \sum_{i=1}^{C} n_i (\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})(\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})^T} SB​=i=1∑C​ni​(μi​−μoverall​)(μi​−μoverall​)T​

So: σ between 2 = w T S B w \sigma_{\text{between}}^2 = \mathbf{w}^T S_B \mathbf{w} σbetween2​=wTSB​w


Within-Class Scatter Matrix S W S_W SW​

The within-class variance for class i i i measures how spread out the projected points are around their class mean:

σ i 2 = ∑ x ∈ Class   i ( z − m i ) 2 = ∑ x ∈ Class   i w T ( x − μ i ) ( x − μ i ) T w \sigma_i^2 = \sum_{\mathbf{x} \in \text{Class}\,i} (z - m_i)^2 = \sum_{\mathbf{x} \in \text{Class}\,i} \mathbf{w}^T (\mathbf{x} - \boldsymbol{\mu}_i)(\mathbf{x} - \boldsymbol{\mu}_i)^T \mathbf{w} σi2​=x∈Classi∑​(z−mi​)2=x∈Classi∑​wT(x−μi​)(x−μi​)Tw

Summing over all classes:

σ within 2 = w T ( ∑ i = 1 C ∑ x ∈ Class   i ( x − μ i ) ( x − μ i ) T ) w \sigma_{\text{within}}^2 = \mathbf{w}^T \left( \sum_{i=1}^{C} \sum_{\mathbf{x} \in \text{Class}\,i} (\mathbf{x} - \boldsymbol{\mu}_i)(\mathbf{x} - \boldsymbol{\mu}_i)^T \right) \mathbf{w} σwithin2​=wT(i=1∑C​x∈Classi∑​(x−μi​)(x−μi​)T)w

The term in parentheses is the within-class scatter matrix:

S W = ∑ i = 1 C ∑ x ∈ Class   i ( x − μ i ) ( x − μ i ) T \boxed{S_W = \sum_{i=1}^{C} \sum_{\mathbf{x} \in \text{Class}\,i} (\mathbf{x} - \boldsymbol{\mu}_i)(\mathbf{x} - \boldsymbol{\mu}_i)^T} SW​=i=1∑C​x∈Classi∑​(x−μi​)(x−μi​)T​

So: σ within 2 = w T S W w \sigma_{\text{within}}^2 = \mathbf{w}^T S_W \mathbf{w} σwithin2​=wTSW​w


The Fisher Criterion and Eigenvalue Problem

The LDA objective becomes:

J ( w ) = σ between 2 σ within 2 = w T S B w w T S W w \boxed{J(\mathbf{w}) = \frac{\sigma_{\text{between}}^2}{\sigma_{\text{within}}^2} = \frac{\mathbf{w}^T S_B \mathbf{w}}{\mathbf{w}^T S_W \mathbf{w}}} J(w)=σwithin2​σbetween2​​=wTSW​wwTSB​w​​

Maximizing J ( w ) J(\mathbf{w}) J(w) leads to the generalized eigenvalue problem:

S W − 1 S B   w = λ w \boxed{S_W^{-1} S_B \, \mathbf{w} = \lambda \mathbf{w}} SW−1​SB​w=λw​

Here, w \mathbf{w} w is the eigenvector of S W − 1 S B S_W^{-1} S_B SW−1​SB​, and λ \lambda λ is the eigenvalue. The eigenvector corresponding to the largest eigenvalue is the optimal projection direction LD1.


General Rules and Key Formulas

Rule 1: Class Mean Formula

μ i = 1 n i ∑ x ∈ Class   i x \boldsymbol{\mu}_i = \frac{1}{n_i} \sum_{\mathbf{x} \in \text{Class}\,i} \mathbf{x} μi​=ni​1​x∈Classi∑​x

Rule 2: Overall Mean Formula

μ overall = 1 n ∑ j = 1 n x j \boldsymbol{\mu}_{\text{overall}} = \frac{1}{n} \sum_{j=1}^{n} \mathbf{x}_j μoverall​=n1​j=1∑n​xj​

Rule 3: Within-Class Scatter (compact form)

S W = ∑ i = 1 C ( X i − μ i ) T ( X i − μ i ) S_W = \sum_{i=1}^{C} (X_i - \boldsymbol{\mu}_i)^T (X_i - \boldsymbol{\mu}_i) SW​=i=1∑C​(Xi​−μi​)T(Xi​−μi​)

where X i X_i Xi​ is the submatrix of rows belonging to class i i i.

Rule 4: Between-Class Scatter

S B = ∑ i = 1 C n i ( μ i − μ overall ) ( μ i − μ overall ) T S_B = \sum_{i=1}^{C} n_i (\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})(\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})^T SB​=i=1∑C​ni​(μi​−μoverall​)(μi​−μoverall​)T

Rule 5: Eigenvalue Problem

det ⁡ ( S W − 1 S B − λ I ) = 0 \det(S_W^{-1} S_B - \lambda I) = 0 det(SW−1​SB​−λI)=0

Rule 6: Projection

z = w T x z = \mathbf{w}^T \mathbf{x} z=wTx

Rule 7: Maximum Number of Discriminants

For C C C classes, LDA produces at most C − 1 C - 1 C−1 meaningful discriminant directions.

C C C (classes) Max LDs
2 1
3 2
4 3

Step-by-Step Algorithm

Algorithm: Linear Discriminant Analysis (LDA)
 
Input:  Data matrix X ∈ ℝⁿˣᵖ with class labels, number of components k ≤ C-1
Output: Projected data Z ∈ ℝⁿˣᵏ, projection vectors W
 
Step 1: COMPUTE CLASS MEANS
        For each class i:  μᵢ = (1/nᵢ) Σ x  (for x in class i)
 
Step 2: COMPUTE OVERALL MEAN
        μ_overall = (1/n) Σ xⱼ  (over all samples)
 
Step 3: COMPUTE WITHIN-CLASS SCATTER MATRIX
        Sᵢ = (Xᵢ - μᵢ)ᵀ(Xᵢ - μᵢ)  for each class i
        S_W = Σ Sᵢ
 
Step 4: COMPUTE BETWEEN-CLASS SCATTER MATRIX
        S_B = Σ nᵢ(μᵢ - μ_overall)(μᵢ - μ_overall)ᵀ
 
Step 5: COMPUTE S_W⁻¹ S_B
 
Step 6: EIGENDECOMPOSITION
        Solve: det(S_W⁻¹S_B - λI) = 0  →  find λ₁ ≥ λ₂ ≥ ... ≥ λ_{C-1}
        For each λᵢ: solve (S_W⁻¹S_B - λᵢI)wᵢ = 0  →  find unit eigenvector wᵢ
 
Step 7: SELECT TOP-k DISCRIMINANTS
        W_k = [w₁ | w₂ | ... | wₖ]
 
Step 8: PROJECT DATA
        Z = X · W_k  ∈ ℝⁿˣᵏ

Worked Example — From Lecture (6 Samples, 2 Features)

Scope: n = 6 n = 6 n=6 samples, p = 2 p = 2 p=2 features, C = 2 C = 2 C=2 classes → reduce to k = 1 k = 1 k=1 discriminant (LD1).


Step 1 — Collect the Data

Sample x 1 x_1 x1​ x 2 x_2 x2​ Class
1 1 2 0
2 2 3 0
3 3 1 0
4 6 5 1
5 7 7 1
6 8 6 1

Step 2 — Compute Class Means

For Class 0 ( n 0 = 3 n_0 = 3 n0​=3):

μ 0 = ( 1 + 2 + 3 3 ,   2 + 3 + 1 3 ) = ( 2 ,   2 ) \boldsymbol{\mu}_0 = \left(\frac{1+2+3}{3},\ \frac{2+3+1}{3}\right) = (2,\ 2) μ0​=(31+2+3​, 32+3+1​)=(2, 2)

For Class 1 ( n 1 = 3 n_1 = 3 n1​=3):

μ 1 = ( 6 + 7 + 8 3 ,   5 + 7 + 6 3 ) = ( 7 ,   6 ) \boldsymbol{\mu}_1 = \left(\frac{6+7+8}{3},\ \frac{5+7+6}{3}\right) = (7,\ 6) μ1​=(36+7+8​, 35+7+6​)=(7, 6)

Step 3 — Compute Overall Mean

μ overall = ( 1 + 2 + 3 + 6 + 7 + 8 6 ,   2 + 3 + 1 + 5 + 7 + 6 6 ) = ( 4.5 ,   4 ) \boldsymbol{\mu}_{\text{overall}} = \left(\frac{1+2+3+6+7+8}{6},\ \frac{2+3+1+5+7+6}{6}\right) = (4.5,\ 4) μoverall​=(61+2+3+6+7+8​, 62+3+1+5+7+6​)=(4.5, 4)

Step 4 — Compute Within-Class Scatter Matrix S W S_W SW​

For Class 0 — center the class data:

X 0 − μ 0 = [ 1 − 2 2 − 2 2 − 2 3 − 2 3 − 2 1 − 2 ] = [ − 1 0 0 1 1 − 1 ] X_0 - \boldsymbol{\mu}_0 = \begin{bmatrix} 1-2 & 2-2 \\ 2-2 & 3-2 \\ 3-2 & 1-2 \end{bmatrix} = \begin{bmatrix} -1 & 0 \\ 0 & 1 \\ 1 & -1 \end{bmatrix} X0​−μ0​= ​1−22−23−2​2−23−21−2​ ​= ​−101​01−1​ ​ S 0 = ( X 0 − μ 0 ) T ( X 0 − μ 0 ) = [ − 1 0 1 0 1 − 1 ] [ − 1 0 0 1 1 − 1 ] = [ 2 − 1 − 1 2 ] S_0 = (X_0 - \boldsymbol{\mu}_0)^T (X_0 - \boldsymbol{\mu}_0) = \begin{bmatrix} -1 & 0 & 1 \\ 0 & 1 & -1 \end{bmatrix} \begin{bmatrix} -1 & 0 \\ 0 & 1 \\ 1 & -1 \end{bmatrix} = \begin{bmatrix} 2 & -1 \\ -1 & 2 \end{bmatrix} S0​=(X0​−μ0​)T(X0​−μ0​)=[−10​01​1−1​] ​−101​01−1​ ​=[2−1​−12​]

For Class 1 — center the class data:

X 1 − μ 1 = [ 6 − 7 5 − 6 7 − 7 7 − 6 8 − 7 6 − 6 ] = [ − 1 − 1 0 1 1 0 ] X_1 - \boldsymbol{\mu}_1 = \begin{bmatrix} 6-7 & 5-6 \\ 7-7 & 7-6 \\ 8-7 & 6-6 \end{bmatrix} = \begin{bmatrix} -1 & -1 \\ 0 & 1 \\ 1 & 0 \end{bmatrix} X1​−μ1​= ​6−77−78−7​5−67−66−6​ ​= ​−101​−110​ ​ S 1 = ( X 1 − μ 1 ) T ( X 1 − μ 1 ) = [ − 1 0 1 − 1 1 0 ] [ − 1 − 1 0 1 1 0 ] = [ 2 1 1 2 ] S_1 = (X_1 - \boldsymbol{\mu}_1)^T (X_1 - \boldsymbol{\mu}_1) = \begin{bmatrix} -1 & 0 & 1 \\ -1 & 1 & 0 \end{bmatrix} \begin{bmatrix} -1 & -1 \\ 0 & 1 \\ 1 & 0 \end{bmatrix} = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} S1​=(X1​−μ1​)T(X1​−μ1​)=[−1−1​01​10​] ​−101​−110​ ​=[21​12​]

Total Within-Class Scatter:

S W = S 0 + S 1 = [ 2 − 1 − 1 2 ] + [ 2 1 1 2 ] = [ 4 0 0 4 ] \boxed{S_W = S_0 + S_1 = \begin{bmatrix} 2 & -1 \\ -1 & 2 \end{bmatrix} + \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 4 & 0 \\ 0 & 4 \end{bmatrix}} SW​=S0​+S1​=[2−1​−12​]+[21​12​]=[40​04​]​

Observation: S W S_W SW​ is diagonal — features have no cross-class correlation within their respective classes.


Step 5 — Compute Between-Class Scatter Matrix S B S_B SB​

For Class 0 ( n 0 = 3 n_0 = 3 n0​=3):

μ 0 − μ overall = [ 2 − 4.5 2 − 4 ] = [ − 2.5 − 2 ] \boldsymbol{\mu}_0 - \boldsymbol{\mu}_{\text{overall}} = \begin{bmatrix} 2 - 4.5 \\ 2 - 4 \end{bmatrix} = \begin{bmatrix} -2.5 \\ -2 \end{bmatrix} μ0​−μoverall​=[2−4.52−4​]=[−2.5−2​] S B 0 = 3 ⋅ [ − 2.5 − 2 ] [ − 2.5 − 2 ] = 3 ⋅ [ 6.25 5 5 4 ] = [ 18.75 15 15 12 ] S_{B0} = 3 \cdot \begin{bmatrix} -2.5 \\ -2 \end{bmatrix} \begin{bmatrix} -2.5 & -2 \end{bmatrix} = 3 \cdot \begin{bmatrix} 6.25 & 5 \\ 5 & 4 \end{bmatrix} = \begin{bmatrix} 18.75 & 15 \\ 15 & 12 \end{bmatrix} SB0​=3⋅[−2.5−2​][−2.5​−2​]=3⋅[6.255​54​]=[18.7515​1512​]

For Class 1 ( n 1 = 3 n_1 = 3 n1​=3):

μ 1 − μ overall = [ 7 − 4.5 6 − 4 ] = [ 2.5 2 ] \boldsymbol{\mu}_1 - \boldsymbol{\mu}_{\text{overall}} = \begin{bmatrix} 7 - 4.5 \\ 6 - 4 \end{bmatrix} = \begin{bmatrix} 2.5 \\ 2 \end{bmatrix} μ1​−μoverall​=[7−4.56−4​]=[2.52​] S B 1 = 3 ⋅ [ 2.5 2 ] [ 2.5 2 ] = 3 ⋅ [ 6.25 5 5 4 ] = [ 18.75 15 15 12 ] S_{B1} = 3 \cdot \begin{bmatrix} 2.5 \\ 2 \end{bmatrix} \begin{bmatrix} 2.5 & 2 \end{bmatrix} = 3 \cdot \begin{bmatrix} 6.25 & 5 \\ 5 & 4 \end{bmatrix} = \begin{bmatrix} 18.75 & 15 \\ 15 & 12 \end{bmatrix} SB1​=3⋅[2.52​][2.5​2​]=3⋅[6.255​54​]=[18.7515​1512​]

Total Between-Class Scatter:

S B = S B 0 + S B 1 = [ 37.5 30 30 24 ] \boxed{S_B = S_{B0} + S_{B1} = \begin{bmatrix} 37.5 & 30 \\ 30 & 24 \end{bmatrix}} SB​=SB0​+SB1​=[37.530​3024​]​

Step 6 — Compute S W − 1 S B S_W^{-1} S_B SW−1​SB​

Since S W = [ 4 0 0 4 ] S_W = \begin{bmatrix} 4 & 0 \\ 0 & 4 \end{bmatrix} SW​=[40​04​] is diagonal:

S W − 1 = 1 4 ⋅ I = [ 0.25 0 0 0.25 ] S_W^{-1} = \frac{1}{4} \cdot I = \begin{bmatrix} 0.25 & 0 \\ 0 & 0.25 \end{bmatrix} SW−1​=41​⋅I=[0.250​00.25​] S W − 1 S B = [ 0.25 0 0 0.25 ] [ 37.5 30 30 24 ] = [ 9.375 7.5 7.5 6 ] S_W^{-1} S_B = \begin{bmatrix} 0.25 & 0 \\ 0 & 0.25 \end{bmatrix} \begin{bmatrix} 37.5 & 30 \\ 30 & 24 \end{bmatrix} = \begin{bmatrix} 9.375 & 7.5 \\ 7.5 & 6 \end{bmatrix} SW−1​SB​=[0.250​00.25​][37.530​3024​]=[9.3757.5​7.56​]

Step 7 — Find Eigenvalues and Eigenvectors

Solve det ⁡ ( S W − 1 S B − λ I ) = 0 \det(S_W^{-1} S_B - \lambda I) = 0 det(SW−1​SB​−λI)=0:

det ⁡ [ 9.375 − λ 7.5 7.5 6 − λ ] = 0 \det \begin{bmatrix} 9.375 - \lambda & 7.5 \\ 7.5 & 6 - \lambda \end{bmatrix} = 0 det[9.375−λ7.5​7.56−λ​]=0 ( 9.375 − λ ) ( 6 − λ ) − ( 7.5 ) 2 = 0 (9.375 - \lambda)(6 - \lambda) - (7.5)^2 = 0 (9.375−λ)(6−λ)−(7.5)2=0 56.25 − 9.375 λ − 6 λ + λ 2 − 56.25 = 0 56.25 - 9.375\lambda - 6\lambda + \lambda^2 - 56.25 = 0 56.25−9.375λ−6λ+λ2−56.25=0 λ 2 − 15.375 λ = 0    ⟹    λ ( λ − 15.375 ) = 0 \lambda^2 - 15.375\lambda = 0 \implies \lambda(\lambda - 15.375) = 0 λ2−15.375λ=0⟹λ(λ−15.375)=0 λ 1 = 15.375 λ 2 = 0 \lambda_1 = 15.375 \qquad \lambda_2 = 0 λ1​=15.375λ2​=0

Find eigenvector for λ 1 = 15.375 \lambda_1 = 15.375 λ1​=15.375 (LD1):

Solve ( S W − 1 S B − 15.375   I ) w = 0 (S_W^{-1} S_B - 15.375\,I)\mathbf{w} = \mathbf{0} (SW−1​SB​−15.375I)w=0:

[ 9.375 − 15.375 7.5 7.5 6 − 15.375 ] [ v 1 v 2 ] = 0 \begin{bmatrix} 9.375 - 15.375 & 7.5 \\ 7.5 & 6 - 15.375 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \mathbf{0} [9.375−15.3757.5​7.56−15.375​][v1​v2​​]=0 [ − 6 7.5 7.5 − 9.375 ] [ v 1 v 2 ] = 0 \begin{bmatrix} -6 & 7.5 \\ 7.5 & -9.375 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \mathbf{0} [−67.5​7.5−9.375​][v1​v2​​]=0

From row 1: − 6 v 1 + 7.5 v 2 = 0    ⟹    v 2 = 6 7.5 v 1 = 0.8   v 1 -6v_1 + 7.5v_2 = 0 \implies v_2 = \dfrac{6}{7.5} v_1 = 0.8\,v_1 −6v1​+7.5v2​=0⟹v2​=7.56​v1​=0.8v1​

Setting v 1 = 1 v_1 = 1 v1​=1: v = [ 1 ,   0.8 ] T \mathbf{v} = [1,\ 0.8]^T v=[1, 0.8]T

Normalizing:

∥ v ∥ = 1 2 + 0.8 2 = 1.64 ≈ 1.281 \|\mathbf{v}\| = \sqrt{1^2 + 0.8^2} = \sqrt{1.64} \approx 1.281 ∥v∥=12+0.82 ​=1.64 ​≈1.281 w = [ 1 / 1.281 0.8 / 1.281 ] = [ 0.781 0.625 ] \mathbf{w} = \begin{bmatrix} 1/1.281 \\ 0.8/1.281 \end{bmatrix} = \begin{bmatrix} 0.781 \\ 0.625 \end{bmatrix} w=[1/1.2810.8/1.281​]=[0.7810.625​]

Interpretation: LD1 loads more heavily on x 1 x_1 x1​ than x 2 x_2 x2​, meaning x 1 x_1 x1​ contributes more to class separation.


Step 8 — Project the Data

Using w = [ 1 , 0.8 ] T \mathbf{w} = [1, 0.8]^T w=[1,0.8]T (un-normalized, as in the lecture slides):

Z = X ⋅ w = [ 1 2 2 3 3 1 6 5 7 7 8 6 ] [ 1 0.8 ] Z = X \cdot \mathbf{w} = \begin{bmatrix} 1 & 2 \\ 2 & 3 \\ 3 & 1 \\ 6 & 5 \\ 7 & 7 \\ 8 & 6 \end{bmatrix} \begin{bmatrix} 1 \\ 0.8 \end{bmatrix} Z=X⋅w= ​123678​231576​ ​[10.8​] z 1 = 1 ( 1 ) + 2 ( 0.8 ) = 1 + 1.6 = 2.6 z_1 = 1(1) + 2(0.8) = 1 + 1.6 = 2.6 z1​=1(1)+2(0.8)=1+1.6=2.6 z 2 = 2 ( 1 ) + 3 ( 0.8 ) = 2 + 2.4 = 4.4 z_2 = 2(1) + 3(0.8) = 2 + 2.4 = 4.4 z2​=2(1)+3(0.8)=2+2.4=4.4 z 3 = 3 ( 1 ) + 1 ( 0.8 ) = 3 + 0.8 = 3.8 z_3 = 3(1) + 1(0.8) = 3 + 0.8 = 3.8 z3​=3(1)+1(0.8)=3+0.8=3.8 z 4 = 6 ( 1 ) + 5 ( 0.8 ) = 6 + 4.0 = 10.0 z_4 = 6(1) + 5(0.8) = 6 + 4.0 = 10.0 z4​=6(1)+5(0.8)=6+4.0=10.0 z 5 = 7 ( 1 ) + 7 ( 0.8 ) = 7 + 5.6 = 12.6 z_5 = 7(1) + 7(0.8) = 7 + 5.6 = 12.6 z5​=7(1)+7(0.8)=7+5.6=12.6 z 6 = 8 ( 1 ) + 6 ( 0.8 ) = 8 + 4.8 = 12.8 z_6 = 8(1) + 6(0.8) = 8 + 4.8 = 12.8 z6​=8(1)+6(0.8)=8+4.8=12.8

Final Result — Lecture Example:

Sample x 1 x_1 x1​ x 2 x_2 x2​ Class LD1 Score
1 1 2 0 2.6
2 2 3 0 4.4
3 3 1 0 3.8
4 6 5 1 10.0
5 7 7 1 12.6
6 8 6 1 12.8

Conclusion: Class 0 scores cluster in [ 2.6 ,   4.4 ] [2.6,\ 4.4] [2.6, 4.4] and Class 1 scores cluster in [ 10.0 ,   12.8 ] [10.0,\ 12.8] [10.0, 12.8] — a clean separation with no overlap on LD1!


Worked Example — Quiz Problem (4 Samples, 2 Features)

Source: Ahsanullah University of Science and Technology — CSE 4113, Pattern Recognition and Machine Learning, Quiz #03.
Problem: Reduce the dimension (from 2D to 1D) of the following dataset using Linear Discriminant Analysis.


Given Data

Sample Feature x 1 x_1 x1​ Feature x 2 x_2 x2​ Class
1 1 2 0
2 3 1 0
3 6 5 1
4 8 7 1

Two classes: Class 0 = {Sample 1, Sample 2}, Class 1 = {Sample 3, Sample 4}


Step 1 — Compute Class Means

For Class 0 ( n 0 = 2 n_0 = 2 n0​=2, samples: ( 1 , 2 ) (1,2) (1,2) and ( 3 , 1 ) (3,1) (3,1)):

μ 0 = ( 1 + 3 2 ,   2 + 1 2 ) = ( 2 ,   1.5 ) \boldsymbol{\mu}_0 = \left(\frac{1+3}{2},\ \frac{2+1}{2}\right) = \left(2,\ 1.5\right) μ0​=(21+3​, 22+1​)=(2, 1.5)

For Class 1 ( n 1 = 2 n_1 = 2 n1​=2, samples: ( 6 , 5 ) (6,5) (6,5) and ( 8 , 7 ) (8,7) (8,7)):

μ 1 = ( 6 + 8 2 ,   5 + 7 2 ) = ( 7 ,   6 ) \boldsymbol{\mu}_1 = \left(\frac{6+8}{2},\ \frac{5+7}{2}\right) = \left(7,\ 6\right) μ1​=(26+8​, 25+7​)=(7, 6)

Step 2 — Compute Overall Mean

μ overall = ( 1 + 3 + 6 + 8 4 ,   2 + 1 + 5 + 7 4 ) = ( 18 4 ,   15 4 ) = ( 4.5 ,   3.75 ) \boldsymbol{\mu}_{\text{overall}} = \left(\frac{1+3+6+8}{4},\ \frac{2+1+5+7}{4}\right) = \left(\frac{18}{4},\ \frac{15}{4}\right) = (4.5,\ 3.75) μoverall​=(41+3+6+8​, 42+1+5+7​)=(418​, 415​)=(4.5, 3.75)

Step 3 — Compute Within-Class Scatter Matrix S W S_W SW​

For Class 0 — center the class data:

X 0 − μ 0 = [ 1 − 2 2 − 1.5 3 − 2 1 − 1.5 ] = [ − 1 0.5 1 − 0.5 ] X_0 - \boldsymbol{\mu}_0 = \begin{bmatrix} 1-2 & 2-1.5 \\ 3-2 & 1-1.5 \end{bmatrix} = \begin{bmatrix} -1 & 0.5 \\ 1 & -0.5 \end{bmatrix} X0​−μ0​=[1−23−2​2−1.51−1.5​]=[−11​0.5−0.5​] S 0 = ( X 0 − μ 0 ) T ( X 0 − μ 0 ) = [ − 1 1 0.5 − 0.5 ] [ − 1 0.5 1 − 0.5 ] S_0 = (X_0 - \boldsymbol{\mu}_0)^T (X_0 - \boldsymbol{\mu}_0) = \begin{bmatrix} -1 & 1 \\ 0.5 & -0.5 \end{bmatrix} \begin{bmatrix} -1 & 0.5 \\ 1 & -0.5 \end{bmatrix} S0​=(X0​−μ0​)T(X0​−μ0​)=[−10.5​1−0.5​][−11​0.5−0.5​] S 0 = [ ( − 1 ) ( − 1 ) + ( 1 ) ( 1 ) ( − 1 ) ( 0.5 ) + ( 1 ) ( − 0.5 ) ( 0.5 ) ( − 1 ) + ( − 0.5 ) ( 1 ) ( 0.5 ) ( 0.5 ) + ( − 0.5 ) ( − 0.5 ) ] = [ 2 − 1 − 1 0.5 ] S_0 = \begin{bmatrix} (-1)(-1)+(1)(1) & (-1)(0.5)+(1)(-0.5) \\ (0.5)(-1)+(-0.5)(1) & (0.5)(0.5)+(-0.5)(-0.5) \end{bmatrix} = \begin{bmatrix} 2 & -1 \\ -1 & 0.5 \end{bmatrix} S0​=[(−1)(−1)+(1)(1)(0.5)(−1)+(−0.5)(1)​(−1)(0.5)+(1)(−0.5)(0.5)(0.5)+(−0.5)(−0.5)​]=[2−1​−10.5​]

For Class 1 — center the class data:

X 1 − μ 1 = [ 6 − 7 5 − 6 8 − 7 7 − 6 ] = [ − 1 − 1 1 1 ] X_1 - \boldsymbol{\mu}_1 = \begin{bmatrix} 6-7 & 5-6 \\ 8-7 & 7-6 \end{bmatrix} = \begin{bmatrix} -1 & -1 \\ 1 & 1 \end{bmatrix} X1​−μ1​=[6−78−7​5−67−6​]=[−11​−11​] S 1 = ( X 1 − μ 1 ) T ( X 1 − μ 1 ) = [ − 1 1 − 1 1 ] [ − 1 − 1 1 1 ] S_1 = (X_1 - \boldsymbol{\mu}_1)^T (X_1 - \boldsymbol{\mu}_1) = \begin{bmatrix} -1 & 1 \\ -1 & 1 \end{bmatrix} \begin{bmatrix} -1 & -1 \\ 1 & 1 \end{bmatrix} S1​=(X1​−μ1​)T(X1​−μ1​)=[−1−1​11​][−11​−11​] S 1 = [ ( − 1 ) ( − 1 ) + ( 1 ) ( 1 ) ( − 1 ) ( − 1 ) + ( 1 ) ( 1 ) ( − 1 ) ( − 1 ) + ( 1 ) ( 1 ) ( − 1 ) ( − 1 ) + ( 1 ) ( 1 ) ] = [ 2 2 2 2 ] S_1 = \begin{bmatrix} (-1)(-1)+(1)(1) & (-1)(-1)+(1)(1) \\ (-1)(-1)+(1)(1) & (-1)(-1)+(1)(1) \end{bmatrix} = \begin{bmatrix} 2 & 2 \\ 2 & 2 \end{bmatrix} S1​=[(−1)(−1)+(1)(1)(−1)(−1)+(1)(1)​(−1)(−1)+(1)(1)(−1)(−1)+(1)(1)​]=[22​22​]

Total Within-Class Scatter:

S W = S 0 + S 1 = [ 2 − 1 − 1 0.5 ] + [ 2 2 2 2 ] = [ 4 1 1 2.5 ] \boxed{S_W = S_0 + S_1 = \begin{bmatrix} 2 & -1 \\ -1 & 0.5 \end{bmatrix} + \begin{bmatrix} 2 & 2 \\ 2 & 2 \end{bmatrix} = \begin{bmatrix} 4 & 1 \\ 1 & 2.5 \end{bmatrix}} SW​=S0​+S1​=[2−1​−10.5​]+[22​22​]=[41​12.5​]​

Step 4 — Compute Between-Class Scatter Matrix S B S_B SB​

For Class 0 ( n 0 = 2 n_0 = 2 n0​=2):

μ 0 − μ overall = [ 2 − 4.5 1.5 − 3.75 ] = [ − 2.5 − 2.25 ] \boldsymbol{\mu}_0 - \boldsymbol{\mu}_{\text{overall}} = \begin{bmatrix} 2 - 4.5 \\ 1.5 - 3.75 \end{bmatrix} = \begin{bmatrix} -2.5 \\ -2.25 \end{bmatrix} μ0​−μoverall​=[2−4.51.5−3.75​]=[−2.5−2.25​] S B 0 = 2 ⋅ [ − 2.5 − 2.25 ] [ − 2.5 − 2.25 ] = 2 ⋅ [ 6.25 5.625 5.625 5.0625 ] = [ 12.5 11.25 11.25 10.125 ] S_{B0} = 2 \cdot \begin{bmatrix} -2.5 \\ -2.25 \end{bmatrix} \begin{bmatrix} -2.5 & -2.25 \end{bmatrix} = 2 \cdot \begin{bmatrix} 6.25 & 5.625 \\ 5.625 & 5.0625 \end{bmatrix} = \begin{bmatrix} 12.5 & 11.25 \\ 11.25 & 10.125 \end{bmatrix} SB0​=2⋅[−2.5−2.25​][−2.5​−2.25​]=2⋅[6.255.625​5.6255.0625​]=[12.511.25​11.2510.125​]

For Class 1 ( n 1 = 2 n_1 = 2 n1​=2):

μ 1 − μ overall = [ 7 − 4.5 6 − 3.75 ] = [ 2.5 2.25 ] \boldsymbol{\mu}_1 - \boldsymbol{\mu}_{\text{overall}} = \begin{bmatrix} 7 - 4.5 \\ 6 - 3.75 \end{bmatrix} = \begin{bmatrix} 2.5 \\ 2.25 \end{bmatrix} μ1​−μoverall​=[7−4.56−3.75​]=[2.52.25​] S B 1 = 2 ⋅ [ 2.5 2.25 ] [ 2.5 2.25 ] = 2 ⋅ [ 6.25 5.625 5.625 5.0625 ] = [ 12.5 11.25 11.25 10.125 ] S_{B1} = 2 \cdot \begin{bmatrix} 2.5 \\ 2.25 \end{bmatrix} \begin{bmatrix} 2.5 & 2.25 \end{bmatrix} = 2 \cdot \begin{bmatrix} 6.25 & 5.625 \\ 5.625 & 5.0625 \end{bmatrix} = \begin{bmatrix} 12.5 & 11.25 \\ 11.25 & 10.125 \end{bmatrix} SB1​=2⋅[2.52.25​][2.5​2.25​]=2⋅[6.255.625​5.6255.0625​]=[12.511.25​11.2510.125​]

Total Between-Class Scatter:

S B = S B 0 + S B 1 = [ 25 22.5 22.5 20.25 ] \boxed{S_B = S_{B0} + S_{B1} = \begin{bmatrix} 25 & 22.5 \\ 22.5 & 20.25 \end{bmatrix}} SB​=SB0​+SB1​=[2522.5​22.520.25​]​

Step 5 — Compute S W − 1 S_W^{-1} SW−1​

For S W = [ 4 1 1 2.5 ] S_W = \begin{bmatrix} 4 & 1 \\ 1 & 2.5 \end{bmatrix} SW​=[41​12.5​]:

det ⁡ ( S W ) = ( 4 ) ( 2.5 ) − ( 1 ) ( 1 ) = 10 − 1 = 9 \det(S_W) = (4)(2.5) - (1)(1) = 10 - 1 = 9 det(SW​)=(4)(2.5)−(1)(1)=10−1=9 S W − 1 = 1 9 [ 2.5 − 1 − 1 4 ] = [ 0.2778 − 0.1111 − 0.1111 0.4444 ] S_W^{-1} = \frac{1}{9} \begin{bmatrix} 2.5 & -1 \\ -1 & 4 \end{bmatrix} = \begin{bmatrix} 0.2778 & -0.1111 \\ -0.1111 & 0.4444 \end{bmatrix} SW−1​=91​[2.5−1​−14​]=[0.2778−0.1111​−0.11110.4444​]

Step 6 — Compute S W − 1 S B S_W^{-1} S_B SW−1​SB​

S W − 1 S B = [ 0.2778 − 0.1111 − 0.1111 0.4444 ] [ 25 22.5 22.5 20.25 ] S_W^{-1} S_B = \begin{bmatrix} 0.2778 & -0.1111 \\ -0.1111 & 0.4444 \end{bmatrix} \begin{bmatrix} 25 & 22.5 \\ 22.5 & 20.25 \end{bmatrix} SW−1​SB​=[0.2778−0.1111​−0.11110.4444​][2522.5​22.520.25​]

Computing entry by entry:

[ 1 , 1 ] : 0.2778 × 25 + ( − 0.1111 ) × 22.5 = 6.944 − 2.500 = 4.444 [1,1]: \quad 0.2778 \times 25 + (-0.1111) \times 22.5 = 6.944 - 2.500 = 4.444 [1,1]:0.2778×25+(−0.1111)×22.5=6.944−2.500=4.444 [ 1 , 2 ] : 0.2778 × 22.5 + ( − 0.1111 ) × 20.25 = 6.250 − 2.250 = 4.000 [1,2]: \quad 0.2778 \times 22.5 + (-0.1111) \times 20.25 = 6.250 - 2.250 = 4.000 [1,2]:0.2778×22.5+(−0.1111)×20.25=6.250−2.250=4.000 [ 2 , 1 ] : ( − 0.1111 ) × 25 + 0.4444 × 22.5 = − 2.778 + 10.000 = 7.222 [2,1]: \quad (-0.1111) \times 25 + 0.4444 \times 22.5 = -2.778 + 10.000 = 7.222 [2,1]:(−0.1111)×25+0.4444×22.5=−2.778+10.000=7.222 [ 2 , 2 ] : ( − 0.1111 ) × 22.5 + 0.4444 × 20.25 = − 2.500 + 9.000 = 6.500 [2,2]: \quad (-0.1111) \times 22.5 + 0.4444 \times 20.25 = -2.500 + 9.000 = 6.500 [2,2]:(−0.1111)×22.5+0.4444×20.25=−2.500+9.000=6.500 S W − 1 S B = [ 4.444 4.000 7.222 6.500 ] \boxed{S_W^{-1} S_B = \begin{bmatrix} 4.444 & 4.000 \\ 7.222 & 6.500 \end{bmatrix}} SW−1​SB​=[4.4447.222​4.0006.500​]​

Step 7 — Find Eigenvalues

Solve det ⁡ ( S W − 1 S B − λ I ) = 0 \det(S_W^{-1} S_B - \lambda I) = 0 det(SW−1​SB​−λI)=0:

det ⁡ [ 4.444 − λ 4.000 7.222 6.500 − λ ] = 0 \det \begin{bmatrix} 4.444 - \lambda & 4.000 \\ 7.222 & 6.500 - \lambda \end{bmatrix} = 0 det[4.444−λ7.222​4.0006.500−λ​]=0 ( 4.444 − λ ) ( 6.500 − λ ) − ( 4.000 ) ( 7.222 ) = 0 (4.444 - \lambda)(6.500 - \lambda) - (4.000)(7.222) = 0 (4.444−λ)(6.500−λ)−(4.000)(7.222)=0 28.886 − 4.444 λ − 6.500 λ + λ 2 − 28.889 = 0 28.886 - 4.444\lambda - 6.500\lambda + \lambda^2 - 28.889 = 0 28.886−4.444λ−6.500λ+λ2−28.889=0 λ 2 − 10.944 λ − 0.003 ≈ 0 \lambda^2 - 10.944\lambda - 0.003 \approx 0 λ2−10.944λ−0.003≈0 λ 2 − 10.944 λ = 0    ⟹    λ ( λ − 10.944 ) = 0 \lambda^2 - 10.944\lambda = 0 \implies \lambda(\lambda - 10.944) = 0 λ2−10.944λ=0⟹λ(λ−10.944)=0 λ 1 ≈ 10.944 λ 2 ≈ 0 \lambda_1 \approx 10.944 \qquad \lambda_2 \approx 0 λ1​≈10.944λ2​≈0

Note: λ 2 ≈ 0 \lambda_2 \approx 0 λ2​≈0 is expected — with 2 classes ( C = 2 C = 2 C=2), there is only 1 meaningful discriminant direction (maximum C − 1 = 1 C - 1 = 1 C−1=1).


Step 8 — Find the Eigenvector for λ 1 ≈ 10.944 \lambda_1 \approx 10.944 λ1​≈10.944

Solve ( S W − 1 S B − λ 1 I ) w = 0 (S_W^{-1} S_B - \lambda_1 I)\mathbf{w} = \mathbf{0} (SW−1​SB​−λ1​I)w=0:

[ 4.444 − 10.944 4.000 7.222 6.500 − 10.944 ] [ v 1 v 2 ] = 0 \begin{bmatrix} 4.444 - 10.944 & 4.000 \\ 7.222 & 6.500 - 10.944 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \mathbf{0} [4.444−10.9447.222​4.0006.500−10.944​][v1​v2​​]=0 [ − 6.500 4.000 7.222 − 4.444 ] [ v 1 v 2 ] = 0 \begin{bmatrix} -6.500 & 4.000 \\ 7.222 & -4.444 \end{bmatrix} \begin{bmatrix} v_1 \\ v_2 \end{bmatrix} = \mathbf{0} [−6.5007.222​4.000−4.444​][v1​v2​​]=0

From row 1: − 6.500   v 1 + 4.000   v 2 = 0    ⟹    v 2 = 6.500 4.000 v 1 = 1.625   v 1 -6.500\,v_1 + 4.000\,v_2 = 0 \implies v_2 = \dfrac{6.500}{4.000} v_1 = 1.625\,v_1 −6.500v1​+4.000v2​=0⟹v2​=4.0006.500​v1​=1.625v1​

Setting v 1 = 1 v_1 = 1 v1​=1: v = [ 1 ,   1.625 ] T \mathbf{v} = [1,\ 1.625]^T v=[1, 1.625]T

Normalizing:

∥ v ∥ = 1 2 + 1.625 2 = 1 + 2.640625 = 3.640625 ≈ 1.908 \|\mathbf{v}\| = \sqrt{1^2 + 1.625^2} = \sqrt{1 + 2.640625} = \sqrt{3.640625} \approx 1.908 ∥v∥=12+1.6252 ​=1+2.640625 ​=3.640625 ​≈1.908 w = [ 1 / 1.908 1.625 / 1.908 ] ≈ [ 0.524 0.852 ] \mathbf{w} = \begin{bmatrix} 1/1.908 \\ 1.625/1.908 \end{bmatrix} \approx \begin{bmatrix} 0.524 \\ 0.852 \end{bmatrix} w=[1/1.9081.625/1.908​]≈[0.5240.852​]

Verification (row 2): 7.222 ( 1 ) − 4.444 ( 1.625 ) = 7.222 − 7.222 = 0 7.222(1) - 4.444(1.625) = 7.222 - 7.222 = 0 7.222(1)−4.444(1.625)=7.222−7.222=0 ✅

Interpretation: LD1 loads more on x 2 x_2 x2​ than x 1 x_1 x1​ — feature x 2 x_2 x2​ contributes more to class separation in this dataset.


Step 9 — Project the Data

Using the un-normalized projection vector w = [ 1 ,   1.625 ] T \mathbf{w} = [1,\ 1.625]^T w=[1, 1.625]T:

Z = X ⋅ w = [ 1 2 3 1 6 5 8 7 ] [ 1 1.625 ] Z = X \cdot \mathbf{w} = \begin{bmatrix} 1 & 2 \\ 3 & 1 \\ 6 & 5 \\ 8 & 7 \end{bmatrix} \begin{bmatrix} 1 \\ 1.625 \end{bmatrix} Z=X⋅w= ​1368​2157​ ​[11.625​]

Sample 1 (Class 0): z 1 = 1 ( 1 ) + 2 ( 1.625 ) = 1 + 3.25 = 4.25 z_1 = 1(1) + 2(1.625) = 1 + 3.25 = 4.25 z1​=1(1)+2(1.625)=1+3.25=4.25

Sample 2 (Class 0): z 2 = 3 ( 1 ) + 1 ( 1.625 ) = 3 + 1.625 = 4.625 z_2 = 3(1) + 1(1.625) = 3 + 1.625 = 4.625 z2​=3(1)+1(1.625)=3+1.625=4.625

Sample 3 (Class 1): z 3 = 6 ( 1 ) + 5 ( 1.625 ) = 6 + 8.125 = 14.125 z_3 = 6(1) + 5(1.625) = 6 + 8.125 = 14.125 z3​=6(1)+5(1.625)=6+8.125=14.125

Sample 4 (Class 1): z 4 = 8 ( 1 ) + 7 ( 1.625 ) = 8 + 11.375 = 19.375 z_4 = 8(1) + 7(1.625) = 8 + 11.375 = 19.375 z4​=8(1)+7(1.625)=8+11.375=19.375

Final Result — Quiz Problem:

Sample x 1 x_1 x1​ x 2 x_2 x2​ Class LD1 Score
1 1 2 0 4.250
2 3 1 0 4.625
3 6 5 1 14.125
4 8 7 1 19.375

Conclusion: Class 0 scores cluster in [ 4.250 ,   4.625 ] [4.250,\ 4.625] [4.250, 4.625] and Class 1 scores cluster in [ 14.125 ,   19.375 ] [14.125,\ 19.375] [14.125, 19.375] — near-perfect separation on a single dimension. The original 2D data is reduced to 1D while retaining maximum class discriminability!


Verification: Checking Class Separation

Projected Class Means:

m 0 = 4.250 + 4.625 2 = 4.4375 m_0 = \frac{4.250 + 4.625}{2} = 4.4375 m0​=24.250+4.625​=4.4375 m 1 = 14.125 + 19.375 2 = 16.75 m_1 = \frac{14.125 + 19.375}{2} = 16.75 m1​=214.125+19.375​=16.75 ∣ m 1 − m 0 ∣ = ∣ 16.75 − 4.4375 ∣ = 12.3125 (between-class distance in LD1 space) |m_1 - m_0| = |16.75 - 4.4375| = 12.3125 \quad \text{(between-class distance in LD1 space)} ∣m1​−m0​∣=∣16.75−4.4375∣=12.3125(between-class distance in LD1 space)

Within-class variances (in LD1 space):

σ 0 2 = ( 4.250 − 4.4375 ) 2 + ( 4.625 − 4.4375 ) 2 = 0.0352 + 0.0352 = 0.0703 \sigma_0^2 = (4.250 - 4.4375)^2 + (4.625 - 4.4375)^2 = 0.0352 + 0.0352 = 0.0703 σ02​=(4.250−4.4375)2+(4.625−4.4375)2=0.0352+0.0352=0.0703 σ 1 2 = ( 14.125 − 16.75 ) 2 + ( 19.375 − 16.75 ) 2 = 6.890625 + 6.890625 = 13.78125 \sigma_1^2 = (14.125 - 16.75)^2 + (19.375 - 16.75)^2 = 6.890625 + 6.890625 = 13.78125 σ12​=(14.125−16.75)2+(19.375−16.75)2=6.890625+6.890625=13.78125 σ within 2 = σ 0 2 + σ 1 2 = 0.0703 + 13.78125 = 13.852 \sigma_{\text{within}}^2 = \sigma_0^2 + \sigma_1^2 = 0.0703 + 13.78125 = 13.852 σwithin2​=σ02​+σ12​=0.0703+13.78125=13.852 J ( w ) = ( m 0 − m 1 ) 2 σ within 2 = ( 12.3125 ) 2 13.852 ≈ 151.60 13.852 ≈ 10.94 J(\mathbf{w}) = \frac{(m_0 - m_1)^2}{\sigma_{\text{within}}^2} = \frac{(12.3125)^2}{13.852} \approx \frac{151.60}{13.852} \approx 10.94 J(w)=σwithin2​(m0​−m1​)2​=13.852(12.3125)2​≈13.852151.60​≈10.94

This matches our leading eigenvalue λ 1 ≈ 10.944 \lambda_1 \approx 10.944 λ1​≈10.944. ✅

The eigenvalue λ 1 \lambda_1 λ1​ is exactly the Fisher criterion value J ( w ) J(\mathbf{w}) J(w), confirming the solution is correct.


Geometric Intuition

LDA rotates and re-orients the coordinate system so that the new axis maximally separates the classes, rather than simply capturing the most variance (as PCA does).

The Projection Analogy

Think of projecting a 2D scatter plot down onto a 1D line — like shining a flashlight at the data and looking at the shadow. The question LDA answers is: what angle should the flashlight be at so the two class-shadows land as far apart as possible?

The Geometry of LDA projects data to maximize class separation

  • Bad projection (left panel): Projecting onto the vertical axis ( x 2 x_2 x2​) mixes the two classes together. Their projections heavily overlap — it is impossible to draw a clean boundary on this 1D line to separate them.

  • Good LDA projection (right panel): The optimal direction w \mathbf{w} w (LD1, green dashed line) is found by LDA. When data is projected perpendicularly onto this axis, Class 0 (blue) and Class 1 (coral) land in two compact, well-separated clusters with a clear gap between them.

What LDA Is Simultaneously Optimising

The green axis achieves two things at once:

  1. Maximizes between-class scatter S B S_B SB​ — the projected class means m 0 m_0 m0​ and m 1 m_1 m1​ are pushed as far apart as possible along w \mathbf{w} w.
  2. Minimizes within-class scatter S W S_W SW​ — each class's own points are pulled as tightly together as possible around their projected mean.

This simultaneous optimization is captured by the Fisher criterion:

J ( w ) = σ between 2 σ within 2 = w T S B w w T S W w J(\mathbf{w}) = \frac{\sigma^2_{\text{between}}}{\sigma^2_{\text{within}}} = \frac{\mathbf{w}^T S_B \mathbf{w}}{\mathbf{w}^T S_W \mathbf{w}} J(w)=σwithin2​σbetween2​​=wTSW​wwTSB​w​

The projection direction w \mathbf{w} w that maximizes J ( w ) J(\mathbf{w}) J(w) is exactly the eigenvector of S W − 1 S B S_W^{-1} S_B SW−1​SB​ corresponding to its largest eigenvalue. The eigenvalue itself equals the maximum value of J ( w ) J(\mathbf{w}) J(w), confirming that LDA finds the theoretically optimal projection.

Summary: LDA does not merely pick the axis of greatest spread. It picks the axis that makes the class "shadows" as non-overlapping as possible — maximizing the gap between class means while minimizing the blur within each class.

LDA as a Classifier

Once the projection vector w \mathbf{w} w is found, LDA can classify new data points.

Classification Steps

Step 1 — Project the new data point:

z new = w T x new z_{\text{new}} = \mathbf{w}^T \mathbf{x}_{\text{new}} znew​=wTxnew​

Step 2 — Compute projected class means:

m i = w T μ i for each class  i m_i = \mathbf{w}^T \boldsymbol{\mu}_i \quad \text{for each class } i mi​=wTμi​for each class i

Step 3 — Assign to the nearest class:

For 1D projection:

Class = arg ⁡ min ⁡ i ∣ z new − m i ∣ \text{Class} = \arg\min_i |z_{\text{new}} - m_i| Class=argimin​∣znew​−mi​∣

For multi-dimensional projections (multiple LDs):

Class = arg ⁡ min ⁡ i ∑ j ( z new , j − m i j ) 2 \text{Class} = \arg\min_i \sqrt{\sum_j (z_{\text{new},j} - m_{ij})^2} Class=argimin​j∑​(znew,j​−mij​)2 ​

Classifier Example (from lecture)

Using w = [ 1 , 0.8 ] T \mathbf{w} = [1, 0.8]^T w=[1,0.8]T and a new point x new = ( 5 , 4 ) \mathbf{x}_{\text{new}} = (5, 4) xnew​=(5,4):

z new = 1 ⋅ 5 + 0.8 ⋅ 4 = 5 + 3.2 = 8.2 z_{\text{new}} = 1 \cdot 5 + 0.8 \cdot 4 = 5 + 3.2 = 8.2 znew​=1⋅5+0.8⋅4=5+3.2=8.2

Projected class means (from Step 8):

m 0 = 2.6 + 4.4 + 3.8 3 = 10.8 3 = 3.6 m 1 = 10 + 12.6 + 12.8 3 = 35.4 3 = 11.8 m_0 = \frac{2.6 + 4.4 + 3.8}{3} = \frac{10.8}{3} = 3.6 \qquad m_1 = \frac{10 + 12.6 + 12.8}{3} = \frac{35.4}{3} = 11.8 m0​=32.6+4.4+3.8​=310.8​=3.6m1​=310+12.6+12.8​=335.4​=11.8

Distances:

∣ z new − m 0 ∣ = ∣ 8.2 − 3.6 ∣ = 4.6 |z_{\text{new}} - m_0| = |8.2 - 3.6| = 4.6 ∣znew​−m0​∣=∣8.2−3.6∣=4.6 ∣ z new − m 1 ∣ = ∣ 8.2 − 11.8 ∣ = 3.6 |z_{\text{new}} - m_1| = |8.2 - 11.8| = 3.6 ∣znew​−m1​∣=∣8.2−11.8∣=3.6

Since 8.2 8.2 8.2 is closer to m 1 = 11.8 m_1 = 11.8 m1​=11.8, the point is classified as Class 1. ✅


Limitations of LDA

Limitation Description Alternative
Linearity Only finds linear decision boundaries Kernel LDA, QDA
Max discriminants Limited to C − 1 C - 1 C−1 components PCA for more components
Gaussian assumption Assumes classes are normally distributed QDA (different covariances)
Equal covariance Assumes all classes share the same covariance QDA for unequal covariances
Small sample problem S W S_W SW​ may be singular if n < p n < p n<p Regularized LDA (RLDA)
Sensitive to outliers Means and scatter matrices affected by outliers Robust LDA
Multicollinear features S W S_W SW​ becomes ill-conditioned PCA pre-processing + LDA

Complete Formula Reference

Formula Expression
Projection z = w T x z = \mathbf{w}^T \mathbf{x} z=wTx
Class mean μ i = 1 n i ∑ x ∈ C i x \boldsymbol{\mu}_i = \dfrac{1}{n_i} \sum_{\mathbf{x} \in C_i} \mathbf{x} μi​=ni​1​∑x∈Ci​​x
Overall mean μ overall = 1 n ∑ j = 1 n x j \boldsymbol{\mu}_{\text{overall}} = \dfrac{1}{n} \sum_{j=1}^n \mathbf{x}_j μoverall​=n1​∑j=1n​xj​
Within-class scatter S W = ∑ i ( X i − μ i ) T ( X i − μ i ) S_W = \sum_i (X_i - \boldsymbol{\mu}_i)^T (X_i - \boldsymbol{\mu}_i) SW​=∑i​(Xi​−μi​)T(Xi​−μi​)
Between-class scatter S B = ∑ i n i ( μ i − μ overall ) ( μ i − μ overall ) T S_B = \sum_i n_i (\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})(\boldsymbol{\mu}_i - \boldsymbol{\mu}_{\text{overall}})^T SB​=∑i​ni​(μi​−μoverall​)(μi​−μoverall​)T
Generalized eigenvalue problem S W − 1 S B   w = λ w S_W^{-1} S_B \,\mathbf{w} = \lambda \mathbf{w} SW−1​SB​w=λw
Fisher criterion J ( w ) = w T S B w w T S W w J(\mathbf{w}) = \dfrac{\mathbf{w}^T S_B \mathbf{w}}{\mathbf{w}^T S_W \mathbf{w}} J(w)=wTSW​wwTSB​w​
Classification rule Class = arg ⁡ min ⁡ i ∥ z new − m i ∥ \text{Class} = \arg\min_i \|z_{\text{new}} - m_i\| Class=argmini​∥znew​−mi​∥
Max discriminants k ≤ C − 1 k \leq C - 1 k≤C−1

Summary

DATA (n × p) with class labels
         │
         ▼
COMPUTE CLASS MEANS  μᵢ for each class i
         │
         ▼
COMPUTE OVERALL MEAN  μ_overall
         │
         ▼
WITHIN-CLASS SCATTER: S_W = Σᵢ (Xᵢ - μᵢ)ᵀ(Xᵢ - μᵢ)
         │
         ▼
BETWEEN-CLASS SCATTER: S_B = Σᵢ nᵢ(μᵢ - μ_overall)(μᵢ - μ_overall)ᵀ
         │
         ▼
COMPUTE  S_W⁻¹ S_B
         │
         ▼
EIGENDECOMPOSITION: S_W⁻¹ S_B w = λw
         │
         ▼
SORT eigenpairs: λ₁ ≥ λ₂ ≥ ... ≥ λ_{C-1}
         │
         ▼
SELECT top-k discriminants: W_k = [w₁ | w₂ | ... | wₖ]
         │
         ▼
PROJECT: Z = X · W_k  (n × k)
         │
         ▼
REDUCED DATA (n × k), maximally class-separable

Key Takeaways

  1. LDA is supervised — it uses class labels to guide dimensionality reduction
  2. Eigenvalue = Fisher criterion value — λ i = J ( w i ) \lambda_i = J(\mathbf{w}_i) λi​=J(wi​), the actual between/within ratio
  3. Max C − 1 C - 1 C−1 components — for binary classification, there is only ever 1 discriminant direction
  4. S W S_W SW​ must be invertible — if singular, use regularization or PCA pre-processing
  5. LDA ≠ PCA — LDA maximizes class separation; PCA maximizes total variance and ignores labels
  6. Classification by nearest projected mean — after projecting a new point onto LD1 (or LD space), assign it to the class whose projected mean is closest
  7. Eigenvalue of S W − 1 S B S_W^{-1} S_B SW−1​SB​ — not of the covariance matrix; the matrix to decompose is S W − 1 S B S_W^{-1} S_B SW−1​SB​, not Σ \Sigma Σ