by Thalles Silva

# An illustrative introduction to Fisher’s Linear Discriminant

To deal with classification problems with two or more classes, most Machine Learning (ML) algorithms work the same way.

Usually, they apply some kind of transformation to the input data with the effect of reducing the original input dimensions to a smaller number. The goal is to project the data into a new space. Then, once projected, the algorithm tries to classify the points by finding a linear separation.

For problems with small input dimensions, the task is somewhat easier. Take the following dataset as an example.

Suppose we want to classify the red and blue circles correctly.

It is clear that with a simple linear model we will not get a good result. There is no linear combination of the inputs and weights that maps the inputs to their correct classes. But what if we could transform the data so that we could draw a line that separates the two classes?

That is what happens if we square the two input feature-vectors. Now, a linear model will easily classify the blue and red points.

However, sometimes we do not know which kind of transformation we should use. Actually, to find the best representation is not a trivial problem. There are many transformations we could apply to our data. Likewise, each one of them could result in a different classifier (in terms of performance).

One solution to this problem is to learn the right transformation. This is known as ** representation learning** and it is exactly what Deep Learning algorithms do.

The magic is that we do not need to “guess” what kind of transformation would result in the best representation of the data. The algorithm will figure it out.

However, keep in mind that regardless of representation learning or hand-crafted transformations, the pattern is the same. We need to change the data somehow so that it can be easily separable.

Let’s take some steps back and consider a simpler problem.

We are going to explore how ** Fisher’s Linear Discriminant** (FLD) manages to classify multi-dimensional data to multiple classes. But before we begin, feel free to open this Colab notebook and follow along.

# Fisher’s Linear Discriminant

One way of viewing classification problems is through the lens of dimensionality reduction.

To begin, consider the case of a two-class classification problem (** K=2**). Blue and red points in

**. In general, we can take any D-dimensional input vector and project it down to D’-dimensions. Here,**

**R²****represents the original input dimensions while**

**D****is the projected space dimensions. Throughout this article, consider**

**D’****less than**

**D’****.**

**D**In the case of projecting to one dimension (the number line), i.e. ** D’=1**, we can pick a threshold

**to separate the classes in the new space. Given an input vector**

**t****:**

**x**- if the predicted value
then,**y >= t**belongs to class C1 (class 1).**x** - otherwise, it is classified as C2 (class 2).

Note that the vector ** y** (predictions)

**is equal to the linear combination o inputs**

**and weights**

**x****=**

**W → y****ᵀ**

**W**

**x.**Take the dataset below as a toy example. We want to reduce the original data dimensions from ** D=2** to

**. In other words, we want a transformation T that maps vectors in 2D to 1D — T(**

**D’=1****) = ℝ² →ℝ¹.**

**v**First, let’s compute the mean vectors ** m1** and

**for the two classes.**

**m2**Note that ** N1** and

**denote the number of points in classes C1 and C2 respectively. Now, consider using the class means as a measure of separation. In other words, we want to project the data onto the vector**

**N2****joining the 2 class means.**

**W**It is important to note that any kind of projection to a smaller dimension might involve some loss of information. In this scenario, note that the two classes are clearly separable (by a line) in their original space.

However, after re-projection, the data exhibit some sort of class overlapping — shown by the yellow ellipse on the plot and the histogram below.

That is where the Fisher’s Linear Discriminant comes into play.

The idea proposed by Fisher is to maximize a function that will give a large separation between the projected class means, while also giving a small variance within each class, thereby minimizing the class overlap.

In other words, FLD selects a projection that maximizes the class separation. To do that, it maximizes the ratio between the between-class variance to the within-class variance.

In short, to project the data to a smaller dimension and to avoid class overlapping, FLD maintains 2 properties.

- A large variance among the dataset classes.
- A small variance within each of the dataset classes.

Note that a large between-class variance means that the projected class averages should be as far apart as possible. On the contrary, a small within-class variance has the effect of keeping the projected data points closer to one another.

To find the projection with the following properties, FLD learns a weight vector ** W** with the following criterion.

If we substitute the mean vectors ** m1** and

**as well as the variance**

**m2****as given by equations (1) and (2) we arrive at equation (3). If we take the derivative of (3) with respect to**

**s****(after some simplifications), we get the learning equation for**

**W****(equation 4).**

**W**That is, ** W **(our desired transformation)

**is directly proportional to the inverse of the**

**matrix times the difference of the class means.**

**within-class covariance**As expected, the result allows a perfect class separation with simple thresholding.

## Fisher’s Linear Discriminant for Multiple Classes

We can generalize FLD for the case of more than ** K>2** classes. Here, we need generalization forms for the

**and**

**within-class****covariance matrices.**

**between-class**For the within-class covariance matrix ** SW**, for each class, take the sum of the matrix-multiplication between the centralized input values and their transpose. Equations 5 and 6.

For estimating the between-class covariance ** SB**, for each class

**, take the**

**k=1,2,3,…,K****of the local class mean**

**outer product****and global mean**

**mk****Then, scale it by the number**

**m.****of records in class**

**— equation 7.**

**k**The maximization of the FLD criterion is solved via an eigendecomposition of the matrix-multiplication between the inverse of ** SW** and

**. Thus, to find the weight vector**

**SB****, we take the**

**W****eigenvectors that correspond to their largest eigenvalues (equation 8).**

**D’**In other words, if we want to reduce our data dimensions from ** D=784** to

**, the transformation vector**

**D’=2****is composed of the 2 eigenvectors that correspond to the**

**W****largest eigenvalues. This gives a final shape of**

**D’=2****, where**

**W = (N,D’)****is the number of input records and**

**N****the reduced feature space dimensions.**

**D’**## Building a linear discriminant

Up until this point, we used Fisher’s Linear discriminant only as a method for dimensionality reduction. To really create a discriminant, we can model a ** multivariate Gaussian distribution** over a D-dimensional input vector

**for each class**

**x****as:**

**K**Here **μ** (the mean) is a D-dimensional vector.

**(sigma) is a**

**Σ****matrix — the covariance matrix. And |**

**DxD****| is the determinant of the covariance. The determinant is a measure of how much the covariance matrix**

**Σ****stretches or shrinks space.**

**Σ**In Python, it looks like this.

The parameters of the Gaussian distribution: **μ **and

**are computed for each class**

*Σ,***using the projected input data**

**k=1,2,3,…,K****We can infer the priors**

**.***class probabilities using the fractions of the training set data points in each of the classes (line 11).*

*P(Ck)*Once we have the Gaussian parameters and priors, we can compute class-conditional densities *P(**x** |Ck)* for each class

**individually. To do it, we first project the D-dimensional input vector**

**k=1,2,3,…,K****to a new**

**x****space. Keep in mind that**

**D’****. Then, we evaluate equation 9 for each projected point. Finally, we can get the posterior class probabilities**

**D’ < D**

*P(Ck|*

*x**for each class*

*)***using equation 10.**

**k=1,2,3,…,K**Equation 10 is evaluated on line 8 of the score function below.

We then can assign the input vector ** x** to the class

**with the largest posterior.**

**k**## Testing on MNIST

Using MNIST as a toy testing dataset. If we choose to reduce the original input dimensions ** D=784 **to

**we get around**

**D’=2***accuracy on the test data. If we increase the projected space dimensions to*

*56%***, however, we reach nearly**

**D’=3***accuracy. These 2 projections also make it easier to visualize the resulting feature space.*

*74%*Some key takeaways from this piece.

- Fisher’s Linear Discriminant, in essence, is a technique for dimensionality reduction, not a discriminant.
- For binary classification, we can find an optimal threshold
and classify the data accordingly.**t** - For multiclass data, we can (1) model a class conditional distribution using a Gaussian. (2) Find the prior class probabilities
and (3) use*P(Ck),*to find the posterior class probabilities**Bayes**.*p(Ck|x)* - To find the optimal direction to project the input data, Fisher needs supervised data.
- Given a dataset with
dimensions, we can project it down to**D****at most**equals to**D’**dimensions.**D-1**

This article is based on ** chapter 4.1** of Pattern Recognition and Machine Learning. Book by Christopher Bishop.

**Thanks for reading.**