← All AI/ML articles

Principal component analysis, visualised.

Find the direction along which your data spreads the most, then describe every point by where it sits along that direction. Rotate the line yourself first, then see what the maths picks.

1. Rotate the line2. Watch variance and error3. Let PCA find the best one
Click empty space to add a point · drag a point to move it · double-click to remove it. The white line passes through the mean.
Variance on line0
Mean sq. error0
PC1 share0
Behind the scenes · 1

How do x and y move together?

PCA starts by centring the data on its mean and measuring how the features vary and co-vary. That is the covariance matrix. A large off-diagonal number means the two features rise and fall together, so one direction can describe most of the cloud.


Diagonal entries are the variances of x and y. The off-diagonal entry is their covariance. The trace (sum of the diagonal) is the total variance PCA has to distribute.

Behind the scenes · 2

Variance and error always trade off

Project every point onto your line. The spread of the shadows is the variance on the line; the distances from points to their shadows are the errors. For any line, variance + error equals the total variance, so the line that maximises variance is the same line that minimises error. Click the curve to set the angle.


Variance on the line for every angle from 0° to 180°. The peak is PC1, the lowest point is PC2.
Behind the scenes · 3

Many features: the scree plot

With more than two features the idea is the same, just with more eigenvectors. Below are 120 samples with six features (A to F). Two hidden factors drive most of them, one feature is mostly noise, and feature F is measured in much larger units. Choose how many components to keep and whether to standardise first.


Bars = variance share per component (kept ones in cyan). Line = cumulative share.

Loadings are the eigenvector entries: how much each original feature contributes to a component. The sign of a whole component is arbitrary, so compare sizes, not signs. Common ways to choose k, such as reaching a target share like 90% or stopping at the elbow of the scree plot, are heuristics, not rules: the right k depends on what you will do with the result.

The mathematics

The mathematics behind PCA

Numbers in A to E come from the 2D points in the playground and update live.

A. Centre and build the covariance matrix

Xc = X − μ   ,   C = XcTXc / (n − 1)

B. Eigen-decomposition

C vi = λi vi   ,   λ1 ≥ λ2 ≥ …

Eigenvectors are the principal directions (orthogonal unit vectors). Eigenvalues are the variance along them. The total variance is the trace of C, which equals the sum of the eigenvalues.


C. Variance along any direction

Var(u) = uT C u   ,   λmin ≤ Var(u) ≤ λmax  (unit u)
Reading it
  • The curve in step 2 is exactly this formula evaluated at every angle.
  • Its maximum is λ1 at PC1; its minimum is λ2 at PC2.
  • When λ1 ≈ λ2 the curve is almost flat, so the direction of PC1 is unstable.

D. Reconstruction error

mean ‖xc − Σj≤k (vjTxc) vj‖² = (n−1)/n · Σj>k λj

Keeping only the top k components and rebuilding the points leaves an error equal to the variance you dropped (with the n−1 versus n convention shown). Here k = 1.


E. Link to the SVD

Xc = U Σ VT columns of V = principal directions (eigenvectors of C) σi² / (n − 1) = λi
Experiment

Break it on purpose

One outlier

Add a point far from the cloud and watch PC1 swing towards it. PCA is based on squared distances, so extreme points pull hard.

A round cloud

When the spread is equal in every direction, the two eigenvalues are close and PC1 has no clear direction. Drag points and watch it jump.

Mixed units

With standardising off, the feature with the biggest units dominates PC1. Switch it on and compare the loadings.

Check yourself

Three quick questions

Key takeaways

  • PC1 is the direction of greatest variance. For any line, variance plus reconstruction error equals the total variance, so maximising one minimises the other.
  • The recipe: centre, compute the covariance matrix, take its eigenvectors (directions) and eigenvalues (variances), project onto the top k.
  • Variance explained is the share of total variance the kept components retain. High variance does not guarantee that all meaningful structure is preserved.
  • Scaling: standardising is often useful when features have different units or scales, but it is not automatically best for every dataset.
  • Limits: PCA is linear, sensitive to outliers, and its components can be hard to interpret. The sign of a component is arbitrary.
← Back to all articles