Unsupervised Learning
Principal Component Analysis (PCA)
For a fixed centred dataset, variance kept plus mean squared orthogonal reconstruction error is constant; PCA chooses a direction that keeps the most variance.
Eleven observations centred on (0.13, 0.24), total variance 7.802. No axis yet.
- Observation
- Centroid
- Selected reconstruction
- Projection axis
- Axis direction handle
- Discarded distance
- Variance kept
- Variance lost
Row 0: centred (-3.627, -3.036).
Cloud axes use equal feature-space scales, expanding bounds for the measured aspect and every possible reconstruction. Strip bounds stay fixed while rotating each dataset. Errors use the transformed feature units. Population divisor n throughout; squared error sums both coordinates per row.
Where should the projection pivot?
Inspect a row: subtract the centroid to describe spread rather than position. The original eleven observations have total population variance 7.802.
- Axis angle
- Projected positions
- Variance kept
- Variance lost
Centring
Subtract each feature mean before measuring spread.
Check this stage
Check your understanding
Rotate a centred projection and inspect the score, reconstructed point and squared error.
What does PCA keep?
PCA keeps directions of greatest centred variance. For a fixed dataset and Euclidean feature geometry, the leading components also minimise squared reconstruction error. That objective says nothing about which features matter for a prediction task. Stanford's PCA notes derive the projection and reconstruction viewpoints.
Start with The Dataset and select an observation. Subtract the displayed mean to obtain its centred coordinates. At Projection, drag the square direction handle or use Axis Angle. The range stores radians and displays degrees; the handle's arrow keys move in feature units. Follow the selected row into the score strip and the reconstructed diamond. The cloud uses equal feature-space scales, so perpendicular residuals also look perpendicular on screen. Its bounds expand to fit the measured aspect without cropping reconstructions.
From a score back to a point
For unit direction , centre and observation :
The single stored score is signed. The reconstruction lies on the axis through the mean. Under the hood retains raw, scaled, used and centred coordinates, score, reconstruction, residual and squared Euclidean error for every row. The highlighted diamond links the selected row to this calculation.
A hand example is the Round square fixture: (1,1), (3,1), (1,3), (3,3). Its mean is (2,2). At horizontal direction (1,0), the first centred row is (−1,−1), its score is −1, and reconstruction is (1,2). The residual is (0,−1), whose squared length is 1. Every row has error 1, so the mean squared reconstruction error is 1.
Where does the lost variance go?
At Variance & Error, the dashed drops show what one-dimensional reconstruction cannot retain. The two bars obey:
This lab uses population divisor n for covariance, captured variance and error. Each row's error sums its two squared residual coordinates; it is not divided by the number of features. The total remains constant while rotating the current dataset. Changing the data or feature transformation can change that total.
On the original eleven observations, total variance is 7.802479. Exact PC1 is 43.282°, capturing 7.523124, leaving 0.279355 and explaining 96.4197%. Snap to PC1 uses the closed-form 2D direction. The challenge requires the original unscaled, unstandardised fixture, ratio above 96.2% and angle error below 0.05 radians. The ratio requirement is slightly stricter near the edge of that angular tolerance.
The scikit-learn PCA API uses sample divisor n−1 for reported component variances. For n>1, multiply this lab's covariance eigenvalues by n/(n−1) to compare those variances. Directions and explained ratios agree. Do not compare a sample eigenvalue directly with population mean squared reconstruction error.
Break the direction, then recover
At Principal Component, load minimum-variance projection. Near −46.7° on the original fixture, only 3.6% of variance remains. At the exact quarter turn from PC1, the kept and lost values exchange places. The preset is snapped to the manual grid, so the swap is approximate. This is a valid but poor projection for this objective, not a diverging numerical algorithm.
Restore experiment recovers the preceding settings, stage, selected row, prediction and guided/free mode, even after further edits. Reset controls restores default settings, keeps the guided stage and clears recovery. Share restores the displayed stage and fixed experiment; an exact snapped PC1 is recomputed. Predictions and recovery stay in memory; exploration progress stays on this device.
Why do units matter?
Increase x multiplier on the same rows, then compare Standardise features. Multiplying x by four multiplies its marginal variance by sixteen. PCA centres its input but does not automatically standardise features, as illustrated in the scikit-learn scaling example.
This lab's optional standardisation subtracts each scaled feature mean and divides by its population SD before PCA. A zero-SD column uses divisor one and remains zero after centring. Errors then belong to standardised coordinates, not original units. Standardising gives nonconstant features equal marginal variance; deciding whether that is useful depends on the problem. It is not a universal repair.
When is there no unique principal direction?
The unscaled Round square has covariance diag(1,1), equal eigenvalues and total variance 2. Every axis captures 1 and leaves error 1. There is no unique PC1. Identical observations have zero total variance: reconstruction error is zero, but the explained ratio is undefined, not evidence of 0% or 100% explained variance.
The lab treats an eigenvalue gap at most times the trace as numerically ambiguous and withholds Snap to PC1. Otherwise the axis is unique up to sign; reversing its sign reverses scores without changing reconstruction. Near-equal eigenvalues can make directions sensitive to small perturbations. Covariance eigenvalues and status remain inspectable. Uncorrelated component scores need not be independent.
Does high variance mean useful information?
No. A low-variance direction can carry a target signal. The scikit-learn PCR versus PLS example constructs exactly that case. PCA uses no target when choosing directions. Nor does it know which components are noise. Outliers can strongly affect its squared objective.
Ordinary regression minimises vertical prediction residuals; PCA minimises orthogonal reconstruction residuals in the chosen feature geometry. They can coincide in special cases. Centring prevents displacement from the origin from contaminating the covariance; an uncentred second-moment direction can differ from the centred principal direction.
Stage recap
The Dataset
Inspect a row: subtract the centroid to describe spread rather than position. The original eleven observations have total population variance 7.802.
Projection
Rotate the direction and follow the selected row into the score strip and reconstructed diamond. The score is a signed dot product of the centred row with a unit direction.
Variance & Error
Each dashed drop is a reconstruction residual. Square its feature-space length and average over rows. Kept variance plus this error equals the current total.
Principal Component
Snap to PC1 or hunt by hand. Then change x units or standardise. A round cloud has no unique best axis; identical observations have no defined explained ratio. High variance does not certify task relevance.
Reference
- Centring
- subtract the mean first — PCA describes spread, not position
- Captured variance
- (1/n) Σ ((xᵢ − x̄)·u)² for unit direction u
- Orthogonal error
- (1/n) Σ (perpendicular distance to the axis)²
- The identity
- captured + lost = total variance, whatever the angle
- Explained ratio
- captured ÷ total; undefined at zero total
- PC1
- largest population covariance eigenvalue selects the direction; ties are ambiguous
- Not regression
- regression minimises vertical error; PCA minimises perpendicular
- Scale matters
- feature units change the objective; standardisation is an optional modelling choice
Break it on purpose
On the original unscaled data, rotate near −46.7 degrees: only 3.6% of variance remains. This is a valid low-variance projection, not numerical divergence. Scale changes and tied eigenvalues expose different limits.
Explore next
- Autoencoder (Bottleneck Compression)Change the bottleneck width and inspect how many shared templates the decoder can use to rebuild an image.
- K-Means ClusteringAssignment holds centres fixed; updating holds memberships fixed. Both minimise the same squared-distance objective, but a fixed point need not be globally optimal.
- Embeddings & Vector SpaceOriginal vectors and a chosen metric determine retrieval; a coordinate map and camera determine the picture.