fisher's linear discriminant in Python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Fisher's Linear Discriminant is a supervised dimensionality-reduction method that projects data onto a direction designed to separate classes as much as possible. In Python, you can implement the two-class version directly with NumPy or use a library implementation such as scikit-learn's LDA when you want a production-ready tool.
The Core Idea
For two classes, Fisher's method looks for a projection vector w that makes class means far apart after projection while keeping samples within each class tightly clustered.
The classic result is:
where:
- '
mu1andmu2are the class means,' - and
S_wis the within-class scatter matrix.
The projection does not try to preserve raw variance the way PCA does. It tries to preserve class separation.
Build the Scatter Matrices
Suppose X1 and X2 are the samples from two classes. Then:
- compute the mean of each class,
- center each class around its own mean,
- accumulate the within-class scatter,
- and solve for the projection direction.
Here is a minimal NumPy implementation:
Using np.linalg.solve is usually better than explicitly computing a matrix inverse.
Project the Data
Once you have w, project each sample onto that line:
If the classes are well separated in the original space, their projected values should form distinct ranges or at least become easier to classify with a threshold.
Turn It Into a Simple Classifier
For a two-class toy example, one simple classifier uses the midpoint between projected class means as a threshold.
This is not the most sophisticated classifier, but it makes Fisher's method concrete: first find a discriminative direction, then classify based on projected position.
Relationship to LDA
In practice, Fisher's Linear Discriminant is closely related to Linear Discriminant Analysis. Scikit-learn wraps the broader method in a familiar API:
If your goal is practical classification, this is usually the best route. If your goal is understanding the method, the NumPy version is more instructive.
Numerical Issues Matter
If the within-class scatter matrix is singular or poorly conditioned, the direct solve may become unstable. That happens when:
- features are highly collinear,
- there are more features than samples,
- or one feature is a linear combination of others.
Common fixes include regularization, dimensionality reduction before LDA, or using a library implementation that already handles these cases more carefully.
Fisher Versus PCA
This comparison matters because the two are often confused:
- PCA ignores labels and preserves variance
- Fisher's method uses labels and preserves class separation
A direction with large variance is not always the direction that best separates two classes. That is exactly why Fisher's method exists.
Common Pitfalls
The biggest pitfall is applying Fisher's method as if it were just another unsupervised projection technique. It needs class labels and uses them directly.
Another mistake is explicitly inverting S_w with np.linalg.inv instead of solving the system. Direct inversion is often less numerically stable.
Developers also sometimes expect perfect separation even when the classes overlap substantially. Fisher's method finds the best linear projection under the model, not a magic separator for arbitrary data.
Finally, if the feature count is large relative to the sample count, watch for singular matrices and consider regularization or library implementations.
Summary
- Fisher's Linear Discriminant finds a projection that maximizes class separation relative to within-class spread.
- For two classes, the core computation is
w = S_w^(-1) (mu1 - mu2). - A NumPy implementation is straightforward and useful for learning the method.
- Scikit-learn's
LinearDiscriminantAnalysisis the practical choice for real projects. - Numerical stability and class overlap are the main practical issues to watch.
Related reading
- Fit mixture of Gaussians with fixed covariance in Python
- Fit model to all variables in Python Scikit Learn
- Fit multivariate gaussian distribution to a given dataset
- Fitting a line that passes through the origin 0,0 to data
- Fitting data vs. transforming data in scikit-learn
- Fitting MultinomialNB on multiple columns of data
- Fitting an unknown curve
- Flask and Keras model Error ''_thread._local' object has no attribute 'value''?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.