Idea. PCA rotates your data into new coordinates (principal components) so that the first axis captures as much spread (variance) as possible, the second the next most, and so on. Components are orthogonal.
Steps.
- Standardize features (optional but common): subtract mean and divide by standard deviation.
- Compute the covariance matrix
Σ = (1/(n−1)) XTXfor mean-centered dataX. - Find eigenvalues/eigenvectors of
Σ. Eigenvectors are principal directions; eigenvalues are the variance explained. - Sort by descending eigenvalue and project:
Z = X · Wwhere columns ofWare top-k eigenvectors.
Explained variance ratio. For eigenvalues λ, EVRi = λi / Σλ. Choose k such that cumulative EVR reaches your target (e.g., 95%).
Note: PCA is linear; it captures linear structure. For curved manifolds, consider Kernel PCA, t‑SNE, or UMAP.