Skip to main content

Welcome to a Grasp lesson

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

Lesson 24

Principal Component Analysis (PCA) for Dimensionality Reduction

Recommended resources

Introduction

In our previous lessons, we've systematically deconstructed Principal Component Analysis. You learned that the eigenvectors of a dataset's covariance matrix define the principal axes of variance, and the corresponding eigenvalues quantify the importance of each axis. You now possess the tools to identify the directions of greatest variation in your data and rank them.

This lesson brings everything together for the final, crucial step: performing the dimensionality reduction itself. We will move from theory to practice, learning how to project your high-dimensional data onto the new, lower-dimensional space defined by the most important principal components. By the end of this lesson, you will be able to take a dataset with thousands of features—like the gene expression data from your cancer project—and transform it into a manageable two- or three-dimensional representation, ready for visualization and analysis.


The Geometry of Projection

At its heart, dimensionality reduction via PCA is a change of coordinates. We are taking data points represented in their original basis (e.g., thousands of gene expression levels) and re-representing them in a new, more efficient basis defined by the principal components.

The mathematical tool for finding the coordinate of a data point along a new axis is the orthogonal projection. The new coordinate is simply the dot product of the original data vector with the unit vector defining the new axis.

If we have a centered data point (a vector in dimensions) and we want to find its coordinate along the first principal component, PC1 (represented by the unit eigenvector ), we compute:

This scalar value, , is the new coordinate of the data point in the 1-dimensional space of PC1. To reduce our data to k dimensions, we simply repeat this process for the top k principal components (). The new representation of the data point becomes a k-dimensional vector:

This projection step is beautifully illustrated in the "PCA in a nutshell" infographic.

This infographic provides a complete overview of the PCA process. We have covered steps 1 through 5 in previous lessons. Today's focus is on step 6: projecting the data points onto the chosen eigenvectors to create the final, low-dimensional representation shown in step 7.

From a Single Point to a Full Dataset

While projecting a single point is useful for intuition, in practice we want to transform the entire dataset at once. This is where the power of linear algebra becomes evident.

Let's organize our components:

  • Let be our centered data matrix of size , where is the number of samples and is the number of original features (e.g., genes). Each row is a data point.
  • Let be our transformation matrix of size , where we've taken the top k eigenvectors () and stacked them as columns.

To project all data points into the new k-dimensional space, we simply perform a matrix multiplication:

The resulting matrix will have dimensions . This is your new, low-dimensional dataset. Each column of is a principal component, and each row is a sample represented by its new coordinates. This single, efficient operation is why PCA is so computationally feasible even for massive datasets.

The following blog post, written for a bioinformatics audience, clearly explains this final step.

Exploring Principal Component Analysis (PCA)

This article breaks down PCA from a bioinformatician's perspective. We'll focus on the section that describes the projection itself.

Please read the section titled 5) Projecting Data onto the k Principal Components. This section describes how the original data is transformed into the new space defined by the principal components, which is exactly the process we are discussing.


Visualizing the Transformation and Information Loss

What does this projection actually look like, and what information do we lose? The image below provides a fantastic illustration.

This figure demonstrates the PCA process on a 2D dataset. **Top-left:** The original correlated data. **Top-right:** The data transformed into the principal component space (rotated). **Bottom-left:** The data reduced to 1D by dropping the second principal component. **Bottom-right:** The 1D data "back-rotated" into the original space, showing how it approximates the original data along the main axis of variance.

The "Back-rotation" panel is particularly insightful. It shows what's left of the data when we only keep information from the first principal component. The distance between these projected points and the original points represents the reconstruction error. By choosing the principal components with the largest eigenvalues, PCA ensures this reconstruction error is minimized for a given number of dimensions.

This is precisely how you can take your high-dimensional gene expression data and create an informative 2D plot. You project the data from thousands of dimensions down onto PC1 and PC2. The StatQuest video we've used before has an excellent segment visualizing this exact process.

StatQuest: Principal Component Analysis (PCA), Step-by-Step

Let's revisit the StatQuest video to see the projection step in action for a higher-dimensional example.

Watch the segment from how to convert the 3D graph. Here, Josh Starmer demonstrates projecting data from 3D (and conceptually, 4D) space onto the 2D plane defined by PC1 and PC2 to create the final PCA plot. This directly mirrors what you would do with your project's data.


The Full PCA Algorithm

We can now summarize the entire PCA process in a clear, step-by-step algorithm. Professor Deisenroth's lecture notes offer a concise and mathematically sound summary.

Principal Component Analysis

These slides from a course on Mathematics for Machine Learning provide a rigorous yet clear summary of the PCA algorithm.

Please review slides 31 through 38. Start with the slide titled Key Steps of PCA (slide 31), which lists the full algorithm. Then, follow the visual walkthrough on the subsequent slides (slides 33-38), which shows the effect of each step on a 2D dataset, culminating in the orthogonal projection onto the principal subspace.

To see how this algorithm translates directly into code for a biological problem, let's return to the bioinformaticamente blog post.

Exploring Principal Component Analysis (PCA)

This section provides a complete Python script implementing the PCA algorithm on an RNA-seq dataset.

Please review the code in the section titled Some practice. You don't need to understand every line, but focus on the overall flow: Loading and Normalization: The data is loaded, log-transformed, and standardized. This corresponds to the centering/scaling steps. PCA Transformation: The line X_pca = pca.fit_transform(X_scaled) is the core of the process. The fit_transform method from the scikit-learn library efficiently performs the eigendecomposition of the covariance matrix and then projects the scaled data (X_scaled) onto the principal components to produce the new low-dimensional data (X_pca). Visualization: The script then generates the scree plot and the final PCA plot of the samples, just as we've discussed.

This practical example shows how the complex series of matrix operations we've been studying is often encapsulated in a single, powerful function call in modern machine learning libraries.


Conclusion

In this lesson, you have mastered the final and most practical step of Principal Component Analysis. You now understand how to use the principal components to transform your data into a lower-dimensional space.

Key Takeaways:

  • Projection as a Dot Product: The coordinate of a data point in the new PCA space is found by taking the dot product of the centered data vector with the corresponding eigenvector.
  • Matrix Multiplication for Efficiency: The entire dataset can be transformed from dimensions to k dimensions by multiplying the centered data matrix by the matrix of top eigenvectors , yielding the new data matrix .
  • Reconstruction Error: The information lost during dimensionality reduction is the reconstruction error, which PCA minimizes by design.
  • PCA in Practice: The full algorithm (centering, scaling, eigendecomposition, projection) is often implemented as a single fit_transform step in ML libraries.

You are now equipped to apply PCA to your own high-dimensional biological data. Our next lesson will introduce the Singular Value Decomposition (SVD). You will learn that SVD is a more general and numerically stable matrix factorization technique that can compute the principal components directly from the data matrix, without ever needing to form the covariance matrix—a crucial advantage in the world of high-dimensional genomics.