Linear Discriminant Analysis (LDA)

๐Ÿ“– Introduction

Linear Discriminant Analysis (LDA) is a supervised dimensionality reduction and classification algorithm that projects data onto a lower-dimensional space while maximizing the separation between different classes. Unlike Principal Component Analysis (PCA), which focuses on preserving variance, LDA focuses on improving class separability.

Information

LDA uses class labels during training, making it a supervised learning technique. It is commonly used as both a preprocessing method for dimensionality reduction and as a classifier.

๐ŸŽฏ Learning Objectives

  • Understand Linear Discriminant Analysis.
  • Learn the difference between PCA and LDA.
  • Understand within-class and between-class scatter.
  • Apply LDA for dimensionality reduction and classification.

๐ŸŒŸ What is Linear Discriminant Analysis?

Linear Discriminant Analysis transforms the original feature space into a lower-dimensional space where observations belonging to different classes become as distinct as possible. It achieves this by maximizing the distance between class means while minimizing the spread of observations within each class.

CharacteristicLinear Discriminant Analysis
Learning TypeSupervised
Main ObjectiveMaximize Class Separation
Uses Class LabelsYes
ApplicationsClassification & Dimensionality Reduction

๐Ÿ“ Key Concepts

ConceptDescription
Class MeanAverage feature vector for each class.
Within-Class ScatterVariation among observations within the same class.
Between-Class ScatterVariation between different class means.
Linear DiscriminantProjection direction that maximizes class separation.
Discriminant ComponentsNew transformed features obtained after projection.

๐Ÿ“Š Within-Class Scatter Matrix

The within-class scatter matrix measures the spread of observations inside each class.

Where:

  • Cแตข โ€” Class i.
  • ฮผแตข โ€” Mean vector of class i.
  • c โ€” Number of classes.

๐Ÿ“ˆ Between-Class Scatter Matrix

The between-class scatter matrix measures how far apart different class means are.

Where:

  • Nแตข โ€” Number of observations in class i.
  • ฮผ โ€” Overall dataset mean.

Remember

Good class separation is achieved by maximizing the between-class scatter while minimizing the within-class scatter.

๐ŸŽฏ LDA Optimization Objective

LDA finds projection directions that maximize the ratio of between-class scatter to within-class scatter.

The optimal projection vector maximizes this objective function.

โš™๏ธ How LDA Works

๐ŸŒณ LDA Workflow

Input Dataset with Labels
Compute Class Means
Calculate Scatter Matrices
Solve Eigenvalue Problem
Select Linear Discriminants
Transform Dataset

๐Ÿ“Š PCA vs LDA

FeaturePCALDA
Learning TypeUnsupervisedSupervised
Uses Class LabelsNoYes
Main ObjectiveMaximize VarianceMaximize Class Separation
Output ComponentsPrincipal ComponentsLinear Discriminants
Typical UseFeature ExtractionClassification & Feature Extraction

๐Ÿ“Š Number of Components

Unlike PCA, the maximum number of linear discriminants is limited by the number of classes.

Where:

  • C โ€” Number of classes.

Tip

For a dataset containing three classes, LDA can produce at most two discriminant components.

๐ŸŽ›๏ธ Important Hyperparameters

HyperparameterDescription
solverAlgorithm used to compute discriminants.
n_componentsNumber of discriminant components.
shrinkageRegularization for covariance estimation.
store_covarianceStores covariance matrix after training.

๐Ÿ“Š Evaluation Metrics

  • Accuracy
  • Precision
  • Recall
  • F1-Score
  • ROC-AUC
  • Confusion Matrix
  • Class Separability
  • Visualization Quality
  • Downstream Model Performance

โš–๏ธ Advantages and Limitations

  • Excellent class separation.
  • Reduces dimensionality while preserving discriminative information.
  • Simple and computationally efficient.
  • Works well when class distributions are approximately Gaussian.
  • Improves many classification algorithms.
  • Requires labeled training data.
  • Assumes classes have similar covariance matrices.
  • Assumes approximately Gaussian class distributions.
  • Maximum components are limited to C โˆ’ 1.
  • Less effective when assumptions are strongly violated.

๐ŸŒ Real-World Applications

ApplicationPurpose
๐Ÿฉบ Medical DiagnosisSeparate patients into diagnostic groups.
๐Ÿ˜Š Face RecognitionReduce image dimensions while preserving identity information.
๐Ÿ“ง Spam DetectionImprove document classification.
๐Ÿ’ณ Credit Risk AnalysisClassify loan applicants.
๐Ÿงฌ BioinformaticsClassify biological samples.
๐ŸŽค Speech RecognitionExtract discriminative acoustic features.

๐Ÿ’ป Practical Example

Linear Discriminant Analysis Using Scikit-learn

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.datasets import load_iris

# Load dataset
X, y = load_iris(return_X_y=True)

# Create LDA model
lda = LinearDiscriminantAnalysis(
    n_components=2
)

# Transform data
X_lda = lda.fit_transform(X, y)

print("Transformed Shape:", X_lda.shape)

# Classification
predictions = lda.predict(X)

print("Predictions:")
print(predictions[:10])

โš ๏ธ Common Mistakes

  • Using LDA without labeled training data.
  • Confusing Linear Discriminant Analysis with Latent Dirichlet Allocation (both abbreviated as LDA).
  • Ignoring violations of Gaussian distribution assumptions.
  • Expecting more than C โˆ’ 1 discriminant components.
  • Applying LDA when classes are highly nonlinear and not linearly separable.

Best Practice

Standardize numerical features when appropriate, verify that class distributions are reasonably Gaussian with similar covariance structures, choose n_components โ‰ค C โˆ’ 1, and use LDA as both a dimensionality reduction technique and a classifier when maximizing class separation is more important than preserving overall variance.

๐Ÿ“š Summary

Summary

Linear Discriminant Analysis (LDA) is a supervised dimensionality reduction and classification technique that projects data into a lower-dimensional space by maximizing the separation between classes. Unlike PCA, which preserves variance without using labels, LDA incorporates class information to produce highly discriminative features. Because of its efficiency, interpretability, and strong classification performance under appropriate assumptions, LDA is widely used in medical diagnosis, face recognition, bioinformatics, finance, and speech recognition.

๐Ÿ”— Further Reading