All articles
Machine Learning
ml-unsupervised

Mastering Principal Component Analysis: A Guide for Machine Learning Students

Unlock the power of dimensionality reduction with this comprehensive guide to Principal Component Analysis, covering the mathematical foundations and practical applications.

Math Instructor AI 22 September 2026 8 min read

Introduction to Principal Component Analysis

In the realm of machine learning, we often encounter datasets with a vast number of features. While high-dimensional data can be rich in information, it frequently leads to the curse of dimensionality, where models become computationally expensive and prone to overfitting. Principal Component Analysis (PCA) is the primary unsupervised learning technique used to address this by performing dimensionality reduction.

By the end of this article, you will understand how PCA transforms correlated variables into a smaller set of uncorrelated components, known as principal components. Mastering this technique is essential for your machine learning exams, as it provides the mathematical foundation for feature extraction, noise filtering, and data visualisation.

The Intuition Behind PCA

PCA works by identifying the directions, or principal axes, along which the variation in the data is maximal. The first principal component (PC1) captures the direction of the greatest variance in the dataset. The second principal component (PC2) is then chosen to be orthogonal to the first, capturing the next highest amount of remaining variance. This process continues until we have as many components as original features.

By projecting the original data onto a subset of these components, we reduce the dimensionality while retaining the most significant information. This is not merely about discarding features; it is about creating new, synthetic features that represent the underlying structure of the data.

Step-by-Step Mathematical Breakdown

To perform PCA manually, follow these steps:

  1. Standardise the data: Ensure each feature has a mean of 0 and a standard deviation of 1. This prevents features with larger scales from dominating the analysis.
  2. Compute the Covariance Matrix: This matrix captures how variables vary together.
  3. Calculate Eigenvalues and Eigenvectors: Solve the characteristic equation $det(S - \lambda I) = 0$, where $S$ is the covariance matrix and $\lambda$ represents the eigenvalues.
  4. Sort and Select: Rank the eigenvalues in descending order. The corresponding eigenvectors represent the principal components.
  5. Project: Multiply the original data by the top $k$ eigenvectors to obtain the reduced dataset.

Worked Example 1: 2D to 1D Reduction

Consider a dataset with two features, $X_1$ and $X_2$, with a covariance matrix $S = \begin{pmatrix} 1 & 0.8 \ 0.8 & 1 \end{pmatrix}$.

To find the eigenvalues, we solve $det(S - \lambda I) = 0$: $$det \begin{pmatrix} 1-\lambda & 0.8 \ 0.8 & 1-\lambda \end{pmatrix} = (1-\lambda)^2 - 0.64 = 0$$ $$(1-\lambda)^2 = 0.64 \implies 1-\lambda = \pm 0.8$$ This gives $\lambda_1 = 1.8$ and $\lambda_2 = 0.2$.

The eigenvector for $\lambda_1 = 1.8$ is found by solving $(S - 1.8I)v = 0$: $$\begin{pmatrix} -0.8 & 0.8 \ 0.8 & -0.8 \end{pmatrix} \begin{pmatrix} x \ y \end{pmatrix} = 0 \implies x = y$$ Normalised, the first principal component is $v_1 = \begin{pmatrix} 1/\sqrt{2} \ 1/\sqrt{2} \end{pmatrix}$. We project our data onto this vector to reduce it to one dimension.

Worked Example 2: Variance Explained

If we have eigenvalues $\lambda_1 = 3.0, \lambda_2 = 0.8, \lambda_3 = 0.2$, the total variance is $3.0 + 0.8 + 0.2 = 4.0$. The proportion of variance explained by the first component is $\frac{3.0}{4.0} = 75%$. By keeping only the first two components, we retain $\frac{3.0 + 0.8}{4.0} = 95%$ of the information, effectively reducing the data from 3D to 2D with minimal loss.

Common Mistakes

  • Forgetting to Standardise: If features are on different scales, PCA will be biased towards features with larger numerical ranges.
  • Confusing Eigenvalues with Eigenvectors: Remember that eigenvalues represent the magnitude of variance, while eigenvectors represent the direction.
  • Assuming Linearity: PCA is a linear transformation. It cannot capture complex, non-linear relationships between variables.

Frequently Asked Questions

  • Why do we need to centre the data? Centring ensures the first principal component passes through the mean of the data, which is necessary for the variance calculation to be accurate.
  • How many components should I keep? Use the 'elbow method' on a scree plot or choose a number of components that explains a target percentage of variance, such as 95%.
  • Is PCA supervised or unsupervised? PCA is an unsupervised technique because it does not use target labels to find the components.

Conclusion

Principal Component Analysis is a cornerstone of modern data science, enabling us to simplify complex datasets while preserving essential patterns. By understanding the underlying linear algebra, you are well-equipped to handle high-dimensional challenges in your machine learning projects. To see these concepts in action with interactive visualisations, generate a free animated lesson on this topic at MathInstructor AI.

Topics

pca
dimensionality reduction
machine learning
eigenvectors
principal components
ml-unsupervised
covariance matrix
feature extraction
linear algebra

Want this explained out loud?

Turn any question into a narrated, animated lesson in seconds.

Try the Studio free