Machine Learning Series: Dimensionality Reduction
Hello Reader,
When we deal with machine learning we work with enormous datasets composed of hundreds and sometimes thousands of features. Dealing with hundreds of dimension create a few challenges. As humans we are used to three-dimensional space, navigate in a multi-dimensional space can give us seasickness. Another key issue to consider is the course of dimensionality.
![]() |
| https://en.wikipedia.org/wiki/File:Dimension_levels.svg |
Curse of dimensionality
If we pick two random points in a unit square in two dimensions on the average distance between the points will be roughly .52, while in three dimensions will be .66, and in 1,000,000 dimensions will be more than 400.
The problem is that in high dimensional spaces are plenty of space, a dataset with a large number of features tends to be sparse leading to overfitting. One solution is to get more training samples to increase the density of the training instances or decrease the number of features.
The good news is that some algorithms can help us to reduce the dimensions of a dataset, let's see how
Principal Component Analysis
PCA is the most utilized technique for dimensionality reduction, it looks for the plane that retains the maximum amount of variance and projects the points into it. Iteratively looks for a second axis orthogonal to the first that retains the maximum amount of variance. The ith axis is called the ith Principal Component. How can we found the PC?, luckily linear algebra comes to rescue with a matrix factorization technique called Singular Value Decomposition. SVD decomposes a matrix X in UΣVT components V contains our PCs.
Let's see it in action, we have a dataset in three dimensions, we want to use PCA to draw it in two dimensions
First I am going to calculate the Single Value Decomposition function of the dataset. Note to make sure the SVD works we need to center the dataset at the origin
C1 and C2 contain the two components we need to represent in 2 dimensions. The two components represent the two planes on which the algorithm will perform the projection as shown in the picture below. First, we need to center the dataset at the origin then we can calculate the Single Value Decomposition function for the dataset.
Let's now project the points on the two axes
And let's plot the final result in 2D
That's a stunning result.
Data Compression
One of the main applications of PCA is data compression, let's see an example using a famous MNIST dataset.
The dataset is composed of a set of handwritten digits, each digit has a size of 28 x 28 which translates to 784 features for each training instance. We will train a classification algorithm to see how it performs on the full dataset and how long does it takes.
We used perceptron as a classification algorithm, it takes on average 16 seconds to train on 70000 images with 89% accuracy. Let's compress the dataset with PCA. First, we instantiate the PCA object from Sklearn using all 784 dimensions in other words we are not compressing anything.
Let's analyze the function explained_variance_ratio, it returns the amount of variance captured by each PC (e.g. the first component collects 9% of the total variance of the dataset). Now let's instantiate the PCA object configured to collect a total of 95% of the total variance of the dataset.
To retain 95% of the variance we need just 154 dimensions, now let's retrain the model with the compressed dataset to see the performance
We were able to train the algorithm in 3 seconds, way faster than the previous attempts, the accuracy is 86% slightly below the model trained with the full set of features.
Another application of the Dimensionality reduction algorithms is the visualization of high-dimensional datasets. Let's reduce the dimensionality with PCA to speed up T-SNE (t-Distributed Stochastic Neighbor Embedding) to visualize the MNIST dataset in two dimensions
Here is the final result, we can see how the various digits are grouped in well-defined clusters, it's astonishing to see how T-SNE was able to paint on a 2d canvas a set of points from a 748-dimensional space.
Recommended resources















Comments
Post a Comment