Home / Machine Learning

Principal Component Analysis

Rotate a line through a cloud of points and watch how much of the spread it captures. One angle captures more than any other, and that line is PC1.

The Cloud

2.4
0.8
30°

or drag any point directly on the plot

Your Line

0°

the direction you squash the data onto

the dashed stubs are what projecting throws away

Variance in Every Direction

—

Green is PC1, blue is PC2, orange is the line you chose.

Explained Variance

PC1 Explains
90.0%
λ₁ (PC1 variance) 5.76
λ₂ (PC2 variance) 0.64
PC1 Direction 30°

Your Line vs PC1

Variance Captured 1.92
Kept 30.0%
Lost 70.0%

No direction beats PC1. Rotate your line and the captured variance rises to a single peak, then falls again.

Covariance Matrix

4.61
2.22
2.22
1.79

PCA is the eigen-decomposition of exactly this matrix.

Overview

Principal component analysis (PCA) finds orthogonal directions that capture as much variation in numeric data as possible. Projecting onto the first few directions compresses the data; it can also make the original features harder to interpret.

What PCA computes

PCA first centres each feature by subtracting its mean. It then finds directions that maximise the variance of the projected observations. If the centred data has covariance matrix Σ, the variance along a unit direction u is:

variance along u = uᵀΣu, subject to uᵀu = 1

The direction with the largest value is the first principal component, or PC1. It is an eigenvector of Σ; its eigenvalue is the variance captured along that direction. PC2 captures the most remaining variance while staying perpendicular to PC1. Later components continue the same way.

For example, suppose two centred features have covariance matrix [[4, 3], [3, 4]]. The directions (1, 1) and (1, −1) have variances 7 and 1. PC1 therefore explains 7/(7 + 1) = 87.5% of the total variance. Keeping only PC1 reduces two values per row to one, while discarding the remaining 12.5% of variance.

Try the projection visualiser

  1. Set Projection Angle to 0°. The line misses the cloud’s main direction, so the readout shows less captured variance.
  2. Move it toward 30°. The captured variance reaches a maximum when the line aligns with PC1. The perpendicular direction is PC2; the two axes are orthogonal.

The visualiser shows a two-dimensional case. In higher dimensions, the same rule applies: sort components by eigenvalue, then choose how many to keep. A cumulative explained-variance plot can guide compression, but a threshold such as 95% is a heuristic, not a universal requirement.

Scaling and data leakage

PCA is sensitive to feature units because it maximises variance. If one column is measured in thousands and another in fractions, the large-scale column may dominate. Standardising features is often appropriate when units differ and you want them to contribute on comparable scales. It is not automatic: when raw variance has meaning or features already share a meaningful scale, standardising can remove useful information.

Fit both the scaler and PCA on training data only, then apply those fitted transformations to validation and test data. Fitting them on the full dataset lets held-out rows influence the components, which leaks information. A scikit-learn pipeline keeps those steps together; see feature scaling and the official PCA documentation.

When PCA helps, and what it cannot tell you

PCA can reduce storage, speed up some downstream models, and provide a compact view of high-dimensional numeric data. It is useful when features are correlated and retaining most of their total variance is a sensible objective.

Variance is not the same as predictive value. A low-variance direction may separate classes, so PCA can discard a feature that matters to the target. The components are weighted combinations of the original features; inspect their loadings before assigning them a meaning. PCA is also linear and sensitive to outliers, which can strongly affect covariance. It does not remove outliers or discover arbitrary nonlinear structure. Compare its goal with t-SNE and UMAP when the task is visualising nonlinear neighbourhoods.

Sources and next steps

See the scikit-learn PCA guide for implementation details. For the variance and eigenvector ideas behind the method, review covariance and correlation and eigenvalues and eigenvectors.

Predict, then reveal

About to run: set Cloud Tilt to its maximum (180). Before it does — what happens to the readout?

Committing to an answer first is the point — the reveal runs the experiment on the visualisation above and reads the real value back, so nothing here is scripted.

Check yourself

0 of 3

Answer without scrolling back up.

  1. What does the first principal component point along?

  2. Two features are almost perfectly correlated. What happens to the second eigenvalue?

  3. Why must you usually standardise the columns before running PCA?

Cheat sheet

Principal Component Analysis

Rotate a line through a point cloud and watch the variance it captures peak on exactly one angle. That angle is PC1, and the rest is what you lose.

MACHINE LEARNING · vizlearn.in/machine_learning/pca.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.