High-dimensional spaces suffer from the Curse of Dimensionality: points are sparse, distances become uniform, and models overfit. Dimensionality reduction compresses features, removes redundancies, and enables visualization. Principal Component Analysis (PCA) projects data onto a lower-dimensional subspace while maximizing variance preservation.
Matrix Factorization: SVD
PCA identifies the axis that accounts for the largest amount of variance in the training set. It finds principal components using Singular Value Decomposition (SVD), which decomposes the centered design matrix into three matrices:
The matrix contains the unit vectors that define the principal components:
To project the training set down to dimensions, we multiply the design matrix by the matrix containing the first principal components:
The Eigendecomposition Approach
Alternatively, PCA can be computed by performing an eigendecomposition on the Sample Covariance Matrix of the centered data. The covariance matrix captures the variance and correlation of features:
By solving the characteristic equation, we decompose into its eigenvalues () and eigenvectors ():
- The Eigenvectors represent the directions of the principal components (equivalent to the columns of from SVD). They are orthogonal to each other.
- The Eigenvalues represent the magnitude of variance captured along each corresponding principal component.
The Trace of the covariance matrix (the sum of its diagonal elements) is equal to the total variance in the dataset. Consequently, the sum of all eigenvalues is also equal to the total variance, allowing us to compute the proportion of variance captured by any single principal component as .
Explained Variance Ratio
The explained variance ratio indicates the proportion of the dataset’s variance that lies along each principal component. We use it to choose the number of dimensions that preserve a target percentage of variance (e.g., 95%).
Incremental PCA
For large datasets that do not fit in memory, we use Incremental PCA (IPCA). The algorithm splits the training set into mini-batches and feeds them one by one, enabling dimensionality reduction on out-of-core data.
Example: Computing PCA
The following example demonstrates projecting data using PCA and calculating the explained variance ratio:
Interactive Lab
Reduce a synthetic 3D dataset to 2D using Principal Component Analysis (PCA) and examine the explained variance ratios.
Exercise
Evaluate your understanding of PCA projection logic:
Why must the design matrix X be centered (zero mean) before applying SVD for PCA?
Choosing the Number of Components
Instead of selecting an arbitrary number of dimensions, we look at the cumulative explained variance plot. We choose the number of principal components that capture a target percentage (typically 95%) of the total variance. This ensures that we discard noise while preserving the essential geometric structure of the original data.