Principal Component Analysis
An image can have millions of pixel values. If many of those values vary together, we may be able to describe their variation with far fewer numbers. Using fewer coordinates would reduce the work of storing and comparing images, and with two or three coordinates, we could plot them.
In low-rank approximation, we used the SVD to approximate a matrix using fewer directions. Principal component analysis (PCA) uses those directions as coordinates for our data. We keep each example’s coordinates along the leading singular vectors and discard the rest.
Encoding and Reconstruction
Put \(n\) examples with \(d\) features into a matrix \(\mathbf X\in\mathbb R^{n\times d}\), with example \(i\) in row \(\mathbf x_i^\top\). We’ll assume the columns are centered, i.e., we’ve already subtracted each feature’s mean. Zero then represents an average example, and the coordinates describe how each observation differs from that average. Without centering, a large shared offset could dominate the approximation even if it tells us little about differences between examples.
Let \(r=\operatorname{rank}(\mathbf X)\) and choose \(1\leq k\leq r\) with \(k<d\). Collect the first \(k\) right singular vectors in \(\mathbf V_k\in\mathbb R^{d\times k}\). These are the principal directions, and they’re orthonormal: \(\mathbf V_k^\top\mathbf V_k=\mathbf I_k\). Taking an inner product with each direction gives an example \(k\) new coordinates. A direction can combine many original features, so we can compress the data without simply deleting columns of \(\mathbf X\).
Call this operation the encoder \(E\), and put the resulting embeddings \(\mathbf z_i^\top\) in the rows of \(\mathbf Z\in\mathbb R^{n\times k}\). A decoder \(D\) uses those coordinates to reconstruct the original features, giving \(\widehat{\mathbf X}\in\mathbb R^{n\times d}\):
\[ \mathbf Z=E(\mathbf X)=\mathbf X\mathbf V_k,\qquad \widehat{\mathbf X}=D(\mathbf Z)=\mathbf Z\mathbf V_k^\top. \]
The decoder adds the principal directions together, weighted by the embedding coordinates. Its output lies in their span. Any component perpendicular to that span has zero inner product with every principal direction, so it contributes nothing to the embedding and cannot be recovered by the decoder.
Below, we’re compressing three-dimensional points into two coordinates. On the left, \(\mathbf x_i\) projects onto the principal plane at \(\widehat{\mathbf x}_i\); the dashed segment is the discarded component. On the right, \(\mathbf z_i\) gives the two coordinates of that projection within the plane. Dragging the left view changes the camera angle, while the embedding stays fixed.
We use the same principal directions to encode new examples. Subtract the training feature means, then multiply by \(\mathbf V_k\). After decoding, add those means back to return to the original units. Using the same means and directions puts new examples in the same coordinate system as the training data.
Optimality
We measure reconstruction error by adding up the squared error across all features and examples:
\[ \|\mathbf X-\widehat{\mathbf X}\|_F^2 =\sum_{i=1}^n\|\mathbf x_i-\widehat{\mathbf x}_i\|_2^2. \]
Write the rank-\(r\) SVD as \(\mathbf X=\mathbf U_r\boldsymbol\Sigma_r\mathbf V_r^\top\). Multiplying by \(\mathbf V_k\) keeps the first \(k\) terms because each right singular vector has inner product one with itself and zero with the others. Encoding and decoding therefore give
\[ \mathbf Z=\mathbf X\mathbf V_k=\mathbf U_k\boldsymbol\Sigma_k,\qquad \widehat{\mathbf X}=\mathbf Z\mathbf V_k^\top=\mathbf U_k\boldsymbol\Sigma_k\mathbf V_k^\top=\mathbf X_k. \]
The reconstruction is exactly the truncated SVD. By Eckart–Young–Mirsky, it minimizes Frobenius error among all matrices of rank at most \(k\):
\[ \|\mathbf X-\widehat{\mathbf X}\|_F^2 =\min_{\operatorname{rank}(\mathbf B)\leq k}\|\mathbf X-\mathbf B\|_F^2 =\sum_{j=k+1}^{r}\sigma_j^2. \]
This also tells us that a different linear encoder and decoder cannot improve the reconstruction error at the same embedding dimension. A linear decoder reconstructs every example as a combination of the same \(k\) vectors, so the reconstructed matrix has rank at most \(k\). PCA attains the best error allowed by that constraint. This optimality is with respect to the data used to fit the directions.
The singular values let us choose \(k\) by how much reconstruction error we’re willing to accept. Because \(\mathbf X\) is centered, \(\|\mathbf X\|_F^2=\sum_{j=1}^r\sigma_j^2\) measures the total squared variation around the mean. Keeping \(k\) directions retains the fraction
\[ \frac{\sum_{j=1}^k\sigma_j^2}{\sum_{j=1}^r\sigma_j^2}. \]
This is called the fraction of variance explained. Converting the sums of squared deviations to variances divides both numerator and denominator by the same factor, leaving the ratio unchanged. If the singular values decrease quickly, a few coordinates retain most of the variation. If they’re all similar, we need many coordinates to get a small relative reconstruction error.
Our choice of feature units affects this calculation. Changing a feature from meters to centimeters multiplies its squared contribution to the loss by \(100^2\), giving PCA more reason to retain that feature’s variation. We can scale the features before fitting PCA to change their relative weight in the reconstruction objective. Centering alone doesn’t change those weights.
In the demo, we’ll vary \(k\) and compare the measured reconstruction error with the discarded singular-value energy.
Distances
The embedding \(\mathbf z_i\) has \(k\) coordinates, while both \(\mathbf x_i\) and \(\widehat{\mathbf x}_i\) have \(d\). To see how they relate, extend the principal directions to a full orthonormal eigenbasis \(\mathbf v_1,\ldots,\mathbf v_d\) of \(\mathbf X^\top\mathbf X\), ordered by decreasing eigenvalue. These are the right singular vectors, including directions with eigenvalue zero when \(r<d\). We can write each example in this basis:
\[ \mathbf x_i=\sum_{j=1}^{d}(\mathbf v_j^\top\mathbf x_i)\mathbf v_j. \]
The coefficient of \(\mathbf v_j\) is its inner product with \(\mathbf x_i\). The embedding stores the first \(k\) coefficients, and the reconstruction combines the corresponding basis vectors:
\[ \mathbf z_i=\mathbf V_k^\top\mathbf x_i,\qquad \widehat{\mathbf x}_i=\mathbf V_k\mathbf z_i=\sum_{j=1}^{k}(\mathbf v_j^\top\mathbf x_i)\mathbf v_j. \]
Here we’ve written examples as column vectors; transposing gives the row operations used for \(\mathbf X\). For training example \(i\) and \(j=1,\ldots,k\), the coefficient \(\mathbf v_j^\top\mathbf x_i\) is also \(\sigma_j U_{ij}\), where \(U_{ij}\) is entry \((i,j)\) of \(\mathbf U_r\). Thus \(\mathbf v_j\) specifies a direction in feature space, and column \(j\) of \(\mathbf U_k\boldsymbol\Sigma_k\) records the coordinates of all training examples along it.
The residual contains the remaining basis vectors:
\[ \mathbf r_i=\mathbf x_i-\widehat{\mathbf x}_i=\sum_{j=k+1}^{d}(\mathbf v_j^\top\mathbf x_i)\mathbf v_j. \]
When we compare two examples, their difference has retained and discarded components too. The squared distance between their embeddings accounts for the retained component; the residuals tell us exactly how much squared distance was lost.
Claim: For two examples encoded with the same orthonormal PCA directions,
\[ \|\mathbf x_i-\mathbf x_j\|_2^2=\|\mathbf z_i-\mathbf z_j\|_2^2+\|\mathbf r_i-\mathbf r_j\|_2^2. \]
In particular, PCA never increases a Euclidean distance.
Proof
Split the difference into its retained and discarded parts:
\[ \mathbf x_i-\mathbf x_j=(\widehat{\mathbf x}_i-\widehat{\mathbf x}_j)+(\mathbf r_i-\mathbf r_j). \]
The reconstructed difference is a linear combination of \(\mathbf v_1,\ldots,\mathbf v_k\). The residual difference is a linear combination of \(\mathbf v_{k+1},\ldots,\mathbf v_d\). Every vector in the first group is perpendicular to every vector in the second, so the two differences are perpendicular. The Pythagorean theorem gives
\[ \|\mathbf x_i-\mathbf x_j\|_2^2=\|\widehat{\mathbf x}_i-\widehat{\mathbf x}_j\|_2^2+\|\mathbf r_i-\mathbf r_j\|_2^2. \]
Finally, \(\widehat{\mathbf x}_i-\widehat{\mathbf x}_j=\mathbf V_k(\mathbf z_i-\mathbf z_j)\). The columns of \(\mathbf V_k\) are orthonormal, so expressing the retained coordinates in the original feature space preserves their length:
\[ \|\mathbf V_k(\mathbf z_i-\mathbf z_j)\|_2^2=(\mathbf z_i-\mathbf z_j)^\top\mathbf V_k^\top\mathbf V_k(\mathbf z_i-\mathbf z_j)=\|\mathbf z_i-\mathbf z_j\|_2^2. \]
Substituting proves the identity. Since \(\|\mathbf r_i-\mathbf r_j\|_2^2\geq0\), the embedding distance cannot exceed the original distance.
A pair keeps its distance exactly when its two residuals agree. If the examples differ only in discarded directions, their embeddings are identical. Different pairs can therefore shrink by different amounts, and the nearest neighbor can change even though PCA minimizes total reconstruction error. Likewise, a low-variance feature can determine a label while contributing little to that error. PCA chooses coordinates for reconstruction; the distinctions we need for another task may require a different objective. We’ll explore that choice in embeddings.