Visual explainer
Dimensionality Reduction
Work through PCA centering, variance, projection and reconstruction, then see how retaining most variance can erase class information.
Dimensionality reduction replaces many coordinates with fewer. Principal Component Analysis (PCA) chooses linear directions that retain as much variance as possible. It makes data smaller, but retained variance isn't the same as retained task information.
Our synthetic example starts with four samples and two features. Rounded coordinates are A = (3.707107, 5.121320), B = (5.121320, 3.707107), C = (0.878680, 2.292893), D = (2.292893, 0.878680). Both axes use equal units. The cross marks their mean, μ = (3, 3). The tilted cloud suggests that much of the two-feature variation follows one direction.
Center Before Choosing Directions
Subtract the training mean from every sample. The cloud now surrounds zero without changing its internal distances. This view uses the same 45 pixels per coordinate unit as the opening plot. The dashed axes are the original feature directions; the solid diagonals are perpendicular component directions. Their displayed line lengths identify directions, not variance magnitudes.
The first direction is u = (1, 1)/√2. The second is v = (−1, 1)/√2. Both have length one and their dot product is zero. In this basis, our four centered samples have component coordinates A = (2, 1), B = (2, −1), C = (−2, 1), D = (−2, −1). Those exact values define the rounded original coordinates above.
The PCA guide describes orthogonal directions ordered by variance. Centering is part of this PCA setup; scaling each feature is a separate choice.
Count Retained Variance
Let t be the coordinate along PC1 and s the coordinate along PC2. Both score means are zero. With four samples, sample variance divides the squared deviations by 4 − 1:
PC1 therefore retains (16/3)/(20/3) = 80% of total variance; PC2 accounts for 20%. Each outlined track represents 100% on the same scale. These percentages belong to this fixture. Dropped variance needn't be noise, and there's no universal retention percentage for a given number of dimensions.
Project Onto the Line
A's centered coordinates are (1/√2, 3/√2). Its score is the dot product t = u·(A − μ) = 1/2 + 3/2 = 2. The diamond is the projected point tu = (√2, √2). The dashed segment drops perpendicularly to the retained line; its right-angle marker identifies an orthogonal projection.
This close-up uses 65 pixels per feature unit rather than 45. PCA keeps a coordinate along its chosen line, not one of the original features. Projection can shorten distances, including reducing a nonzero distance to zero.
Store One Score per Sample
The reduced data has four rows and one column: scores [2, 2, −2, −2]. A and B share score 2; C and D share score −2. Each overlapping circle-and-diamond mark deliberately represents two samples at exactly the same coordinate. No plotting jitter separates them.
The ruler uses 80 pixels per score unit. Storing one score per sample reduces the data representation from eight scalars to four, though the fitted mean and basis also need storage. With only four samples that overhead matters; this drawing isn't a compression benchmark.
Transform a New Sample
The triangle is a new sample x = (3 + √2, 3), outside the training set. Subtract the fitted mean to get (√2, 0), then take the dot product with u: t = 1. Its diamond sits at u in centered coordinates. This view uses 80 pixels per feature unit.
Keep the training mean, feature preprocessing and basis fixed when transforming held-out data. Computing the dot product has a cost; it doesn't require refitting PCA. The PCA API provides both transform and inverse-transform operations.
Other reduction methods optimize different goals. Neighborhood-oriented visualization methods such as t-SNE don't promise a fixed fraction of preserved neighbors or globally faithful gaps. See t-SNE and UMAP for that separate mechanism. A two-dimensional plot alone doesn't establish clusters or anomalies.
Measure Reconstruction Error
Reconstruction adds the mean back to the retained component: x̂ = μ + tu. For A, the centered reconstruction is (√2, √2), marked by the diamond. The circle is the original centered A. Their difference is v = (−1/√2, 1/√2), whose length is one. The parallel mint bracket measures that residual at 80 pixels per unit.
Each of the four samples loses a PC2 score of magnitude one, so the total squared reconstruction error is 1 + 1 + 1 + 1 = 4. This is loss of actual coordinates, regardless of whether that lost information matters to the task. For the new sample from the preceding scene, reconstruction is (3 + 1/√2, 3 + 1/√2), also one unit from its original position.
Units Change the Choice
Our centered sample covariance is [[10/3, 2], [2, 10/3]], giving PC1 at 45°. Multiply only the first feature by ten and its covariance becomes [[1000/3, 20], [20, 10/3]]. The new leading unit direction is approximately (0.998182, 0.060275), just 3.455614° above the horizontal.
These paired plots show unit direction vectors at the same drawing scale, not transformed data clouds. The scaling example demonstrates how feature scales affect PCA. Standardization is one possible choice, but it changes the question being optimized. Fit any scaling on training data only.
Keep More Components
Both panels reconstruct the same four centered samples at 45 pixels per unit. With only PC1, two pairs coincide; nested rings show those duplicates. Keeping both scores reconstructs each point as tu + sv, recovering all four original positions in this two-feature toy dataset. Total squared error falls from 4 to 0, up to numerical rounding.
The trade-off is explicit: retaining both components stores two scores per sample and provides no dimensional reduction here. In a larger dataset, choose the component count using the storage budget, reconstruction tolerance and downstream validation, rather than treating any variance cutoff as universally sufficient.
Where It Breaks
Now reveal a label that PCA never used: circles have s = +1; diamonds have s = −1. The two classes are separated along PC2 in the original centered plane. Dropping PC2 puts one circle and one diamond at score −2, and another opposite-class pair at score 2. The amber overlaid shapes show the collision.
No classifier receiving only t can distinguish the opposite labels within each pair. With one example of each class at each score, any deterministic label choice gets two of these four samples wrong. Yet the representation retained 80% of variance. PCA optimizes spread, not class separation. Preserve the needed feature or validate another representation; a nonlinear map doesn't automatically recover information already discarded.
The Quick Version
- PCA centers data and ranks orthogonal directions by variance.
- Projection stores scores along retained directions.
- This fixture keeps 80% of variance with one component.
- Discarded components cause measurable reconstruction error.
- A fitted mean and basis can transform new samples.
- Feature units affect which direction PCA selects.
- High retained variance can still erase the useful class distinction.