Principal Component Analysis (PCA) is a valuable statistical method used to take high-fidelity data, capture the “most important parts” of the data, and represent them in lower fidelity. This is widely useful for applications like machine learning where the curse of dimensionality significantly increases the amount of data you need.
Eigenvectors
From linear algebra, we say that matrices encode linear transformations of space. That means given a matrix , it tells you where each vector lands in after the transformation. A linear transformation is geometrically interpreted as one where grid lines remain parallel and evenly spaced, and the origin is left fixed in place.
A linear transformation can do three things: rotate, scale, and shear. For a given transformation encoded by , there are a few vectors that might only scale (not rotate or shear) after the transformation is applied. These special vectors are called the eigenvectors of . The amount they scale is known as the eigenvalue (often denoted as ). Formally, is an eigenvector of if the following equation holds for some (and conventionally ).
Eigendecomposition
Given a matrix , we can represent it in terms of its eigenvectors/eigenvalues if the following conditions hold:
- independent eigenvectors exist
If the above hold, we can factor into the following:
where is a matrix containing each of the eigenvectors in its columns, and is a diagonal matrix where is the eigenvalue for the i-th column of .
Intuitively, this entire process can be viewed as a change of basis. Imagine we take a vector and transform it by :
Multiplying by translates from being represented by the standard basis vectors to being represented in terms of our eigenvectors. This means the first coordinate of scales the first eigenvector, the second coordinate scales the second eigenvector, and so forth.
Since we know that only scales our eigenvectors, is diagonal. applies the scaling factor for each eigenvector where scales the first coordinate, scales the second coordinate, and so on.
Finally, multiplying by transforms the vector given by from being represented in terms of our eigenvectors to being represented back in terms of our original standard basis vectors.
Given this explanation, can you see why the two constraints above had to hold? The answer is that non-square matrices don’t have eigenvectors. If with , then while , so it is impossible to find a and that satisfy the equality since the two sides live in different dimensions. Additionally, independent eigenvectors must exist for there to be a valid inverse of . The intuition is that having a matrix with dependent columns squishes space to lower dimensions, and there is no inverse matrix that can undo the transformation from higher to lower dimensionality space.
One useful property of eigendecomposition is it lets you efficiently compute the powers of a matrix:
and in general
Taking the n-th power of a diagonal matrix just involves taking the n-th power of each of the diagonal terms. This is much more efficient to compute.
Singular Value Decomposition (SVD)
SVD operates on the key insight that you can express every linear transformation as the combination of applying a rotation to the input space, scaling along the resulting axes, and applying another rotation to land in the output space. Given any matrix , SVD says we can decompose it into the following:
where , , , and and are orthogonal matrices. Since and are orthogonal, we know that they encode rotations/reflections. is a diagonal matrix whose entries (the singular values) encode the stretch along each axis.
In order to see how SVD works geometrically, imagine the unit circle in and transform it by . Functionally, this just means take a bunch of vectors that represent the circle and transform each by and observe the result.
In order for us to decompose a matrix into a combination of rotation, stretch, rotation, we must choose orthogonal vectors such that they remain orthogonal after the transformation (if this weren’t the case, it would be impossible to find a that was just a rotation/reflection matrix). These vectors happen to be the ones that stretch the most. Intuitively, this is because vectors are pulled unevenly towards the strongest stretching directions, so vectors already along the strongest stretching directions preserve relative angles with each other.
To find which vectors stretch the most, let’s try to find what maximizes the value of :
This implies that the eigenvectors of are the directions that stretch the most. The reason eigenvectors of get stretched the most by is because eigenvectors only get stretched (not rotated), so there is no “wasted” effort going into rotation. All of the matrix transformation goes towards stretching. These eigenvectors represent , and you order the eigenvectors in such that each subsequent column has an eigenvalue less than or equal to the previous column.
To uncover , observe the following property:
by the fact that as is an orthogonal matrix.
But since we said is just a diagonal matrix (as it only scales ), we can easily compute both and by taking the result of . Then make each column of the result unit length and store it as the i-th column of . The i-th entry of becomes the reciprocal of the scaling factor we used to scale the i-th column of to unit length.
So effectively, we can capture the transformation of applied to () by performing the following:
- Multiply by . This expresses in terms of the input vectors that get stretched the most. Again, this can be seen as a change of basis where multiplying by expresses in terms of the principal stretching directions.
- Apply the stretch encoded by . scales the first principal stretch direction, scales the second, and so forth.
- Perform one final rotation to express the scaled result of step 2 in the output space. This converts the vector being represented by the principal stretch direction back into the output space.
Principal Component Analysis (PCA)
PCA is one of the most widely used applications of SVD. PCA is a statistical technique used to (lossily) compress a high-dimensional dataset into fewer dimensions.
To understand how PCA works, let’s switch gears. Instead of viewing a matrix as a linear transformation, let’s view as a convenient way to package data. Each sample is a row in and the columns represent features.
The first step in PCA is to center the data by subtracting the mean for each feature (columns of ). The intuition is that PCA cares about the relationship amongst the variables, not the actual values of the variables themselves. Denote the centered matrix as .
Once the data is centered, a natural next question is: how do we extract the most significant patterns in the data to represent the original dataset in lower dimensions? That’s where SVD comes into play. Recall the formula for SVD, applied to our centered matrix:
where , , , and and are orthogonal matrices.
Remember how we said that is just a collection of the eigenvectors of ? Well, when we view as a centered data matrix, happens to be proportional to the covariance matrix because dots every feature with each other feature. The eigenvectors of the covariance matrix, sorted by eigenvalue, give us an ordered list of directions such that projecting the data onto each eigenvector captures the largest remaining variance not already captured by the previous directions. Each of these directions is known as a “principal component.” Sneak peek: Knowing the directions of maximum variance is helpful because we want to capture the “most important” parts of the data. What makes each data point unique amongst all the others is where they fall along the line of maximal variance.
is composed of the eigenvectors of the matrix , which is the covariance of the rows. To see this, take a look at this proof:
Looking at the final form, this is exactly the formula for eigendecomposition. This implies that the columns of are the eigenvectors of . In the context of PCA, we say that helps us combine the principal components in to recover the original samples. Concretely, the first row of tells us how much to encode the specific principal components given by to recover the first sample, the second row of tells us how much to encode the specific principal components given by to recover the second sample, and so forth.
once again corresponds to the stretching factor. is organized so that higher occur first.
To perform compression, you choose the top-k columns of , , and and represent them as , , and . You can then either do:
or
The first multiplication projects each datapoint onto the most important principal components. This is useful because we retain the parts of the data that account for the most variance. is equivalent to the first form, which you can see by right-multiplying both sides of by .
Application to MNIST
Here’s a visualization of performing PCA (with ) on the MNIST dataset. MNIST is a collection of images of handwritten digits from 0-9. You’ll see that clusters emerge for each digit, showing that PCA helps capture the internal structure of the data.