AI from scratch · Linear algebra 2 · Stage 1: the math of learning

Eigenvectors and PCA

A matrix moves the whole plane (chapter 1), and most arrows get knocked off their line. But a few special directions only get stretched. Those are the eigenvectors, and they're the matrix's skeleton: they say where repeating a move ends up, how Google first ranked the web, which way a loss valley is steepest, and which direction in your data matters most. This lesson finds them by hand, by algebra and by repetition, then uses them to rank this site's own pages and to squeeze real data down to its most important axis.

1. A basis is a choice of steps

Chapter 1 described every point with two steps, \(\hat\imath = (1, 0)\) and \(\hat\jmath = (0, 1)\). Nothing forces that choice. Any two arrows that don't lie on one line can reach every point of the plane by mixing them (their span is the whole plane), so they form a basis, and a point's coordinates are just how much of each step you take. Linear algebra 1's column picture was this exact question.

In the basis \(u = (1, 1)\), \(w = (1, -1)\), the point \((5, 1)\) is \(a\,u + b\,w\). What is \(a\)?

\(a(1, 1) + b(1, -1) = (a + b, a - b) = (5, 1)\), so \(a = 3\), \(b = 2\). Same point, different coordinates. The trick of this lesson is choosing the basis that makes a matrix simplest.

2. The arrows a matrix only stretches

Drag the blue arrow \(v\). The yellow arrow is where the matrix sends it, \(Av\). Usually it points somewhere else. Find the directions where yellow lies exactly along blue: there \(Av = \lambda v\) for some number \(\lambda\), the eigenvalue, and \(v\) is an eigenvector. The dashed lines show where they are once you've looked.

Eigenvectors are the directions a matrix only stretches, never turns; the eigenvalue is how much it stretches them.

For \(\begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}\), drag \(v\) to \((1, 1)\). It's an eigenvector: what's its eigenvalue?

\(Av = (2 + 1, 1 + 2) = (3, 3) = 3v\), so \(\lambda = 3\). Now try \((1, -1)\): \(Av = (1, -1)\), \(\lambda = 1\), stretched by nothing at all.

Pick "rotate". Can you find an eigenvector?

A rotation has no real eigenvectors. (Its eigenvalues are complex numbers, which is how complex numbers secretly describe turning; the radio course runs on that.) The shear has just one eigen-direction, the horizontal line it slides along. A symmetric matrix like the first one always has a full set, at right angles to each other.

3. Finding them with algebra

\(Av = \lambda v\) means \((A - \lambda I)v = 0\) for a nonzero \(v\): the matrix \(A - \lambda I\) squashes some arrow to zero, so it must be singular, which means its determinant is zero (Linear algebra 1). That gives an equation for \(\lambda\) alone:

The math, step by step

For \(A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}\), the characteristic polynomial (a polynomial is a sum of powers of one unknown, here \(\lambda\)) is

\[ \det(A - \lambda I) = \det \begin{pmatrix} 2 - \lambda & 1 \\ 1 & 2 - \lambda \end{pmatrix} = (2 - \lambda)^2 - 1 = 0 \]

so \(2 - \lambda = \pm 1\): \(\lambda = 3\) or \(\lambda = 1\). For each, solve \((A - \lambda I)v = 0\): with \(\lambda = 3\), \(\begin{pmatrix} -1 & 1 \\ 1 & -1 \end{pmatrix} v = 0\) says \(v_1 = v_2\), the diagonal \((1, 1)\). Any multiple works too: eigenvectors are really eigen-directions.

For any 2×2 matrix the polynomial is \(\lambda^2 - (\text{trace})\lambda + \det = 0\), where the trace is the sum of the diagonal. So the eigenvalues always add up to the trace and multiply to the determinant: the area scale from chapter 1 is the product of the stretches along the eigen-directions.

What's the largest eigenvalue of \(\begin{pmatrix} 3 & 1 \\ 0 & 2 \end{pmatrix}\)? (Hint: for a triangular matrix, look at the diagonal.)

\(\det(A - \lambda I) = (3 - \lambda)(2 - \lambda) - 1 \cdot 0\), so \(\lambda = 3\) or \(2\). The zero below the diagonal kills the cross term. Check: they add to the trace 5 and multiply to the determinant 6.

\(\begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}\) has trace 2 and determinant −3. One eigenvalue is 3. What's the other?

They add to 2, so the other is \(-1\) (and \(3 \times -1 = -3\), the determinant, checks). A negative eigenvalue flips: pick that matrix and drag \(v\) to \((1, -1)\); the yellow arrow points straight back the other way.

4. Repeat a move, and the biggest eigenvalue wins

Press Apply A over and over above. Each press of the move stretches the part of \(v\) along each eigenvector by its own \(\lambda\). After \(k\) moves that's \(\lambda^k\): the part along the biggest eigenvalue grows fastest and takes over, so the arrow swings onto the dominant eigenvector. That's power iteration, the simplest way to find an eigenvector: multiply, rescale, repeat.

\[ v = c_1 e_1 + c_2 e_2 \quad\Rightarrow\quad A^k v = c_1 \lambda_1^k e_1 + c_2 \lambda_2^k e_2 \]

How fast it lines up depends on the ratio \(|\lambda_2 / \lambda_1|\): the second part shrinks by that factor every step. Writing a matrix in its own eigenbasis, \(A = V \Lambda V^{-1}\) with the eigenvalues on the diagonal of \(\Lambda\), turns "apply it \(k\) times" into "raise each eigenvalue to the \(k\)": \(A^k = V \Lambda^k V^{-1}\).

Repeat a move and the direction with the biggest stretch takes over; how fast depends on how far ahead the winner is.

Where it breaks: when there's no clear winner

5. A random walk over this site: PageRank

Imagine a reader who clicks random links on this site forever. Which pages do they spend the most time on? Put everyone on one page, then each step, each page shares its readers equally among the pages it links to. That's a matrix times a vector, repeated: a Markov chain. This is the real link graph of this site's 169 pages (as of 12 October 2026). Everyone starts on the last ham radio lesson.

The shares settle into a fixed list no matter where the readers started. That fixed point is a vector \(r\) with \(Mr = r\): an eigenvector with eigenvalue 1, and that's PageRank, the idea Google was founded on. One fix makes it work: with probability 0.15 each step, a reader gets bored and jumps to a random page (the damping). Without it, readers could get stuck in a corner of pages that only link to each other, and the answer would depend on where they started.

Run it from the start and watch the readout's "change since last step". It shrinks by about the same factor each step. Why so steady?

Section 4's ratio rule in action. The jump-to-a-random-page part guarantees every other eigenvalue is at most 0.85 in size, so each step removes at least 15% of the remaining error: about 50 steps gets 3 decimals.

6. Projection: the shadow on a line

To project a point onto a line through the origin with direction \(u\) (length 1), drop a perpendicular: the shadow is \((x \cdot u)\,u\), and the number \(x \cdot u\) is the point's coordinate along that line. The leftover, \(x - (x \cdot u)u\), is at right angles to \(u\), so by Pythagoras, length² of \(x\) = shadow² + leftover².

Project \(x = (3, 4)\) onto the direction \(u = (0.6, 0.8)\). What's its coordinate \(x \cdot u\)?

\(3 \times 0.6 + 4 \times 0.8 = 1.8 + 3.2 = 5\). That's the full length of \(x\): it already lies along \(u\), so nothing is left over.

7. PCA: the direction your data cares about most

Back to Probability 2's outages: each dot is one outage, across the MTA's estimate, up the actual time, both as \(\log_{10}\) hours with the mean subtracted. Turn the line. Each dot casts a shadow on it, and the red sticks are what the line misses. Find the angle where the shadows spread out the most.

Two goals turn out to be one. Spreading the shadows as wide as possible and making the red sticks as short as possible are the same thing, because shadow² + stick² is fixed for every dot (Pythagoras again). And the best direction is no search at all: the variance along a unit direction \(u\) is \(u^\top \Sigma u\), with \(\Sigma\) the covariance matrix from Probability 2, and that's largest exactly along \(\Sigma\)'s top eigenvector, with the eigenvalue as the variance captured. That's principal component analysis:

  1. Subtract the mean of every feature (often also divide by its standard deviation).
  2. Compute the covariance matrix \(\Sigma\).
  3. Its eigenvectors, sorted by eigenvalue, are the principal components; each eigenvalue is how much variance that direction holds.
  4. Keep the top few and describe every example by its coordinates along them.

PCA turns the data to its own best axes: the first is the direction of greatest spread, and each eigenvalue says how much of the spread its direction holds.

Why is the top eigenvector the direction of most spread?
  1. The variance of the shadows along a unit direction \(u\) is \(u^\top \Sigma u\) (the shadows are \(x \cdot u\); average their squares).
  2. \(\Sigma\) is symmetric, so its eigenvectors \(e_1, e_2\) are at right angles and form a basis. Write \(u = c_1 e_1 + c_2 e_2\); since \(u\) has length 1, \(c_1^2 + c_2^2 = 1\).
  3. Then \(\Sigma u = c_1 \lambda_1 e_1 + c_2 \lambda_2 e_2\), and dotting with \(u\) (the cross terms vanish, at right angles): \(u^\top \Sigma u = \lambda_1 c_1^2 + \lambda_2 c_2^2\).
  4. That's a weighted average of the eigenvalues, with weights \(c_1^2, c_2^2\) adding up to 1. An average is largest when all the weight sits on the biggest value: \(u = e_1\), variance \(\lambda_1\).

The same argument in any number of dimensions gives every component in turn: the best direction at right angles to the first is \(e_2\), and so on.

Press Let PCA find it. What share of the total variance does the first component hold?

\(\lambda_1 / (\lambda_1 + \lambda_2) = 0.293 / (0.293 + 0.047) \approx 0.862\). One number per outage, its position along that line, keeps 86% of the variation in two. The direction runs at about 62°, steeper than 45° because actual times vary more than the estimates do.

In 2D that's a curiosity. With a 28×28 image (784 numbers) or a word embedding (768), it's how you compress, denoise, and plot data you can't see: a few components often hold most of the variance. PCA is also the first thing to try for visualizing embeddings in stage 6. Its limit: it only finds straight directions; curved structure needs other tools.

Preview: eigenvalues are curvature (Calculus 3)

Calculus 3 will meet a loss valley's curvature as a matrix of second derivatives, the Hessian. It's symmetric, so it has eigenvectors at right angles: they're the valley's main axes, and each eigenvalue is how sharply the valley curves along its axis. Their ratio decides how slowly gradient descent crawls, and it's one more reason this lesson's eigenvectors turn up everywhere. Nothing later in this lesson depends on it.

8. All of it in code

9. What to carry forward

All lessons: AI from scratch.

Last updated