Linear Algebra, Live
Strang §7.3 · Principal component analysis

PCA: finding the main direction of data

Slide 4 kept the biggest piece of a picture. Now we keep the biggest piece of a cloud of measured points. Picture the GPS dots your phone records as you walk down a long straight street. The dots wobble a little to the sides, but together they form a long, thin cloud along the street. The street's direction is the cloud's main direction. Principal component analysis (PCA) finds it from the numbers alone. Put the points in the rows of a matrix, move the cloud so its average sits at the origin, and take the SVD. The first input direction v₁ points along the cloud, and σ₁² says how much of the spread lies that way: the sum of the squared distances of the points from the average, measured along that direction.

Lesson 8 · slide 5PCA
center: C = data − mean    C = UΣVᵀ    first principal component = v₁    share = σ₁² / (σ₁² + σ₂² + …)
Key ideas
  1. Put the measurements as rows of a matrix and subtract the average of each column: the centered matrix C.
  2. The first principal component is v₁, the first input direction in the SVD of C. It is the direction of greatest spread.
  3. σᵢ² measures the spread along vᵢ; σ₁² / (σ₁² + σ₂² + …) is the share it explains.
  4. The line through the mean along v₁ minimises the sum of squared perpendicular distances to the points.

A cloud with a main direction

A cloud of points can be round, or long and thin. A long cloud has a direction where it spreads the most. That direction is what PCA is looking for.

Each dot is a big step c along 11 plus a small step d·−0.20.2 across it (c and d vary from dot to dot). The cloud is long along 11 and thin across: 11 is its main direction.

Step 1: center the data

Take five points: 11, 20, 32, 44 and 53. Their average is

x: (1 + 2 + 3 + 4 + 5)/5 = 3,   y: (1 + 0 + 2 + 4 + 3)/5 = 2,   mean = 32

Subtract 32 from every point, so the cloud sits around the origin. The centered points are the rows of a 5 by 2 matrix C.

Step 2: the SVD of C

C = −2−1−1−2001221,   CᵀC = 4 + 1 + 0 + 1 + 42 + 2 + 0 + 2 + 22 + 2 + 0 + 2 + 21 + 4 + 0 + 4 + 1 = 108810
trace 20, det 100 − 64 = 36:   λ² − 20λ + 36 = (λ − 18)(λ − 2),   λ = 18 and 2
σ₁ = √18 ≈ 4.24 with v₁ = 11/√2,   σ₂ = √2 ≈ 1.41 with v₂ = 1−1/√2
Share along v₁: 18/(18 + 2) = 90%
From the data to the singular values: center, form CᵀC, find its eigenvalues, take square roots. This is slide 2's recipe on data.

Why v₁ is the main direction

For a unit direction v, the vector Cv lists how far each point reaches along v. Then ‖Cv‖² adds up the squares: the spread along v. By slide 1, ‖Cv‖ is largest for v = v₁, with ‖Cv₁‖ = σ₁. Check with our points:

Cv₁ = −3−3033/√2,   ‖Cv₁‖² = (9 + 9 + 0 + 9 + 9)/2 = 18 = σ₁² ✓
Cv₂ = −110−11/√2,   ‖Cv₂‖² = 4/2 = 2 = σ₂² ✓

So v₁ is the first principal component. The line through the mean 32 in the direction 11 is y = x − 1.

Not the same as least squares

Lesson 5 fitted a line by making the vertical gaps small. That assumes only y has errors. PCA treats x and y the same and makes the perpendicular distances small. Take the centered point 12 and drop it straight onto the main line:

The centered point 12 drops onto the main line at p = 1.51.5. The gap e = −0.50.5 meets the line at a right angle.
(part along the line)² + (gap)² = 4.5 + 0.5 = 5 = 1² + 2²   (Pythagoras)

Each point's squared distance from the mean is fixed. So the more spread a line keeps along itself, the smaller the gaps to it. For our five points the gaps squared add up to σ₂² = 2: the total spread is 20 (the trace of CᵀC), the line keeps 18, and 20 − 18 = 2 is left over. That is the smallest possible. Lesson 5's line through the same points is different:

Least squares (lesson 5) makes the vertical gaps small and finds the line y = −0.4 + 0.8x. PCA's line y = x − 1 is steeper, because it measures gaps at right angles to the line.

Keep reading with the full course

Finish lesson 8 and you will be able to: compress an image and keep only what matters.

Still locked here:

  • The rest of this explanation
  • 4 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 to 3 stay free.

Examples

Example 1 · Points exactly on a line

00, 12, 24, centered: −1−2, 00, 12.

Show solutionHide solution
C = −1−20012: rank 1
v₁ = 12/√5, σ₂ = 0: 100% of the spread along the line
Exercise Turn the line until it keeps the largest share of the spread (the first principal component). Edit the code and press Run.
slide_5.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course