Understanding Principal Component Analysis
Principal Component Analysis, usually called PCA, is a way of re-describing a dataset.
Start With the Picture, Not the Procedure
Principal Component Analysis, usually called PCA, is a way of re-describing a dataset. Instead of looking at the data through its original features, PCA rotates the data so that it is described by new features that are statistically uncorrelated with each other. It is often followed by a second step: keeping only the most important of those new features and discarding the rest.
The source material for this topic starts with a demonstration rather than with the theory. A plotting helper from the mglearn library generates a synthetic two-dimensional dataset, applies PCA to it, and shows the result in several panels. The first panel shows the original points. Later panels show the same points after PCA has rotated them, and after a reduction step has dropped one direction. As you read, your task is to work out what changes between panels and why those changes happen.
Before reading further, form a guess. If two features in a dataset tend to rise and fall together, what would you expect a rotation of the coordinate axes to do to that relationship? Hold your guess in mind while the next section traces the rotation step by step.
Tracing the Rotation of Correlated Features
Start with the state of the data before PCA touches it. In the generated example the two features are correlated by construction, because the second feature is built from the first. When you plot the points, they form a tilted cloud: knowing where a point sits along one feature tells you roughly where it sits along the other. The off-diagonal entries of the matrix that summarizes the two features are therefore clearly nonzero, which is the numerical signature of that shared movement.
The rotation changes the axes, not the points. Before the rotation is applied, the points form a tilted cloud relative to the original feature axes. As the axes line up with the principal components, the cloud settles so that it sits along the new axes, and the correlation between the two new features disappears. This is the moment to look for in a trace of the code: before it, the cloud is tilted; after it, the cloud lies along the axes.
Principal Components as Maximum-Variance Directions
PCA does not start by looking at the original columns. It starts by looking for directions in the data. The first direction it looks for is the direction of maximum variance: the direction, or vector, that carries the most information. Put another way, it is the direction along which the features are most correlated with each other. The algorithm labels this first direction Component 1.
The first component is chosen first because it captures the most information. The procedure then searches for the direction that carries the most remaining information while being orthogonal, meaning at a right angle, to the first one. In two dimensions only one perpendicular orientation exists, so the second component is forced into that single position. In higher-dimensional spaces many orthogonal directions exist, and the procedure chooses among them the one that carries the most of what is left.
The right angle is what keeps the new features uncorrelated. If the second direction were allowed to lean toward the first, the two new features would still move together to some degree. Forcing the directions to meet at a right angle is what removes that shared movement.
A component is drawn as an arrow, but where its head and tail sit does not matter. The same line can be drawn from the center outward in either sense. What matters is the line's direction and the amount of variance it captures.
PCA produces as many principal components as the original features. Each one is a direction in the feature space, not one of the original columns of the table.
Centering the Data Before Rotation
Before the rotation is applied, the mean is subtracted from the data. This moves the cloud of points so that it is centered around zero. The rotated representation that PCA produces is therefore centered around zero as well. Centering is a separate step from rotation, and it is easy to skip when you write the procedure yourself. The problem usually appears later, in the output, rather than at the point where the step was skipped.
Dropping a Component from Two Dimensions to One
PCA can also reduce the number of dimensions. After the components are found, you can retain only some of them. In the illustration the natural choice is to keep only the first principal component. That turns a two-dimensional dataset into a one-dimensional dataset.
Reducing a two-feature dataset to its first component
You have a two-feature dataset that has been centered and rotated into Component 1 and Component 2. You decide to keep only Component 1. What is the shape of the final result, and what has been lost?
- 1Identify the retained direction
Component 1 is the direction of maximum variance, so it is the most informative direction found. It is the one kept.
- 2Drop the second direction
Component 2 is discarded as a direction. The discarding applies to the direction itself, not to a column chosen from the original table.
- 3Project each point
Every point is projected onto the Component 1 line, so each point is now described by a single position along that line.
- 4Undo the rotation and add the mean back
The rotation is undone and the mean is added back, so the points again live in the original two-dimensional feature space.
- 5Read the final shape
The points now lie along the retained direction only, which is why the final panel looks like a line rather than a cloud.
The final representation is one-dimensional in the sense that every point lies along the retained line. It preserves the information held by the first component and discards what the second component carried.
The transformation is sometimes used to remove noise effects from the data, or to visualize which part of the information the retained components capture. The source material does not measure how much spread is lost in a specific case, so the amount lost depends on the particular dataset and should be checked rather than assumed.
Where a Reduced Result Goes Wrong
A reduced PCA result can look clean and still be wrong. The safest way to find the problem is to walk through the pipeline in order and find the first stage where the data stops matching what the source describes. The diagram below lays out those checkpoints.
Common mistake Skipping the mean subtraction before rotation
What it looks likeA line appears shifted away from where the original points were.
Why it failsPCA rotates centered data. Without centering, the rotated representation is not centered around zero, and it can diverge from the described result.
Do this insteadSubtract the mean first, and remember to add it back after the reduction.
Common mistake Treating a principal component as one of the original columns
What it looks likeReading Component 1 as if it were the Feature 1 column of the table.
Why it failsEach principal component is a direction in the feature space, not one of the original columns.
Do this insteadDescribe each component as a direction that carries a share of the variance.
Common mistake Assuming the second direction can lean toward the first
What it looks likeChoosing a second axis that is not at a right angle to the first.
Why it failsComponents are orthogonal by construction. Without the right angle, the new features can still move together.
Do this insteadKeep each later component orthogonal to the ones before it.
Common mistake Keeping too few components without checking the result
What it looks likeKeeping only the first component and presenting the line as if it captured everything.
Why it failsDiscarded components carry the information they hold, and the amount lost depends on the dataset.
Do this insteadInspect what the retained components capture before relying on the reduced representation.
Diagnosing a Shifted Line
You apply PCA to your own two-feature dataset and keep only the first principal component. The final result is a line, but it is shifted away from where the original points were. Using the checks in the order given in the pipeline above, which check should you run first, and what does the first failing check tell you?
Hints
- Start from the beginning of the pipeline, not the end.
- The line should lie where the centered data was, once the mean is added back.
- A correlation check after rotation tells you whether the new features are uncorrelated.
Explain in one or two sentences why keeping the first principal component preserves the most information, using the phrase direction of maximum variance.
Hints
- Think about what the first component is chosen to maximize.
- Consider what is discarded when the second component is dropped.
Recap of the PCA Procedure
- 1Subtract the mean so the data is centered around zero.
- 2Find the direction of maximum variance and label it Component 1.
- 3Find the next direction with the most remaining variance that is orthogonal to the first, and continue for the remaining components.
- 4Rotate the data onto the components so the new features are statistically uncorrelated.
- 5Optionally keep only the most important components, project onto them, undo the rotation, and add the mean back.
- 6Check each stage in order if the reduced result looks misleading.
Key Takeaways
- PCA rotates a dataset so that the rotated features are statistically uncorrelated, and it is often followed by keeping only the most important of the new features.
- The principal components are directions of maximum variance: Component 1 carries the most information, and each later component is orthogonal to those before it.
- There are as many principal components as original features, and each one is a direction in the feature space rather than one of the original columns.
- Keeping only the first principal component turns a two-dimensional dataset into a one-dimensional one, and the points end up lying along the retained direction once the rotation is undone and the mean is added back.
- A reduced result can mislead if the data was not centered before rotation, if the rotated features still correlate, or if the mean was not added back at the end.