Practical Labs/Lab: Singular Value Decomposition and Principal Component Analysis
Level 3 · linear-algebra
Lab: Singular Value Decomposition and Principal Component Analysis
Build a minimal, verifiable implementation that demonstrates Singular Value Decomposition and Principal Component Analysis. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.
Mathematical basis
For Singular Value Decomposition and Principal Component Analysis, use and the Eckart–Young bound .
Instructions
- 01Read section 7 of the Singular Value Decomposition and Principal Component Analysis learning page and write its governing equation on paper.
- 02Implement the core operation in NumPy on a synthetic rank-2 matrix and MNIST patches. Do not call a library function that solves the whole task.
- 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
- 04Run the experiment with the target rank k and record relative reconstruction error.
- 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.
Starter code
import numpy as np
def low_rank(x, k):
x = np.asarray(x, float)
u, s, vt = np.linalg.svd(x, full_matrices=False)
return (u[:, :k] * s[:k]) @ vt[:k]
x = np.array([[3.,1.],[1.,3.]])
y = low_rank(x, 1)
assert y.shape == x.shape and np.linalg.norm(x-y) > 0
print(np.round(y, 3))