Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Dimensionality Reduction with PCA

Hello! Welcome to your next lesson on eigenanalysis and its applications.

Introduction

In our previous lesson, we undertook a deep theoretical dive to derive the formulation of Principal Component Analysis (PCA). We rigorously proved, from two different perspectives, that the principal components of a dataset are precisely the eigenvectors of its covariance matrix, and the corresponding eigenvalues represent the variance captured by each component.

Today, we transition from theory to practice. Our learning outcome is to apply PCA to a data matrix to perform dimensionality reduction. We will build on the "why" you learned last time to establish the "how"—a concrete, step-by-step algorithm for using PCA. You will learn how to process a dataset, extract the principal components, decide how many components to keep, and transform the data into a lower-dimensional representation.

Let's begin by visually recalling the core idea.

Principal Component Analysis for Dimensionality Reduction
This image illustrates the essence of PCA. In a 2D dataset, PCA identifies a new coordinate system defined by the principal components (PC1, PC2). PC1 is oriented along the direction of maximum data variance. By projecting the data onto just PC1, we can reduce the dimensionality from 2D to 1D while retaining most of the information.

The PCA Algorithm: From Data to Components

Applying PCA involves a sequence of clear, well-defined steps. We'll use the standard machine learning data convention where our data matrix has dimensions , with being the number of samples (data points) and being the number of features.

Step 1: Center the Data

The first, and crucial, step is to center the data by subtracting the mean of each feature. For each feature (i.e., each column of ), we compute its mean and subtract it from every entry in that column.

This ensures that each feature has a mean of zero. This is a necessary prerequisite because the covariance matrix, which measures how features vary together, is defined in terms of deviations from the mean.

Step 2: Compute the Covariance Matrix

With the centered data matrix , we can now compute the sample covariance matrix, which we'll denote as .

Let's break this down:

  • has dimensions .
  • has dimensions .
  • The product results in an matrix.
  • The -th entry of this matrix is the dot product of the -th and -th columns (features) of , which, when scaled by , gives the covariance between feature and feature . The diagonal entries are the variances.

Step 3: Eigendecomposition of the Covariance Matrix

This is where the theory from our last lesson comes into play. We perform an eigendecomposition of the symmetric covariance matrix .

  • is a diagonal matrix containing the eigenvalues () of .
  • is an orthonormal matrix whose columns () are the corresponding eigenvectors of .

As we proved, these eigenvectors are the principal components of our data, and the eigenvalues are the variances captured by each component.

Step 4: Sort Components and Project the Data

The eigenvalues tell us the importance of each principal component. To perform dimensionality reduction, we first sort the eigenvalues in descending order and sort the eigenvectors (principal components) accordingly.

Let's say we decide to reduce the dimensionality from to (where ). We select the first principal components corresponding to the largest eigenvalues. Let's form a matrix of size whose columns are these top principal components.

To get our new, lower-dimensional data representation , we project the centered data onto these components:

  • is .
  • is .
  • The resulting matrix is . Each row of is the new -dimensional representation of the corresponding original data point.

Performing Dimensionality Reduction

The most critical decision in applying PCA is choosing , the number of components to keep. How do we make this choice in a principled way?

We use the concept of explained variance. The total variance in the data is the sum of all the eigenvalues: . The proportion of total variance explained by the -th principal component is:

To choose , we typically look at the cumulative explained variance. We calculate the sum of the explained variance for the top components and keep enough components to exceed a certain threshold, often 95% or 99%.

For a deeper dive into the algorithm and the practicalities of dimensionality reduction, the following resources are excellent.

A Tutorial on Principal Component Analysis

The following sections from the excellent tutorial by Jonathon Shlens summarize the PCA procedure and the specific application of dimensionality reduction.

First, read Section VII.A 'Quick Summary' for a concise recap of the steps. Then, read Section VII.B 'Dimensional Reduction', which explains the logic of discarding less important axes based on their associated variances. Finally, to see how these steps translate to code, look at Appendix B, which provides explanatory Matlab code for performing PCA.

Principal Component Analysis

These notes from NYU provide a very clear, mathematically rigorous explanation of how PCA-based dimensionality reduction works, including a formal proof that PCA finds the optimal subspace for capturing variance.

Read Section 7, 'Dimensionality reduction via PCA'. Pay close attention to Theorem 7.1, which proves the optimality of PCA for this task. Also, review the examples, especially Example 7.5, which shows how projecting onto the first few principal components preserves data structure for visualization, whereas projecting onto the last few does not.

Test your understanding!

You have a dataset with 5 features (). After running PCA, you get the following eigenvalues: , , , , .

  1. What is the total variance in the data?
  2. What percentage of the total variance is explained by the first principal component (PC1)?
  3. If you want to retain at least 90% of the total variance, how many principal components () should you keep?
Show answer
  1. The total variance is the sum of the eigenvalues: .
  2. PC1 explains of the total variance.
  3. Let's compute the cumulative variance:
    • PC1: 50%
    • PC1 + PC2:
    • PC1 + PC2 + PC3:
    • PC1 + PC2 + PC3 + PC4:
      You should keep 3 principal components to retain exactly 90% of the variance, or 4 to be strictly above 90%. In practice, you would typically choose to ensure the threshold is met and exceeded.

A Complete Example: PCA in Action

Let's see this entire process applied to a dataset. The following video provides an excellent walkthrough, demonstrating how PCA can find the underlying structure in noisy, high-dimensional data.

17: Principal Components Analysis_ - Intro to Neural Computation

This video from MIT OpenCourseWare demonstrates the full application of PCA, from data generation to analysis. It shows how to compute the covariance matrix, extract eigenvectors/eigenvalues, and use them to rotate the data, revealing its low-dimensional structure.

Watch the segment from 01:07:46 to 01:19:10. This part of the lecture is a self-contained demonstration. Focus on how the lecturer: Takes a high-dimensional dataset (time-series signals). Computes the covariance matrix. Examines the eigenvalue spectrum to determine the data's 'true' dimensionality. Projects the data onto the top principal components to reveal hidden clusters. Uses the projection to filter noise from the original signals.

An Alternative Method: The SVD Connection

While computing the eigendecomposition of the covariance matrix is the definitional way to perform PCA, it is not always the most numerically stable or efficient method in practice. A more robust approach uses the Singular Value Decomposition (SVD), which we will study in detail in our next lesson.

For now, it is sufficient to know that you can perform PCA directly by taking the SVD of the centered data matrix .

Principal Component Analysis (PCA)

Steve Brunton provides a concise and clear explanation of the connection between PCA and SVD.

Watch from 08:55 to 11:56. This segment directly explains how the principal components and the variance information can be obtained from the SVD of the mean-subtracted data, bypassing the need to explicitly form the covariance matrix.

The key takeaway is that if (the SVD of the centered data), then:

  • The columns of are the principal components (the same eigenvectors of ).
  • The singular values in the diagonal matrix are related to the eigenvalues of by . This means the squared singular values also tell you how variance is distributed across the components.

This deep connection between eigenanalysis, PCA, and SVD is a cornerstone of linear algebra in data science. For your interest, the Shlens tutorial (LINK, Section VI) provides a full mathematical derivation of this relationship.

Conclusion

In this lesson, we have translated the theory of PCA into a practical algorithm for dimensionality reduction. You now have the complete picture, from the foundational mathematics to the applied methodology.

Key Takeaways:

  • The PCA algorithm consists of four main steps: centering the data, computing the covariance matrix, performing eigendecomposition on this matrix, and projecting the data onto the top-ranked eigenvectors (principal components).
  • The number of components to retain, , is chosen based on the desired cumulative explained variance, which is calculated from the eigenvalues.
  • Applying PCA transforms the data into a new, lower-dimensional space where the new features (the principal components) are uncorrelated and ordered by the amount of variance they capture.
  • While PCA is defined by the eigendecomposition of the covariance matrix, it can be more robustly computed using the Singular Value Decomposition (SVD) of the data matrix.

Preview of the Next Lesson:
We've just seen a glimpse of how SVD relates to PCA. In our next lesson, we will formally define the Singular Value Decomposition (SVD). You will learn about its constituent matrices (, , and ) and their fundamental properties. We will explore why SVD is arguably one of the most important matrix decompositions in all of data analysis and machine learning.

Can't find a good explanation? Sign up and we'll make it for you

Sign up