Chapter 48PCA (Principal Component Analysis) › Example
Reading section 5 of 9 · Example · 5 minSection 5 of 9 · 5 min
Section 5 of 9 · 5 min read
PCA (Principal Component Analysis) › ExampleOne section · Sections in this chapter5 / 9 ↓
Sections in this chapter9
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let's work through PCA by hand on a tiny dataset. This will make the abstract formulas concrete. We'll calculate each step explicitly so you see exactly how the algorithm works.
Suppose we have four observations of two variables:
Each row is an observation, each column is a variable. Notice the strong relationship: the second variable is always one more than the first.
Step 1: Mean CenteringLink Copied
PCA requires mean-centered data. First, calculate the mean of each variable:
Mean of variable 1:
Mean of variable 2:
Now subtract these means from each observation:
Each column now has mean zero. The data is centered at the origin.
Step 2: Calculate the Covariance MatrixLink Copied
Next, compute the covariance matrix using . With observations, we divide by :
This is the covariance between variables 1 and 2. It's positive and large, confirming the strong positive relationship we saw.
This is the variance of variable 2.
The covariance matrix is:
Notice that the covariance equals the variances. This happens when two variables are perfectly correlated, as ours nearly are.
Step 3: Find Eigenvalues and EigenvectorsLink Copied
Now we solve the eigenvalue equation . Rearranging, we get , where is the identity matrix.
For non-trivial solutions (where ), the matrix must be singular, meaning its determinant equals zero. This gives the characteristic equation:
Substituting our covariance matrix:
The determinant of a 2×2 matrix is , so:
This factors as a difference of squares:
So or . These are our two eigenvalues.
Now find the eigenvector for each eigenvalue.
For :
Substitute into :
The first row gives , which simplifies to . So the eigenvector has equal components. To make it unit length, we need . Since , we have , so :
This is the first principal component. It points equally in both variable directions, capturing their shared variation.
For :
This gives , so . With unit length:
This is the second principal component. It points in the direction perpendicular to the first. Note that means this direction has zero variance, which makes sense because our data lies along a perfect line.
Step 4: Transform the DataLink Copied
Finally, project the data onto the principal components using :
Let's compute a few entries to see the pattern:
Following this pattern for all rows:
Look at what happened. The second column (second principal component) is all zeros. This makes perfect sense. Our original data was perfectly correlated, lying along a single line. All the variance is captured by the first principal component (the direction along that line). The second principal component, perpendicular to that line, has no variance at all.
If you wanted to reduce dimensionality, you could keep only the first column and represent your data in one dimension instead of two, losing zero information.