Foundations to Frontier
Tests
D. Classical Machine Learning › D9 Dimensionality Reduction and Manifold Learning

Principal Component Analysis (PCA)

PCA finds the orthogonal directions along which centred data vary most. Projecting onto the first k of them gives the best k-dimensional linear approximation of the data in squared error, with uncorrelated coordinates (scores) whose variances are the eigenvalues of the covariance matrix. It is computed by eigendecomposition of the covariance or, more stably, by the SVD of the centred data matrix. This page covers both views (maximum variance and minimum reconstruction error), centring and scaling, explained variance, whitening, choosing the number of components (variance thresholds, Kaiser, scree, parallel analysis, cross-validation) and their pitfalls, cost, and interpretation of loadings. In the exercises you implement PCA both ways and verify its optimality properties; then you test rules for choosing k on data with a known rank and see how noise breaks them.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Find the directions where the data spread out

Tabular data with many columns are often redundant: columns move together. PCA rotates the coordinate system to line up with the data:

  • the first new axis points where the data spread most;
  • the second is the most spread-out direction at right angles to the first;
  • and so on.

Keeping only the first few axes gives a compressed version of the data that loses as little as possible.

PCA: the directions of greatest variance
PC1 explains 88.1% of the variance · eigenvalues 1.58, 0.21 · PC1 direction (0.73, 0.68)
Strong correlation squeezes the cloud onto a line, and one component then explains almost everything. Now rescale feature 2 without standardising: PC1 swings toward the feature with bigger numbers, purely because of its units. Standardising removes that, and then PCA depends only on the correlation.
Why engineers care

PCA is the first tool for high-dimensional measurements. It compresses 100 correlated sensor channels to a handful of signals, denoises spectra, finds the dominant modes of a structure from vibration data (proper orthogonal decomposition), builds monitoring statistics (Hotelling T² and SPE) for process and engine health, and pre-processes inputs for other models.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The first principal component points…
  2. 2. Principal components are mutually…
  3. 3. Changing one feature's units (×1000) without standardising…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

PCA by hand

Covariance Σ = [[2, 1], [1, 2]].

  • The eigenvalues are 3 and 1, so PC1 explains 3/4 = 75% of the variance.
  • PC1 = (1, 1)/√2 and PC2 = (1, −1)/√2.

Scores and reconstruction. Take the four centred points (±1, 0) and (0, ±0.5).

  • Sums of squares are 2 along x and 0.5 along y, so the explained ratios are 0.8 and 0.2.
  • Keeping only PC1 (the x-axis) loses the y-parts: a reconstruction error of 0.5 (sum of squares), exactly the discarded part.

Choosing k by a threshold. With explained ratios 0.5, 0.3, 0.15, 0.05, reaching 90% takes 3 components (cumulative 0.5, 0.8, 0.95).

Via SVD. Centre X (n × d) and write it as UΣVᵀ.

  • The columns of V are the principal directions.
  • The scores are UΣ.
  • The eigenvalues of the covariance are sᵢ²/(n − 1).

There is no need to form XᵀX, which squares the condition number.

The steps in plain English:

  1. Centre each feature. Standardise too if the features have different units.
  2. Compute the SVD of the centred matrix, or the eigendecomposition of its covariance.
  3. Sort the components by variance and choose k (see Level 2).
  4. Project: scores = X_c·V_k. Reconstruct with scores·V_kᵀ + mean. Inspect the loadings (the columns of V_k) to interpret the components.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Eigenvalues of [[3, 0], [0, 1]]: share explained by PC1?
  2. 2. Ratios 0.6, 0.25, 0.1, 0.05: components for 90%?
  3. 3. From the SVD, covariance eigenvalues are…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 30 minUndergraduate students and practising engineers.

Two views, and how many components to keep

Maximum variance equals minimum reconstruction error.

  • PC1 maximises Var(Xw) subject to ‖w‖ = 1. The Lagrangian gives Σw = λw, so the answer is the top eigenvector, with variance λ₁.
  • Equivalently, the k-dimensional subspace minimising Σ‖xᵢ − P xᵢ‖² is spanned by the top k eigenvectors (Eckart–Young), and the minimum error is the sum of the discarded eigenvalues.

Exercise 1 verified both properties, plus uncorrelated scores and agreement between the eigendecomposition and SVD routes.

Scaling matters. PCA on raw data is not scale-invariant. In exercise 1, multiplying one feature by 1000 made PC1 explain 100% of the variance, all of it that feature. Use the correlation matrix (standardised features) unless the units are commensurate.

Choosing k.

  • Variance threshold (keep 90%): simple, but noise adds variance in every direction, so the threshold keeps chasing noise.
  • Kaiser rule (eigenvalue of the correlation matrix above 1): easy, but it ignores sampling variability.
  • Scree / largest gap: visual, and ambiguous with gradual decay.
  • Parallel analysis (Horn, 1965): keep components whose eigenvalues beat those of column-permuted data. It is conservative.
  • Cross-validation of reconstruction error, or a probabilistic-PCA likelihood (next topics).

Exercise 2 had true rank 5 in 30 dimensions. At low noise Kaiser was exact, while the 90% rule picked 13–23 components once noise grew, and parallel analysis consistently under-counted. Its weakest signal component, at a correlation eigenvalue of about 1.15, sat below the random-matrix edge of 1.92, so no permutation test could separate it from chance.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
Z = StandardScaler().fit_transform(X)
pca = PCA(n_components=0.9).fit(Z)              # keep 90% of variance (beware noise)
pca.explained_variance_ratio_, pca.components_
🐍 Exercise 1: PCA two ways
Implement PCA by eigendecomposition and by SVD, projection, reconstruction and explained variance; verify they agree, that scores are uncorrelated, that reconstruction error equals the discarded variance, and how unit scaling distorts PCA.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The minimum k-dimensional reconstruction error equals…
  2. 2. Why compute PCA by SVD rather than forming XᵀX?
  3. 3. In exercise 2, the 90% rule at high noise chose…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Eckart–Young–Mirsky. The best rank-k approximation of a matrix in Frobenius (and spectral) norm is its truncated SVD. PCA applies this to the centred data matrix.

Sampling and the noise edge. For pure noise with n samples in d dimensions, the eigenvalues of the sample correlation matrix spread over the Marchenko–Pastur interval [(1 − √(d/n))², (1 + √(d/n))²] instead of all being 1. A signal component is detectable from the data only if its population eigenvalue exceeds about 1 + √(d/n) (the BBP phase transition), and its sample eigenvalue then appears above the edge. That explains why Kaiser’s “above 1” overcounts in noisy data (noise eigenvalues exceed 1 up to the edge), and why parallel analysis misses weak components.

Whitening. Scaling the scores by 1/√λ gives unit-variance, uncorrelated features (PCA whitening). It is used before ICA and some clustering, but it amplifies noise in low-variance directions.

Interview questions.

  • Covariance or correlation PCA? Correlation (standardised) when features have different units or scales. Covariance when the units are the same and the variance magnitudes matter (pixels, same-type sensors).
  • How do you choose k in practice? Use the downstream objective (cross-validated model performance), cross-validated reconstruction, or parallel analysis as a conservative floor. Don’t rely on a fixed variance percentage in noisy data.
📝 Quick check (Level 3)
0/3 answered
  1. 1. Upper edge of noise eigenvalues for d/n = 0.15?
  2. 2. PCA whitening scales scores by…
  3. 3. The best rank-k approximation of a matrix is given by…

Level 4: Expert

★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.

Which rule finds the true number of components?

History.

  • K. Pearson (1901) introduced lines and planes of closest fit.
  • H. Hotelling (1933) named principal components.
  • C. Eckart and G. Young (1936) proved the low-rank approximation theorem.
  • H. Kaiser (1960) proposed the eigenvalue-above-1 rule; J. Horn (1965) parallel analysis.
  • J. Baik, G. Ben Arous and S. Péché (2005) found the detectability phase transition.

Exercise 2. True rank 5 (component standard deviations 5, 4, 3, 2, 1.5) in 30 dimensions, n = 200, isotropic noise σ, 5 datasets per level. Each cell shows the mean chosen k and, in brackets, how often k was exactly 5.

noise σ 90% variance Kaiser (λ > 1) largest log-gap parallel analysis
0.3 4.0 (0%) 5.0 (100%) 4.8 (80%) 4.4 (40%)
0.7 12.8 (0%) 5.0 (100%) 4.4 (60%) 4.2 (20%)
1.2 19.8 (0%) 5.4 (60%) 3.2 (20%) 4.4 (40%)
2.0 23.0 (0%) 8.6 (0%) 1.8 (0%) 3.6 (0%)
  • The 90% rule was useless as noise grew. Noise spreads variance over all 30 directions, so reaching 90% needs ever more of them.
  • Kaiser was exact at low noise but overcounted at σ = 2, as noise eigenvalues rose above 1, up to the random-matrix edge of 1.92.
  • Parallel analysis never overcounted, but it missed the weakest component. In correlation units that component’s eigenvalue (about 1.15) lay inside the noise bulk. No rule can recover a component the data can’t distinguish from chance; that is the BBP limit.
🐍 Exercise 2 (research): choosing the number of components
On rank-5 data in 30 dimensions with increasing noise, compare the 90%-variance rule, Kaiser's rule, the largest log-eigenvalue gap and Horn's parallel analysis by the k they choose.

PCA in AI, robotics and aerospace

  • Process and engine monitoring: PCA models of normal operation, with Hotelling T² and squared prediction error alarms.
  • Modal analysis and reduced-order models: proper orthogonal decomposition of flow or structural fields for fast simulation.
  • Sensor compression and denoising before downlink or before learning.

References

  1. I. T. Jolliffe, J. Cadima (2016). “Principal component analysis: a review and recent developments.” Phil. Trans. R. Soc. A 374.
  2. H. Hotelling (1933). “Analysis of a complex of statistical variables into principal components.” Journal of Educational Psychology 24.
  3. J. L. Horn (1965). “A rationale and test for the number of factors in factor analysis.” Psychometrika 30.
  4. J. Baik, G. Ben Arous, S. Péché (2005). “Phase transition of the largest eigenvalue for nonnull complex sample covariance matrices.” Annals of Probability 33(5).
  5. C. Eckart, G. Young (1936). “The approximation of one matrix by another of lower rank.” Psychometrika 1.