Overview
Curated: · Written: · Reviewed:
Principal Component Analysis (PCA)
In one line
PCA rotates centred data onto orthogonal directions of maximum variance, orders them by variance, and keeps the first k. Reach for it when features are numeric and linearly correlated, and you need a reusable linear reduction with a known reconstruction error.
Key definitions
- Centred data: every column has mean zero. PCA is defined on centred data only.
- Covariance: C = X^T X / (n-1). Symmetric and positive semidefinite, so its eigenvectors are orthogonal and its eigenvalues are non-negative.
- Principal component: an eigenvector of C, ranked by its eigenvalue. Its sign is arbitrary.
- Loadings: the eigenvector coefficients, as used here. Some sources mean feature-component correlations, so check which before comparing numbers.
- Explained variance ratio: lambda_i divided by the sum of all eigenvalues.
- Whitening: dividing each score by the square root of its eigenvalue, which gives the scores identity covariance.
Formulas and complexity
Full SVD of an n by d matrix costs about O(n d^2) when n is at least d. Truncated or randomised SVD computes only the top k components, which suits large d with small k.
How it works
Centre the features with training means. Take the SVD of the centred matrix, X = U S V^T, or eigendecompose C. Sort by variance and keep the first k columns of V. Scores are Z = X V_k. The reconstruction is the orthogonal projection onto the k-dimensional subspace, plus the mean. The discarded components carry the residual variance, and that residual is the reconstruction error.
When to use and when not to
| Situation | PCA fits? | Use instead |
|---|---|---|
| Numeric, linearly correlated features with mixed units | Yes, after standardising | None needed |
| Curved structure such as concentric rings | No, it is linear | Kernel PCA or an autoencoder |
| Labels exist and you need class separation | Weak, variance ignores labels | LDA or supervised selection |
| Model must explain predictions in original columns | No, components are mixtures | Feature selection |
| One-hot categorical columns | Poor, sparse correlated blocks | Multiple correspondence analysis |
| Visual exploration of clusters | Blurs clusters | t-SNE or UMAP, not as model inputs |
Traps interviewers probe
- Fitting the scaler or PCA before the split. The validation score comes out optimistic, and nothing crashes to warn you. Fit both on training rows only, then transform test rows with the fitted objects. In cross-validation, put them inside the pipeline so they refit per fold.
- Refitting the transforms on a new batch. A new reading must be centred and scaled with training means and standard deviations, then projected with the stored loadings. Refitting on the batch moves the components and breaks any downstream weights.
- Treating variance as usefulness. The top components keep the most spread, not the most signal for your target. Choose k with a validation metric, and use the cumulative ratio as a secondary check.
- Reading signs and axes as meaning. Flipped signs are normal across fits. Align a new component to the stored one before comparing or feeding a fixed weight. Do not name a component from its loadings without a check.
- Equating discarded variance with reconstruction error. On training rows with divisor n-1, the total squared error is (n-1) times the sum of discarded eigenvalues, not the bare sum.
Interview tips
Open with the assumptions: linear, variance-based, centred data. Then give the link: the top eigenvector of C maximises v^T C v under unit length, and the maximum is its eigenvalue. Name the SVD route and say why it avoids squaring the condition number.
Expect these follow-ups:
- Why are components orthogonal? Because C is symmetric, and its eigenvectors with distinct eigenvalues are orthogonal.
- Why is PCA the best linear compression? It minimises total squared reconstruction error among rank-k linear projections.
- Why standardise? Variance is measured in each feature's own units, so a large unit dominates.
- What goes wrong if you fit before the split? The test estimate is optimistic.
- When does whitening hurt? It amplifies small components, which are often noise.
30-second outline: PCA is a linear, unsupervised rotation onto the directions of greatest variance, computed from the SVD of the centred data. I standardise when units differ, fit on training rows only, and choose k with a validation metric, using cumulative variance as a guide. Reconstruction error is the discarded eigenvalues, scaled by n-1 on training rows. It fails on curved, categorical or label-driven structure, and there I would reach for kernel PCA, encoding or LDA.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.