TensorTau PathToAGI Classical Machine Learning Level 04

Level 04 · Classical Machine Learning · Chapter 052

PCA from SVD

What does Singular Value Decomposition have to do with the principal directions we just found from covariance?

Where We Are

Eigenvector PCA forms XcTXcX_c^TX_c. But Chapter 040 introduced a decomposition that acts directly on a matrix:

Xc=UΣVT.X_c=U\Sigma V^T.

The connection is exact.

1. Square the data matrix conceptually

Starting with

Xc=UΣVT,X_c=U\Sigma V^T,

consider

XcTXc.X_c^TX_c.

Substitute the SVD:

XcTXc=(VΣUT)(UΣVT).X_c^TX_c=(V\Sigma U^T)(U\Sigma V^T).

Because UTU=IU^TU=I,

XcTXc=VΣ2VT.X_c^TX_c=V\Sigma^2V^T.

But covariance is proportional to XcTXcX_c^TX_c:

C=1N−1VΣ2VT.C=\frac{1}{N-1}V\Sigma^2V^T.

Therefore the columns of VV are the covariance eigenvectors—the PCA directions.

The covariance eigenvalues are

λi=σi2N−1.\lambda_i=\frac{\sigma_i^2}{N-1}.

2. One idea, two viewpoints

Eigen route:
X → covariance XᵀX → eigenvectors

SVD route:
X → UΣVᵀ → right singular vectors V

Both produce the same principal subspace (up to sign and degeneracies).

3. Why SVD is often preferred

Explicitly forming XTXX^TX can worsen numerical conditioning and may be inefficient. Practical PCA implementations often use SVD or related solvers directly.

4. Compression and reconstruction

Keep the first kk right singular vectors VkV_k.

Z=XcVkZ=X_cV_k

and approximately reconstruct

X^=ZVkT+μ.\hat X=ZV_k^T+\mu.

This is not arbitrary compression. Among rank-kk linear approximations, truncated SVD gives the smallest Frobenius reconstruction error (Eckart–Young theorem).

5. Shape Check

If X is [N,D] and we keep k components:

V_k       [D,k]
Z=X V_k   [N,k]
X_hat     [N,D]

6. Small PyTorch connection

Xc = X - X.mean(0, keepdim=True)
U, S, Vh = torch.linalg.svd(Xc, full_matrices=False)
Vk = Vh[:k].T
Z = Xc @ Vk

Before running, trace every shape.

What to Remember

  • PCA covariance eigenvectors are the right singular vectors of centered data.
  • PCA eigenvalues equal squared singular values divided by N−1N-1.
  • SVD gives a numerically natural path to PCA.
  • Truncation gives optimal linear low-rank reconstruction.

Next → t-SNE and UMAP: PCA preserves linear directions. What if the interesting structure bends and curves?

Six ways to work the chapter, climbing from explaining it in your own words to investigating it like a researcher. Reading is not enough — this is where the chapter lands.

1 — Explain it

Explain why V—not U—contains feature-space principal directions when rows are examples.

2 — Calculate it

If singular values are [6,3,1], compute proportions of variance using squared singular values.

3 — Predict it

What is the shape of Z for X:[500,80] and k=12?

4 — Break it

Run PCA on uncentered data via raw SVD. Compare directions with centered PCA.

5 — Build it

Implement PCA using only mean subtraction and SVD; add reconstruction.

6 — Investigate it

Compare covariance-eigendecomposition PCA and SVD PCA numerically on a poorly conditioned dataset. Measure reconstruction and orthogonality errors.