Chapter 6: PCA From Napkin Math to Production Code — Hand Calculations, Variance Ratios, and SVD
Having unified the top-down and bottom-up foundations of Principal Component Analysis, we now turn theory into complete computational mastery. How do you execute PCA from start to finish on a raw table of numbers by hand? How do you decide whether to keep or principal components? And why do industrial libraries like scikit-learn, PyTorch, and LAPACK compute PCA using Singular Value Decomposition (SVD) directly on instead of explicitly forming ?
In this final chapter, we walk through the complete end-to-end PCA pipeline—from centering and standardization to trace conservation, explained variance ratios, low-rank reconstruction, and numerical linear algebra via SVD.
In this Article
- 1. The Complete 5-Step PCA Algorithm from Raw Data to Reconstruction
- 2. End-to-End Numerical PCA by Hand on a 4-Point 2D Dataset
- 3. Trace Conservation and the Explained Variance Ratio
- 4. Covariance PCA vs. Standardized (Correlation) PCA
- 5. Why Production PCA Uses SVD (X = UΣVᵀ) Instead of Forming XᵀX
- 6. When PCA Fails: Non-Linear Manifolds and Variance ≠ Importance
- 7. Active Recall Check and Key Takeaways
1. The Complete 5-Step PCA Algorithm from Raw Data to Reconstruction
[ Raw Data X_raw (n x d) ]
|
v Step 1: Subtract column mean mu (and optionally divide by std dev s_j)
[ Centered Data X (n x d) ]
|
v Step 2: Compute SVD X = U Sigma V^T (or Covariance C = (1/n) X^T X)
[ Orthonormal Principal Axes V_k = [u_1, ..., u_k] (d x k) ]
|
v Step 3: Project to low-D scores Z_k = X V_k (n x k)
[ Compressed Representation Z_k ]
|
v Step 4: Reconstruct in original space X_hat = Z_k V_k^T + mu^T
[ Denoised / Compressed Approximation X_hat (n x d) ]
2. End-to-End Numerical PCA by Hand on a 4-Point 2D Dataset
Suppose we measure observations on features:
- Step 1 — Center the Columns: The column means are and . Subtracting from every row gives:
- Step 2 — Form the Sample Covariance Matrix :
- Step 3 — Solve the Characteristic Polynomial :
- Trace: .
- Determinant: .
- Quadratic: .
- Eigenvalues: and .
- Step 4 — Find Principal Component 1 () and Explained Variance Ratio:
- From , we get .
- Explained Variance Ratio of PC1: ! Over of the 2D scatter cloud's variance is captured by a single 1D coordinate .
3. Trace Conservation and the Explained Variance Ratio
Because rotating axes preserves the trace of a matrix, , the sum of the original feature variances always equals the sum of all eigenvalues:
4. Covariance PCA vs. Standardized (Correlation) PCA
Because PCA maximizes variance, it is sensitive to measurement units. If Feature 1 is salary in dollars () and Feature 2 is years of experience (), Feature 1 has times larger variance and will completely dominate PC1! Whenever features have incomparable units or scales, standardize each column to zero mean and unit variance () before PCA—which is equivalent to computing the eigenvectors of the correlation matrix .
5. Why Production PCA Uses SVD (X = UΣVᵀ) Instead of Forming XᵀX
Every centered data matrix has a Singular Value Decomposition , where and have orthonormal columns and contains the non-negative singular values. Plugging into gives:
| Quantity | Covariance Eigendecomposition () | Direct SVD on Centered Data () |
|---|---|---|
| Principal Directions | Eigenvectors of | Right singular vectors (columns of ) |
| Eigenvalues (Variances) | Eigenvalues of | Squared singular values divided by : |
| Principal Scores | ||
| Condition Number | (Squares numerical error!) | (Preserves floating-point precision!) |
6. When PCA Fails: Non-Linear Manifolds and Variance ≠ Importance
- Non-Linear Structure: PCA only projects onto flat hyperplanes. If points lie on a spiral ("Swiss roll") or concentric spheres, linear projection scrambles them; use Kernel PCA, UMAP, or autoencoders instead.
- Unsupervised Blindness to Class Labels: PCA is completely unsupervised. If two classes are separated along a low-variance direction while high-variance background noise runs perpendicular to the class boundary, dropping low-variance components destroys the classification signal (where Linear Discriminant Analysis, LDA, excels).
7. Active Recall Check and Key Takeaways
Quick Check: Test Your Understanding
Question: A centered data matrix with rows has singular values , , and . What are the eigenvalues of and what is the explained variance ratio of PC1?
Answer: Using , we get , , and . The total variance is , so PC1 explains of the variance.
- Chapter Microlearnings: Complete your mastery with 6.1 Step-by-Step Numerical PCA by Hand, 6.2 Explained Variance Ratio & Trace Conservation, and 6.3 Why Production PCA Uses SVD.
- Canonical References: See Scikit-Learn Official PCA Documentation and Cross Validated on the Exact Relationship Between SVD and PCA.
Take a Quiz Based on This Article
Test your understanding with AI-generated questions tailored to this content
Adaptive Learning with SynLearn
Explore this article through guided practice that adapts to your answers