Where We Are
Chapter 006 taught us that coordinates depend on basis.
Chapter 006 taught us that some directions may be redundant.
Chapter 015 taught us that singular values reveal which directions a matrix stretches strongly or weakly.
Now imagine a dataset instead of a transformation.
Perhaps each example has 100 features.
Do all 100 directions carry equal information?
Usually not.
Some directions may explain most of the variation, while others contain little more than noise.
Principal Component Analysis asks:
Can we rotate the coordinate system so the first few axes capture as much variation as possible?
The Picture to Hold in Your Head
PCA asks the same question SVD asked, but now the object is a cloud of data rather than an arbitrary transformation: which directions contain most of the structure?
Imagine rotating the coordinate axes until one axis lies along the long direction of the cloud. If almost all variation lies there, then every two-dimensional point can be represented fairly well by just one coordinate along that axis. Compression is therefore a change of basis followed by deliberately discarding weak directions.
Centering matters because PCA studies variation around the mean. Scaling matters because variance carries units: a feature measured in thousands can dominate one measured in single digits even if it is not conceptually more important.
🎛️ First use Step zero: centre it. Then hunt for the maximum in Variance along a line. Finally turn the direction away in Project and rebuild and watch exactly what information the discarded component was carrying.
1. The Problem: A Diagonal Cloud
Suppose our data points are
(1,1.1),(2,1.9),(3,3.2),(4,3.9),(5,5.1).
Plot them mentally:
y
↑
5| •
4| •
3| •
2| •
1| •
0+----------------→ x
The data uses two coordinates, but most variation lies along one diagonal direction.
If we choose an axis along that diagonal, one coordinate may describe most of the structure.
That is dimensionality reduction.
2. First Attempt: Keep the Feature With Larger Variance
Maybe we simply compare x-variance and y-variance and keep the larger one.
But that fails when important variation is diagonal.
Both original axes can miss the best direction.
A Tempting Wrong Idea
PCA does not merely select original features. It discovers new directions that are linear combinations of features.
So we need to search over directions, not columns.
3. Step Zero: Center the Data
Before asking which direction varies most, move the cloud so its mean sits at the origin.
If data matrix is
X=−−−x1T−−−−−−x2T−−−⋮,
compute feature mean
μ=n1i∑xi.
Then centered data is
Xc=X−μ.
Why center?
Because PCA is about variation around the center, not distance from an arbitrary origin.
Without centering, the mean offset can dominate the result.
4. Variance Along a Direction
Choose a unit vector
v.
Project each centered point onto that direction:
zi=xi⋅v.
Now we have one number per point.
The variance of those projected numbers tells us how spread out the data is along v.
So PCA’s first question becomes:
Which unit direction v maximizes projected variance?
That direction is the first principal component.
5. Build the Covariance Matrix
For centered data Xc with examples as rows, covariance matrix is
C=n−11XcTXc.
For two features,
C=[Var(x)Cov(x,y)Cov(x,y)Var(y)].
Diagonal terms measure feature variance.
Off-diagonal terms measure whether features move together.
If covariance is strongly positive, the cloud tends to tilt diagonally upward.
If negative, it tends to tilt downward.
6. Why Eigenvectors Appear
Projected coordinate is
z=Xcv.
Projected variance is proportional to
∥Xcv∥2.
Expand:
∥Xcv∥2=vTXcTXcv.
Ignoring the constant factor 1/(n−1),
variance along v=vTCv.
We want to maximize this subject to
∥v∥=1.
The maximizing direction is the eigenvector of C with largest eigenvalue.
So eigenvectors appear because PCA is an optimization problem over directions.
7. The First Principal Component
Let
Cv1=λ1v1
with largest eigenvalue
λ1.
Then
v1
is the direction of maximum variance.
And
λ1
is the amount of variance captured along that direction.
This gives a beautiful interpretation of eigenvalues:
in PCA, eigenvalue = variance captured by that principal direction.
8. The Second Principal Component
After finding the first direction, we want the next direction that captures as much remaining variance as possible while staying perpendicular to the first.
That is the eigenvector associated with the second-largest eigenvalue.
Continue similarly.
Because covariance matrix is symmetric, its eigenvectors can be chosen orthonormal.
So PCA naturally constructs an orthonormal basis ordered by importance.
9. A New Coordinate System
Stack principal directions as columns:
V=∣v1∣∣v2∣⋯∣vd∣.
Then new coordinates are
Z=XcV.
Each row of Z describes one example in the principal-component basis.
This is Chapter 006’s change of basis, now chosen from data.
10. Dimensionality Reduction
If eigenvalues are
[9.0, 1.0, 0.05, 0.01],
most variation lives in the first two directions.
So keep only
Vk=[v1 v2].
Then
Zk=XcVk.
Original data had 4 features.
Compressed representation has 2 principal coordinates.
We discarded directions with little variance.
11. Explained Variance Ratio
Total variance is sum of eigenvalues:
i∑λi.
The proportion captured by component i is
explained variance ratioi=∑jλjλi
For eigenvalues
[9,1],
the first component captures
109=90%.
The second captures 10%.
That gives a principled way to choose how many dimensions to keep.
12. Reconstruction
Compression should not only reduce coordinates; we should understand what information is lost.
If
Zk=XcVk,
then reconstruct approximately:
X^c=ZkVkT.
Add the mean back:
X^=ZkVkT+μ
If k=d, reconstruction is exact (up to numerical precision).
If k<d, reconstruction is approximate.
Compression error comes from discarded directions.
13. PCA Through SVD
Instead of explicitly building covariance matrix, perform SVD of centered data:
Xc=UΣVT.
Then columns of V are principal directions.
Why?
Because
XcTXc=VΣTΣVT.
So V diagonalizes the covariance structure.
Eigenvalues of covariance are related to squared singular values:
λi=n−1σi2.
Thus PCA and SVD are tightly connected.
14. Why SVD Is Often Preferred Numerically
Computing covariance first squares the condition number and can magnify numerical issues.
Direct SVD of centered data is often more numerically stable.
So there are two conceptual routes:
Covariance route
center X
↓
C = X^T X/(n-1)
↓
eigenvectors of C
SVD route
center X
↓
X = UΣV^T
↓
principal directions = columns of V
Same geometry, different computation.
15. Tiny Numeric Intuition
Suppose centered points lie exactly on
y=x.
Then every point is a multiple of
[11].
Normalized direction is
21[11].
Variance perpendicular to that direction is zero.
So PCA finds:
- first component: diagonal direction,
- second component: perpendicular direction with zero variance.
The dataset is stored in 2D but intrinsically 1D.
16. A Tempting Wrong Idea: “Low Variance Means Unimportant”
PCA preserves directions of high variance.
But high variance is not the same as high predictive usefulness.
Imagine a classification problem where the label depends on a subtle low-variance feature.
PCA might discard it.
A Tempting Wrong Idea
PCA is unsupervised. It does not know your target labels. It preserves variance, not task relevance.
That distinction matters enormously.
17. Scaling Before PCA
Suppose features are:
age: 18–80
income: 20,000–5,000,000
Raw variance of income may dominate simply because units are larger.
PCA on unscaled features may mostly discover “income direction.”
If features have incomparable units, standardization is often appropriate:
z=σx−μ.
But scaling changes the question.
PCA on covariance and PCA on standardized/correlation data are not identical analyses.
18. Geometry of Projection Error
When projecting a point onto a principal subspace, the reconstruction error is the perpendicular distance from the point to that subspace.
PCA chooses the rank-k linear subspace minimizing total squared reconstruction error.
This is deeply connected to truncated SVD and the Eckart–Young theorem.
So PCA can be understood in two equivalent ways:
- maximize captured variance,
- minimize squared reconstruction error.
19. Shape Check
Suppose
X∈R1000×50.
That means:
- 1000 examples,
- 50 features.
After centering:
Xc:(1000×50).
Covariance:
C=XcTXc/(999)
has shape
(50×50).
If we keep k=5 principal directions:
Vk:(50×5).
Compressed data:
Z=XcVk:(1000×5).
Reconstruction:
ZVkT:(1000×50).
Shapes tell the compression story.
20. Code From First Principles
import numpy as np
X = np.array([
[1.0, 1.1],
[2.0, 1.9],
[3.0, 3.2],
[4.0, 3.9],
[5.0, 5.1],
])
mean = X.mean(axis=0)
Xc = X - mean
C = Xc.T @ Xc / (len(X) - 1)
eigvals, eigvecs = np.linalg.eigh(C)
order = np.argsort(eigvals)[::-1]
eigvals = eigvals[order]
eigvecs = eigvecs[:, order]
pc1 = eigvecs[:, 0]
z = Xc @ pc1
The library call is small because the mathematics has already done the conceptual work.
21. Verify PCA With SVD
U, s, Vt = np.linalg.svd(Xc, full_matrices=False)
pc1_svd = Vt[0]
# Direction can flip sign and still represent the same axis.
assert np.isclose(abs(np.dot(pc1, pc1_svd)), 1.0)
Why absolute value?
Because if v is a principal direction, then −v describes the same axis.
Eigenvector sign is arbitrary.
22. Break It
Forget to center
Run PCA on data with a large offset. Compare directions before and after centering.
Mix incompatible units
Use one feature measured in millions and another in decimals. Observe domination.
Keep too few components
Compress curved or multi-directional data to 1D and inspect reconstruction loss.
Use PCA for nonlinear structure
Points arranged on a curved manifold may require many linear components even though intrinsic structure is simple.
PCA is linear.
23. Machine-Learning Connections
PCA appears in:
- visualization,
- noise reduction,
- preprocessing,
- exploratory data analysis,
- compression,
- decorrelation,
- speeding up downstream models,
- understanding representation geometry.
It also trains an important habit:
do not confuse the original coordinate system with the underlying structure of the data.
24. History Lens — Pearson and Hotelling
Ideas underlying PCA trace to Karl Pearson’s early twentieth-century work on fitting lower-dimensional linear structures to data, with later formal development by Harold Hotelling.
The historical problem was recognizable even before modern computers:
many measured variables may reflect a smaller number of dominant patterns.
PCA turns that intuition into linear algebra.
25. Distinctions That Matter
| Pair | Difference |
|---|
| feature selection vs PCA | keep original columns vs create new linear-combination directions |
| variance vs predictive importance | unsupervised spread vs usefulness for target |
| covariance PCA vs standardized PCA | raw-unit variation vs scale-normalized variation |
| projection vs reconstruction | compress into components vs map back to feature space |
| PCA vs nonlinear manifold learning | linear subspace vs potentially curved structure |
26. What We Discovered
- PCA finds orthogonal directions of maximum data variance.
- Data must usually be centered first.
- Covariance matrix summarizes joint variation.
- Principal directions are covariance eigenvectors.
- Eigenvalues measure variance captured by components.
- Keeping top components reduces dimension.
- Reconstruction maps compressed points back approximately.
- SVD of centered data yields principal directions directly.
- PCA maximizes variance and equivalently minimizes squared reconstruction error.
- PCA preserves variance, not necessarily task-relevant information.
- Feature scaling can dramatically change PCA.
27. One-Minute Explanation
PCA looks at a cloud of centered data and asks which direction has the largest spread. That direction becomes the first principal component. The next component captures the largest remaining spread while staying perpendicular, and so on. These directions are eigenvectors of the covariance matrix, or equivalently right singular vectors of the centered data matrix. By keeping only the first few components, we compress data while preserving as much variance as possible. PCA is therefore a learned change of basis plus truncation.
28. Mastery Check
- Why must PCA generally center data?
- Why is PCA not simply feature selection?
- What does covariance measure?
- Why do covariance eigenvectors become principal directions?
- What does each PCA eigenvalue mean?
- How do you compute explained variance ratio?
- How do you reconstruct from k components?
- Why is PCA closely connected to SVD?
- Why can standardization change PCA dramatically?
- Give a case where low variance could still be predictive.
- Why is eigenvector sign arbitrary?
- What kind of structure can linear PCA fail to capture?
🔭 Bridge to Level 2
We now have a language for vectors, matrices, transformations, bases, rank, eigenvectors, SVD and PCA.
But all of these tools describe structure.
Learning requires something else:
How does a quantity change when we change its input by a tiny amount?
That question moves us from the mathematics of space to the mathematics of change.
Next: functions and slopes.