PCA via eigendecomposition and SVD
Why PCA Matters
The Problem: Real data lives in high dimensions (thousands of features), but most variation happens in a lower-dimensional subspace. We want to:
- Compress data (fewer dimensions → less storage, faster computation)
- Visualize high-D data in 2D/3D
- Denoise by keeping signal, dropping noise dimensions
- Decorelate features for downstream algorithms
The Solution: Find orthogonal directions (principal components) ordered by how much variance they capture.
Method 1: Eigendecomposition of Covariance Matrix
Step 1: Center the Data
Start with data matrix (n samples, d features).
where is the mean vector.
WHY? PCA finds directions of variance. Variance is measured from the mean, so we must center first. Without centering, the first PC would just point toward the mean from the origin—useless.
Step 2: Compute Covariance Matrix
WHY divide by ? Bessel's correction for unbiased variance estimation (1 degree of freedom lost to the mean).
Step 3: Eigendecomposition
Solve the eigenvalue problem:
This gives eigenvectors and eigenvalues .
WHY eigenvalues = variance? Consider projecting centered data onto unit vector :
We want to maximize this subject to . Using Lagrange multipliers:
Taking derivative w.r.t. and setting to zero:
So must be an eigenvector! Substituting back: . The eigenvalue is the variance along that direction.
Step 4: Project Data
To reduce to dimensions, project onto the top eigenvectors:
where .
Result: is the low-dimensional representation.
Method 2: Singular Value Decomposition (SVD)
Any matrix can be decomposed as:
where:
- is orthogonal (left singular vectors)
- is diagonal with singular values
- is orthogonal (right singular vectors)
Connection to Eigendecomposition
Start with the covariance matrix:
Substitute :
Since :
This is the eigendecomposition of ! The eigenvectors are the columns of , and:
The singular values squared (divided by ) give the eigenvalues.
WHY use SVD?
- Numerical stability: Covariance matrix can be ill-conditioned; SVD avoids forming it
- Efficiency: For (many features, few samples), SVD is faster
- Direct interpretation: gives sample projections, gives feature loadings
SVD-Based PCA Algorithm
1. Center the data: X_centered = X - mean(X, axis=0)
2. Compute SVD: U, Σ, V^T = SVD(X_centered)
3. Principal components: V (columns are PCs)
4. Eigenvalues: λ_i = σ_i² / (n-1)
5. Project to k dimensions: Z = X_centered @ V[:, :k]
Alternatively, use : the first columns of give the same projection.
Step 1: Center Mean:
Step 2: Covariance
Why this step? We're computing how much each feature varies and co-varies.
Step 3: Eigendecomposition Solve :
Using quadratic formula:
Why this step? We're finding which directions capture the most variance. captures 99.8% of variance!
Eigenvector for : (points along the elongated direction).
Step 4: Project to 1D
Interpretation: These 5 values summarize the original 2D data with minimal information loss (99.8% variance retained).
Singular values: ,
Check eigenvalue connection:
Right singular vectors (columns of ) are the principal components: — same as eigendecomposition!
Why this works? SVD factors the data matrix itself; the right singular vectors automatically point in the maximum-variance directions.
Variance Explained
Practical use: Choose such that (retain 95% of information).
Total variance:
| Cumulative variance | EVR | |
|---|---|---|
| 1 | 10.5 | 70% |
| 2 | 13.7 | 91% |
| 3 | 14.5 | 97% |
Decision: Use to retain 97% of variance (compress from 5D to 3D).
Common Mistakes
Why it feels right: "PCA is just eigendecomposition of the covariance matrix—why preprocess?"
The problem: Without centering, the first PC points from the origin to the data cloud's center (capturing location, not spread). You want variance around the mean, not from the origin.
The fix: Always center: X_centered = X - X.mean(axis=0). Standardizing (dividing by std) is optional but recommended if features have different units.
Why it feels right: "Bigger singular value = more important dimension."
The problem: Singular values scale with data magnitude and sample size. Eigenvalues have a specific interpretation as variance.
The fix: Convert correctly: . Or use explained variance ratio: .
Why it feels right: "PCA is dimensionality reduction—should work on any data."
The problem: PCA finds the best linear subspace. For non-linear structure (Swiss roll, circular clusters), linear projections destroy the geometry.
The fix: Use kernel PCA (implicitly maps to higher-D where structure is linear) or manifold learning methods (t-SNE, UMAP, Isomap).
Computational Complexity
| Method | Time Complexity | Space | Best When | |--------|-------------|--------| | Eigendecomposition of | | | (few features) | | SVD of | | | (few samples) | | Randomized SVD | | | , very large |
Practical tip: For , use randomized/truncated SVD (e.g., sklearn.decomposition.PCA with svd_solver='randomized').
Recall Explain to a12-Year-Old
Imagine you're taking photos of a pancake-shaped cloud of fireflies. From one angle (say, looking down), you see the whole cloud spread out—lots of variation. From another angle (edge-on), it looks thin—little variation. PCA is like finding the best camera angles automatically.
Here's how: You have a big spreadsheet of firefly positions (maybe 1000 dimensions—each firefly's brightness, position, speed etc.). PCA finds the "most interesting" directions—the ones where the data spreads out the most. Then you throw away the boring directions (where everything looks the same) and keep only 2-3 important ones. Now you can draw the cloud on paper!
The eigendecomposition way asks: "Which direction has the biggest variance?" It looks at how data points differ from the average and finds that direction mathematically (eigenvector of the covariance matrix).
The SVD way asks: "How can I break down the data itself into simple pieces?" It factors the data like —but for giant matrices. Turns out, the pieces tell you the same important directions!
Both methods give you the same answer: here are your 2-3 magic camera angles. Compress your data, visualize it, and you're done!
SVP for SVD PCA:
- SVD decomposition of centered X
- V matrix = principal components
- Variance = sigma² / (n-1)
- Project: Z = XV_k
Connections
- 2.5.07-K-Means-Clustering - PCA often used as preprocessing to speed up clustering
- 2.5.01-Introduction-to-Unsupervised-Learning - PCA is a foundational unsupervised technique
- 2.4.03-Feature-Scalingand-Normalization - Standardization before PCA equalizes feature importance
- 3.2.05-Autoencoders - Non-linear generalization of PCA using neural networks
- Linear-Algebra-Eigenvalues-and-Eigenvectors - Mathematical foundation of eigendecomposition
- Linear-Algebra-SVD - SVD theory and applications beyond PCA
- 2.6.04-Kernel-PCA - PCA extended to non-linear data
#flashcards/ai-ml
What does PCA stand for and what does it do? :: Principal Component Analysis finds orthogonal directions in data ordered by variance, enabling dimensionality reduction while preserving maximum information.
Why must data be centered before PCA?
What is the covariance matrix in PCA?
Why are eigenvectors of the covariance matrix the principal components?
What does an eigenvalue represent in PCA?
How do you compute explained variance ratio?
What is the SVD decomposition?
How do singular values relate to eigenvalues in PCA?
In SVD-based PCA, which matrix contains the principal components?
When should you use SVD instead of eigendecomposition for PCA?
How do you project data onto the first principal components?
What happens if you don't center data before PCA?
Why doesn't PCA work well on non-linear data?
What is the time complexity of eigendecomposition-based PCA?
What is the time complexity of SVD-based PCA?
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
PCA ka matlab kya hai? Socho tumhare pas bahut sare features hain (matlab bahut dimensions mein data hai), lekin sabhi features equally important nahi hote. Kuch directions mein data bahut spread out hai, kuch mein bilkul tight. PCA ka kaam hai woh "most important" directions dhoondhna jahan pe maximum variation hai. Yeh do tareekon se ho sakta hai: eigendecomposition (covariance matrix ke eigenvectors nikalke) ya SVD (data matrix ko directly factor karke). Dono ka result same hota hai—PCA bata hai ki konse directions sabse zyada information capture karte hain.
Kyon zaroori hai? Real-world mein data kafi high-dimensional hota hai (jaise image = 1000s of pixels, text = 1000s of words). Lekin sach yeh hai ki zyada-tar variance sirf kuch hi directions mein hota hai. PCA un important directions ko pakad leta hai aur baki "noise" wale dimensions ko drop kar deta hai. Result: kam storage, faster computation, aur visualization bhi asaan (2D/3D plot bana sakte ho). Bonus: algorithms bhi fast chalte hain kyunki dimensions kam ho gaye!
Eigendecomposition method: Pehle data ko center karo (mean subtract karo). Phir covariance matrix banao—yeh bata hai features kaise co-vary karte hain. Iska eigendecomposition karo: eigenvectors woh directions hain jahan maximum variance hai, aur eigenvalues bate hain kitna variance hai. Top-k eigenvectors le lo = tumhare principal