Principal component analysis
Principal component analysis computed live, one power iteration per step: centering, covariance, axes, deflation, projection, explained variance and reconstruction, in 2D, 3D and on iris.
Updated
- Variance and covariance
- Eigenvectors as directions of spread
- Power iteration and deflation
- Projection and reconstruction
- How many components to keep
Intuition
Find where the data spread most
A tilted, elliptical cloud of points varies a lot along both axes, but the two variations are one and the same: the points lie along a diagonal line. PCA rotates the axes onto that line: along it (the first principal component) the variance is largest, and perpendicular to it (the second) little is left. Dropping the second coordinate then loses almost nothing.
The three small labs below run power iteration for real in your browser. The orange arrow is the direction being iterated, turning step by step towards the direction of largest variance; the black arrows are components already found, of length .
First: 2D points lie along a diagonal; power iteration finds it in 6 iterations, and it explains 95.99% of the variance, leaving 4% for the second direction.
Second: 3D data seen from above. The first component explains 88.3%, the second 11.1% and the third only 0.6% — the cloud is nearly a flat ellipse, so two components keep 99.36% of the variance.
Third: the famous iris data, with 4 features (sepal and petal length and width), standardised and projected onto the first two components. The three species (colours) roughly separate along the first component alone — without PCA ever seeing the species labels.
Math
Centring and covariance
samples, features. Subtract each feature's mean (optionally divide by its standard deviation) to get , then compute the sample covariance:
is the covariance of features and , the diagonal holds the variances, and the trace is the total variance.
Components are eigenvectors
Find the unit vector that maximises the projected variance . A Lagrange multiplier gives : the direction of greatest variance is the eigenvector of the largest eigenvalue, and that variance is . The next component maximises variance among directions perpendicular to the first — the second eigenvector. Component 's explained-variance ratio is .
Power iteration
Instead of a general eigenvalue routine, this page multiplies step by step:
Decompose along the eigenvectors: each multiplication by scales the -th component by , so after normalising, every component except the largest shrinks by . The speed is therefore set by . The lab measures convergence by the change in direction , which shrinks by roughly :
- the default ellipsoid has : the change goes , about ×0.016 per step, converging in 9 iterations;
- the near-flat data have and need 21.
After each component comes deflation: removes that direction's variance, and iteration restarts from a point perpendicular to the components found; what was the second largest is now the largest.
Projection and reconstruction
Keep the first components ():
The reconstruction error equals exactly the sum of the discarded eigenvalues (times ).
Standardisation
| Iris | PC1 | PC2 |
|---|---|---|
| Centred only | 92.46% | 5.31% |
| Standardised | 72.96% | 22.85% |
Unstandardised, the first component is about — mostly petal length, the feature with the widest range and hence the largest variance. Standardised, every feature has variance 1, all four compete equally, and the first component's share drops to 73%. Which is right depends on whether the units are comparable; with different units (metres and millimetres, money and head-counts) standardise.
How it is computed, with real numbers
Once the lab above has loaded, this panel works through the same formulas with its current numbers.
Experiment
Follow the lesson
Course
Lesson 1: power iteration and variance
5 steps · each one: set up and run → look → answer
1. The direction turns
2D correlated data, 3 steps. Watch the white dashed line (the vector being iterated) in "Original space".
2. Three components
The 3D ellipsoid, run to the end. Open "Explained variance".
3. Fast and slow convergence
Switch to "Near-flat" and watch the convergence chart.
4. Iris
Iris, centred only. Look at PC1's direction and share.
5. Standardise
Set scaling to "Standardized" and look at the component space.
Try these settings
Exercises
Exercises
Exercise 1: which data are almost one-dimensional?
Keep a single component without standardising. Change the dataset so that it keeps at least 95% of the variance.
Hint
2D correlated: 95.99%. Iris gives 92.46%, the ellipsoid 88.3%, near-flat only 68.5%.
- Variance kept (1) ≥ 0.950
Exercise 2: stop the first component dominating
With one component, iris keeps 92.46% of its variance. Change one setting to bring it to 75% or below.
Hint
Set scaling to "Standardized": 72.96%.
- Variance kept (1) ≤ 0.750
Quiz
Further reading
Frequently asked questions
What are the principal components in PCA?
The eigenvectors of the data's covariance matrix, ordered by eigenvalue (variance) from largest to smallest. The first is the direction of greatest projected variance; each subsequent one has the greatest variance among directions perpendicular to those before it.
How many principal components should I keep?
Look at the cumulative explained variance: keeping 90% or 95% is common, or look for the bend in the scree plot. On this page's 3D ellipsoid, two components already keep 99.36%.
Should I standardise before PCA?
Yes, when features have different units or very different ranges; otherwise the large-valued features dominate. Unstandardised, iris's first component is almost pure petal length (92%); standardised it holds 73% with contributions from all four features.
How are PCA and SVD related?
Take the singular value decomposition of the centred data, Z = UΣVᵀ: the columns of V are the principal directions and the eigenvalues are λ = σ²/(n − 1). scikit-learn implements PCA with the SVD, which is numerically more stable; this page uses power iteration to show every step, with matching results.
Can PCA handle nonlinear structure?
No, it finds linear directions only. Curved manifolds call for kernel PCA, t-SNE, UMAP and similar methods.
Further reading
- Pearson, On lines and planes of closest fit to systems of points in space, 1901: the origin of PCA.
- Hotelling, Analysis of a complex of statistical variables into principal components, 1933: where the name comes from.
- Jolliffe, Cadima, Principal component analysis: a review and recent developments, 2016.
- Golub, Van Loan, Matrix Computations, chapter 8: the symmetric eigenvalue problem and power iteration.
- scikit-learn, PCA: this page's reference implementation.