Principal Component Analysis – PCA

Two gauges on a machine usually rise and fall together. If you could keep only one number per reading, would you keep the first gauge, the second, or an average of the two? Principal component…

Hand-drawn scatterplot with the first principal component aligned along the direction of greatest variation.

Two gauges on a machine usually rise and fall together. If you could keep only one number per reading, would you keep the first gauge, the second, or an average of the two? Principal component analysis (PCA) finds the weighted combination with the greatest variance. It can replace several correlated measurements with fewer numbers, but what you discard matters: the difference between two gauges might be the first sign that one is failing.

What the arrows in a PCA plot mean

Each dot in the figure is one observation with two measurements. The purple arrow follows the long axis of the cloud, where the dots spread out most. The green arrow is perpendicular to it and measures the remaining variation. The arrows give directions; a point’s signed distance along a direction, measured from the center of the cloud, is its score on that component.

PCA

The covariance matrix in the figure has 5 on both diagonals: the two measurements have equal variance. The off-diagonal entries are 4: when one measurement is above its mean, the other tends to be above its mean too. The numbers 9 and 1 beside the arrows are eigenvalues, or variances along the new axes. They are not the slopes of the lines.

Calculate a component score by hand

Here are five pairs of gauge readings chosen to reproduce that covariance matrix, not the individual dots in the picture: (13, 23), (7, 17), (11, 19), (9, 21), (10, 20). Both gauges use the same units. Their means are 10 and 20. Subtract those means column by column to get (3, 3), (−3, −3), (1, −1), (−1, 1), and (0, 0).

For each centered column, the squared values sum to 20. Multiply the two values within each pair and the products sum to 9 + 9 − 1 − 1 + 0 = 16. For sample covariance, divide by the number of observations minus one: 20/4 = 5 for each variance and 16/4 = 4 for the covariance.

The first direction weights the gauges equally: (1, 1)/√2. Multiplying the covariance matrix by (1, 1) gives (9, 9), so the eigenvalue is 9. For the perpendicular direction (1, −1)/√2, the eigenvalue is 1. The first component therefore accounts for 9/(9 + 1) = 90% of the variance. Reversing either arrow changes the signs of its scores, not the variance it captures.

For the first reading, the first score is (3 + 3)/√2 ≈ 4.24. For (11, 19), it is (1 − 1)/√2 = 0. If we keep only that first score, (11, 19) reconstructs as the mean pair (10, 20). We kept the shared motion but lost the disagreement between gauges.

When to center and when to scale

Centering subtracts a feature’s mean; it does not change the feature’s units. If variation in the original units matters, as it does for two gauges measuring the same quantity, PCA on centered readings is a reasonable choice. Now imagine recording one gauge in millimeters and the other in meters. The change of units alone can rotate the first component toward the column with larger numbers.

If a one-standard-deviation change should count equally for each feature, subtract each mean and divide by that feature’s standard deviation before PCA. Check for constant columns first: they contain no variation and have no nonzero standard deviation to divide by. Scaling is a modeling choice, not a requirement; it can give a noisy, low-variance gauge as much influence as a reliable one. The scikit-learn PCA reference makes the default explicit: the estimator centers its input but does not scale each feature.

How many components should you keep?

For compression, plot cumulative explained variance as you add components, then compare reconstruction errors with a tolerance in the original units. One component meets a 90% variance target in the gauge example. If you need to detect small disagreements, however, the discarded component is exactly the one you need. For prediction, compare several component counts with a no-PCA baseline using the same validation splits and a metric that reflects the cost of a missed prediction.

Split before fitting. Set aside the test observations first. Within each cross-validation split of the remaining data, fit any scaler and PCA on that fold’s training rows only. Use those fitted means, scales, and directions to transform the validation rows. After selecting the component count, refit the chosen pipeline on all training data and evaluate it once on the untouched test set. Even without labels, fitting PCA before splitting lets the held-out measurements influence the axes. The scikit-learn guide illustrates how pipelines prevent preprocessing leakage.

For more than two features, you need not form the covariance matrix by hand. Applying singular value decomposition (SVD) to the centered data matrix gives the same principal directions as its covariance matrix: they are the right singular vectors when rows are observations. The NIST engineering statistics handbook gives the statistical background for principal components.

𝓑𝓎: 𝓜𝓸𝒽𝒶𝓃𝒶𝒹 𝒜𝒷𝓊𝓀𝓂𝓮𝒾𝓁 𝒫𝒽.𝒟. 𝓮𝓍𝒸𝓮𝓇𝓅𝓉𝓈