ML MATH & LINEAR ALGEBRA ยท INTERVIEW QUESTION
Explain the role of SVD in PCA. How does truncated SVD relate to dimensionality reduction?
๐ก intermediate ยท Linear Algebra
Hint: Consider the relationship between the covariance matrix eigenvectors and the right singular vectors.
Answer
PCA finds principal components by computing eigenvectors of the sample covariance matrix (for mean-centered data). For mean-centered data, SVD decomposes directly.
For mean-centered data, truncated SVD recovers the same principal subspace as PCA. The practical difference is centering: scikit-learn's PCA centers the data, while TruncatedSVD does not. That makes TruncatedSVD suitable for sparse matrices, but it is not identical to PCA unless you center the input first.
- Columns of are the principal components
- gives eigenvalues of the covariance matrix
- gives the projected data
Truncated SVD keeps only the top- singular values/vectors, giving the best rank-k approximation (Eckart-Young theorem):
Worked example
Take the centered matrix
Its singular values are and . The first right singular vector is the -axis, so keeping projects every row onto that axis. The rank-1 reconstruction is
which keeps the dominant horizontal variation and discards no information because this centered example already has rank one.
import numpy as np
from sklearn.decomposition import TruncatedSVD
X = np.array([[-1., 0.], [0., 0.], [1., 0.]])
# TruncatedSVD does not center; center first for PCA equivalence.
X_centered = X - X.mean(axis=0)
svd = TruncatedSVD(n_components=1)
X_reduced = svd.fit_transform(X_centered)
print(f'Explained variance: {svd.explained_variance_ratio_.sum():.2%}')Related questions
- What is the difference between eigenvalues and singular values?
- What is a matrix and why are matrices important in machine learning?
- What is the rank of a matrix? Why does it matter in ML?
- Explain the relationship between eigendecomposition and diagonalization. Why is it useful?
- What is a dot product and why does it matter in ML?