Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 26

SVD for PCA: Unveiling Principal Components

Recommended resources

Introduction

In our previous lesson, we established the geometric intuition behind Singular Value Decomposition (SVD), understanding it as a process that breaks down any linear transformation into a sequence of rotation, scaling, and another rotation (). You've also explored Principal Component Analysis (PCA) as a method to find directions of maximal variance by calculating the eigenvectors of a covariance matrix.

You might be wondering if these two powerful ideas—one revealing the fundamental geometry of matrices, the other finding the most important axes of a dataset—are related. The answer is a profound yes. This lesson will bridge that gap, showing you that SVD provides a more direct, numerically stable, and computationally robust method for performing PCA. We will demonstrate precisely how the matrices , , and from the SVD of a centered data matrix correspond directly to the principal components, variances, and scores you've worked with before. This connection is not just a mathematical curiosity; it's the reason why modern machine learning libraries use SVD "under the hood" for dimensionality reduction, including in applications like your cancer classification project.


Two Paths to Dimensionality Reduction

To understand the connection, let's visualize two parallel workflows for finding the principal components of a dataset.

This flowchart illustrates two equivalent methods for dimensionality reduction. The left path, via Eigendecomposition, first computes the covariance matrix \(X^T X\) and then finds its eigenvectors. The right path uses Singular Value Decomposition directly on the data matrix \(X\). This lesson will prove that these two paths are mathematically connected and that \(W = V\).

The left path is the method we've used for PCA so far:

  1. Start with a centered data matrix .
  2. Compute the covariance matrix .
  3. Perform an eigendecomposition on to find the eigenvectors (the principal axes) and eigenvalues (the variance).

The right path is the SVD approach:

  1. Start with the same centered data matrix .
  2. Perform SVD directly on to get , , and .

Our goal is to prove that the principal components derived from both paths are identical. Specifically, we will show that the matrix of eigenvectors from the first path is the same as the matrix from the second path.


The Mathematical Bridge: Connecting SVD and the Covariance Matrix

The key to connecting PCA and SVD lies in the formula for the covariance matrix. For a data matrix with samples (rows) and features (columns), where each column has been centered to have a mean of zero, the covariance matrix is defined as:

Now, let's substitute the SVD of the data matrix, , into this equation.

PCA and SVD: The Linear Algebra You Actually Need

This article provides a beautifully concise proof of the relationship between SVD and the eigendecomposition of the covariance matrix.

Please read the short section titled Why Does This Work?. Follow the derivation that starts by substituting the SVD formula into the covariance formula.

As the reading demonstrates, the derivation unfolds as follows:

Using the transpose rule , we get:

Since the left singular vectors in form an orthonormal basis, we know that (the identity matrix). The expression simplifies beautifully:

Let's pause and examine this result. It looks strikingly similar to the eigendecomposition of a matrix, which for a symmetric matrix is , where contains the eigenvectors and is a diagonal matrix of eigenvalues.

By comparing the two forms:

  • (Eigendecomposition)
  • (from SVD)

We can establish a direct correspondence:

  1. The matrix of eigenvectors is identical to the matrix of right singular vectors . This is the main result: the right singular vectors of the data matrix are the principal components (axes) of the data.
  2. The diagonal matrix of eigenvalues is equal to . Since is a diagonal matrix with the squared singular values () on its diagonal, this means each eigenvalue is related to its corresponding singular value by:

This proves that performing SVD on the centered data matrix gives us everything we need for PCA without ever having to compute the covariance matrix explicitly.


The SVD Dictionary for PCA

With this mathematical connection established, we can now translate all the components of PCA into the language of SVD.

The following video by Steve Brunton provides an excellent summary of how this works.

Principal Component Analysis (PCA)

This video on Principal Component Analysis clearly explains the role of data centering and how SVD directly yields the principal components.

First, watch the segment on mean-centering the data to solidify why this is a critical first step. Then, watch the key part of the video from the principal components, where the narrator explicitly shows how the principal components and loadings are obtained directly from the SVD of the mean-subtracted data.

As the video highlights, here is the full translation:

  • Principal Axes (The Directions): The columns of are the principal components. The first column of is PC1, the second is PC2, and so on. These are the directions of maximum variance in your data.

  • Variance (The Importance): The eigenvalues represent the variance of the data projected onto each principal component. We can calculate them directly from the singular values found in :

  • Principal Component Scores (The New Coordinates): The scores are the coordinates of the original data points in the new coordinate system defined by the principal axes. To get them, we project the original data onto the principal axes .

    Using the SVD of , this becomes:

    So, the matrix of principal component scores is simply . This gives you the new, lower-dimensional representation of your data. For example, if you want a 2D representation, you would use the first two columns of .

The following resource provides a comprehensive walkthrough, deriving these relationships with detailed mathematical steps and Python code.

The Theory of the Singular Value Decomposition and its ...

This article synthesizes all the concepts of PCA from the ground up using SVD. It's dense, but it formalizes the connections we've just discussed.

I recommend skimming these sections to see how the pieces fit together. Don't worry about memorizing every derivation. Start with the introduction to the PCA synthesis section. Focus on the section Determining the Number of Principal Axes. Notice how it formally derives the relationship \mathbf{E} = \frac{\mathbf{\Sigma}^2}{N-1} (where \mathbf{E} is the eigenvalue matrix) and confirms that eigenvalues represent variance. Finally, look at the section Computing Loadings. The key result here is the derivation showing that the unstandardized principal component scores are given by \mathbf{P}_{unstd} = \mathbf{U\Sigma}.


Why SVD is the Preferred Method in Practice

At this point, you might think that SVD is just a more complicated way to do something we already knew how to do. However, there are critical reasons why computational libraries like Scikit-learn use SVD to implement their PCA algorithms. This is directly relevant to your goal of understanding model failure modes.

  1. Numerical Stability: The primary reason is numerical stability. The process of forming the covariance matrix, , can be prone to loss of precision. If your data matrix has features that are highly correlated (a common scenario in genomics), the matrix becomes "ill-conditioned." Small floating-point errors in your data can be dramatically amplified during the calculation, leading to inaccurate eigenvectors and eigenvalues. SVD algorithms work directly on the matrix and are far more robust to these issues.

  2. Computational Efficiency: While it might not seem so at first, SVD can be more efficient, especially for "wide" data matrices (), where you have many more features than samples. In this case, the covariance matrix is a massive matrix, and finding its eigendecomposition can be prohibitively expensive. SVD provides more flexible computational pathways.

The following video explains this practical advantage clearly.

PCA and SVD - the math explained simply

This video compares the eigenvalue decomposition method with the SVD method for PCA.

Watch the segment from compute the PCA. The narrator explicitly states that SVD is generally more accurate because it works directly on the data matrix without computing the covariance matrix, which can cause rounding errors.


Conclusion

In this lesson, we have forged the crucial link between Singular Value Decomposition and Principal Component Analysis. You now understand that SVD is not just an alternative to PCA; it is the modern, robust engine that drives it.

Key Takeaways:

  • Direct Connection: SVD on a mean-centered data matrix directly computes all the necessary components for PCA.
  • The SVD-to-PCA Dictionary:
    • Principal Axes are the columns of .
    • Variance along each axis is given by .
    • Principal Component Scores (the projected data) are the columns of the matrix product .
  • Practical Superiority: SVD is the method of choice for implementing PCA in software because it is more numerically stable and avoids the potential precision issues of forming the covariance matrix, especially with high-dimensional, correlated data typical in computational biology.

In our next lesson, we will explore another powerful application of SVD: its role in defining and computing the pseudoinverse of a matrix. This will be essential for understanding how to solve linear regression problems robustly, even when the system is ill-conditioned or has redundant features.