Exploring Dimensionality Reduction with PCA on MNIST Dataset

This video explains how Principal Component Analysis reduces high-dimensional data while preserving variance, then implements it in Python on MNIST digits.

6:07 video3 min readWatch on YouTube

High-dimensional datasets like MNIST come with a real cost: too many features can lead to overfitting and noisy models, a problem known as the curse of dimensionality. This video works through Principal Component Analysis, or PCA, one of the most widely used techniques for addressing that problem, explaining both the mathematical mechanics and a full Python implementation on the MNIST dataset.

What dimensionality reduction solves

Dimensionality reduction is the process of simplifying complex datasets by reducing the number of features while retaining essential information. The motivation is practical: an excessive number of features can lead to overfitting and models that pick up on noise rather than real signal. PCA is introduced as a linear dimensionality reduction technique whose primary goal is transforming a high-dimensional dataset into a lower-dimensional representation while preserving most of its variance.

The mechanics of PCA, step by step

The video breaks PCA down into a clear sequence of steps. First, the features of the dataset are standardized, setting the mean to zero and the standard deviation to one, so that every feature contributes equally to the resulting principal components. Next, the covariance matrix of the standardized dataset is computed, summarizing the relationships and variances between features. From there, eigenvalue decomposition is performed on the covariance matrix, producing eigenvalues and eigenvectors, which are the principal components themselves.

The next step is principal component selection: choosing the top K eigenvectors that correspond to the largest eigenvalues, since these capture the most variance in the data. Finally, the original data is projected onto the selected principal components, producing the lower-dimensional representation that PCA set out to build.

Why use PCA at all

The case for PCA comes back to the curse of dimensionality. Larger feature spaces can lead to overfitting, and PCA addresses this by reducing dimensions while retaining the critical information in the data. Because it captures variance using fewer dimensions, the aim throughout is to retain as much of the original variance as possible even as the number of features shrinks dramatically.

Implementing PCA on MNIST in Python

The implementation starts by importing the necessary packages and loading the dataset from scikit-learn's datasets module, then converting it into a pandas DataFrame. Columns with zero variance are removed, the dataset is converted into a subset, and pixel values are normalized. The PCA process itself follows the same steps outlined conceptually: standardizing the mean and variance, computing the covariance matrix, performing eigenvalue decomposition to get eigenvalues and eigenvectors, and selecting the top K eigenvectors to project the data onto.

When calling the PCA method, the number of components to decompose into has to be specified. In this implementation, two components are used, producing a final visualization of the MNIST dataset reduced down to just two dimensions.

What the result shows

The conclusion drawn from the exercise is straightforward: PCA is a valuable tool for simplifying complex datasets, improving model performance, and surfacing insights that would be harder to see in the original high-dimensional space.

Key takeaways

  • PCA is a linear dimensionality reduction technique that reduces feature count while preserving most of the variance in the data.
  • The process involves standardization, computing a covariance matrix, eigenvalue decomposition, selecting top eigenvectors, and projecting the data.
  • PCA directly addresses the curse of dimensionality, which can otherwise lead to overfitting in models with too many features.
  • The MNIST implementation reduces the dataset down to two principal components for visualization.
  • PCA's linear nature is a limitation for datasets with complex, nonlinear structure, which is where a technique like t-SNE comes in.

Who this is for

This video is aimed at learners building foundational skills in unsupervised machine learning and data preprocessing. It fits into a broader Humanitarians AI series on the MNIST dataset that also covers clustering and nonlinear dimensionality reduction with t-SNE.

Full transcript(auto-generated, with timestamps)

[0:01]Hello everyone last week we explored C's clustering and briefly touched upon TSN as dimensionality reduction technique and today let's tell deeper into the comp concept of dimensionality reduction and specifically focus on PC which is principal component analysis first basically let's understand what is dimensionality reduction dimensionality reduction is the process of simplifying complex data sets by reducing the number of features while retaining essential information it helps us address the Cur of dimensionality where an excessive number of features can lead to overfitting and noisy models and uh what is the working

[1:12]Principle of PCA PCA is a widely used linear dimensionality reduction technique its primary goal is to transform a high dimensional data set into a lower dimensional representation ation while preserving most of its variance first in order to implement PCA we have to start with standardization where we start by standardizing the features of a data set like standardizing the mean or standard deviation where mean should be to zero and standard deviation to one so that this ensures that each feature contributes equally to the composition of principal components then we try to compute The cence Matrix of the standardized data set the co-variance Matrix summarizes relationship between the features and

[2:23]Their variances after that we try to perform igen value decompos position on our Co covariance Matrix this yields igen values and igen vectors which are the principal components then principal component selection is done where we select the top K IG vectors corresponding to the largest igon values these igen vectors form the principal components they capture most of the variance of the data and finally we project the original data onto the selected principal components this will result us with the lower dimensional representation why are we using PC because uh as I have mentioned earlier the Cur of dimensionality which means that the larger feature spaces can

[3:32]Lead to overfitting and PCA reduces Dimensions while retaining critical information and also PCA captures variance using fewer Dimensions so we aim to retain as much variance as possible now since we understood what is PCA and how that works let us implement it in python this is the implementation of PCA on Mist data set first we have to import all the necessary packages and then load the data set from SK learn data sets then convert the data set into the Panda's data frame from there we have to remove columns with zero variance and then then uh convert the data set into a subset and also normalize the pixel

[4:38]Values then in the PCA uh method we have to first standardize the data that is uh standardize the mean and variance and then compute the covariance matx and from co-variance matx we have to do the I value decom position from which we get our IG values and IG vectors and from here we have to get top K igen igen vectors and these vectors are projected into data and return and here in the data uh like while calling the PC method we have to uh describe how many components into how many components we want to decompose it and since here I'm passing two components it will be

[5:33]Decomposed into two components and this is the final visualization of the mes data set after implementing PC uh for two components and in a in the conclusion we can now know that PCA is a valuable tool for simplifying complex data sets and and improving the model performance for gaining valuable insights thank you

More videos

Humanitarians AI Lyrical Literacy Project