Principal Component Analysis Explained With a Worked Example

Principal Component Analysis cover with a copper ruler aligned to an elongated cloud of dots on graph paper.

If two sensors usually rise and fall together, each reading tells you some of what the other one already told you. Principal component analysis (PCA) looks for combinations of readings that capture that shared movement. You can keep a smaller set of those combinations and still retain much of the variation in the original data.

PCA creates new features called principal components. The first component captures as much variance as possible along one direction. Each later component captures the most remaining variance along a direction perpendicular to the earlier ones. Keeping fewer components reduces the number of columns.

Work through PCA with four sensor readings

Imagine four observations from two sensors measured in the same units. The readings are (8, 9), (9, 8), (11, 12), and (12, 11). Both sensor averages are 10. Subtracting those averages gives (−2, −1), (−1, −2), (1, 2), and (2, 1). This centering puts the middle of the observed points at the origin.

The points spread out most along the diagonal where both sensors increase together. A perpendicular direction captures the difference between sensors. To give both directions unit length, divide the sum and the difference by √2. Those are the two component scores in this particular example.

Original readingsCentered readingsComponent 1Component 2
8, 9−2, −1−3/√2−1/√2
9, 8−1, −2−3/√21/√2
11, 121, 23/√2−1/√2
12, 112, 13/√21/√2

The first component has nine times the variance of the second, so its explained variance ratio is 9 ÷ (9 + 1) = 90%. Keeping only the first component turns four two-column observations into four one-column scores. It also makes (8, 9) and (9, 8) indistinguishable. Both reconstruct to (8.5, 8.5) when the missing component is set to zero.

This example shows the decision behind dimensionality reduction. If the job is to track the overall sensor level, one component may be enough. If the job is to catch disagreement between sensors, the lower-variance component carries exactly the information you need.

What the axes mean in a PCA plot

A principal component is a weighted combination of the original columns. Its direction tells you which features contribute and how their contributions vary together. A component score tells you where one observation lands along that direction. In the iris example, four measurements become three component scores for plotting.

Three-dimensional PCA projection of iris measurements, with points colored by species.
Four iris measurements are projected onto three principal components. Chart by the scikit-learn developers.

If you color the points by species after the transformation, you can see which species overlap in the projection. PCA computes the axes using the measurements. The species labels are used afterward to color the plot. Scikit-learn iris PCA example.

Decide whether to scale before fitting PCA

Centering subtracts each feature mean. Standardization also divides each feature by its standard deviation. Scikit-learn PCA centers the input and leaves feature scaling to your preprocessing. Scikit-learn PCA reference.

If one column measures meters and another measures millimeters, the column with the larger numeric spread can dominate PCA even when its underlying variation is comparable. Standardizing gives the features comparable variance before PCA searches for directions. When all columns share a meaningful scale, preserving their original variance differences can be the right choice.

Two wine dataset PCA plots compare unscaled features with standardized features, which show clearer separation in this example.
The unscaled projection is on the left and the standardized projection is on the right. Chart by the scikit-learn developers.

The wine dataset example shows how the first two component scores change with standardization. It is a useful comparison to make on your own data: plot both versions and check which source features dominate the first component. Scikit-learn example on feature scaling.

Choose the number of components for the task

For visualization, start with two components and check how much variance they capture. A shallow projection can hide separation that exists elsewhere. For compression, use cumulative explained variance as a starting point and check reconstruction error on examples you care about.

For prediction, compare a model using PCA with a baseline using the original features. Tune the number of components using validation data and your actual task metric. If losing a low-variance feature damages recall, a high explained variance ratio will not make up for it.

Fit scaling and PCA on the training data, then apply the learned transformation to validation and test data. During cross-validation, fit both steps inside each training fold. A pipeline can enforce that order. Unlimited AI guide to choosing a cross-validation split.

Common questions

Does PCA select existing features?

PCA builds new features from weighted combinations. If you need to retain interpretable raw columns, compare PCA with feature selection, which keeps a subset of the original variables.

Does 90% explained variance mean 90% prediction accuracy?

Explained variance describes how much feature variation the retained components represent. Prediction accuracy describes how often a classifier gets labels right. Measure the two separately and evaluate on held-out data.

Why did the component signs flip?

A component axis can point in either direction. Reversing its sign also reverses its scores, leaving the represented line and the reconstructed data unchanged when the transformation is used consistently. Compare component directions with that sign ambiguity in mind.

Before keeping a PCA transformation, write down what the smaller representation is for and how you will measure the cost of losing information. Then check whether the reduced data serves that purpose on new examples.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top