Lesson 9 of 117 min
Eigenvectors and eigenvalues
When a matrix moves the plane, most arrows turn; an eigenvector is an arrow it only stretches, and its eigenvalue says how many times.
After this lesson you can find the directions a transformation only stretches.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
`eig` for any square matrix, and its `j`
The video used eigh, for a matrix that is the same on both sides of its diagonal. For any other square matrix, use eig. Since NumPy 2.5 it writes every answer with a j: 3.+0.j is just 3, and .real drops the +0.j. A real j, as for the quarter turn, means every arrow turns.
import numpy as np
M = np.array([[2, 1], [1, 2]])
values, vectors = np.linalg.eig(M)
print(values)
print(values.real)
turn = np.array([[0, -1], [1, 0]])
print(np.linalg.eig(turn).eigenvalues)[3.+0.j 1.+0.j] [3. 1.] [0.+1.j 0.-1.j]
Length one, and either sign
eigh gives each eigenvector shrunk to length one, so [1, 1] comes back as 0.71 twice. Any arrow on the same line is an eigenvector too, pointing either way. You can check one yourself: the matrix times it is the eigenvalue times it.
M = np.array([[2, 1], [1, 2]])
values, vectors = np.linalg.eigh(M)
v = vectors[:, 1]
print(v, np.linalg.norm(v))
print(M @ v)
print(3 * v)
print(M @ -v)[0.70710678 0.70710678] 0.9999999999999999 [2.12132034 2.12132034] [2.12132034 2.12132034] [-2.12132034 -2.12132034]
A negative eigenvalue
A flip turns the arrow up round, so it points straight down. It stays on its own line, only the other way, and its eigenvalue is minus one. The arrow across does not move at all: eigenvalue one.
flip = np.array([[1, 0], [0, -1]])
print(flip @ np.array([0, 1]))
values, vectors = np.linalg.eigh(flip)
print(values)[ 0 -1] [-1. 1.]
The three pages, round by round
These are the first five rounds of the video's three pages. The shares swing back and forth, and each swing is smaller than the one before, until they settle on 0.4, 0.2 and 0.4.
links = np.array([[0, 0, 1],
[0.5, 0, 0],
[0.5, 1, 0]])
shares = np.array([1, 1, 1]) / 3
for step in range(1, 6):
shares = links @ shares
print(step, shares.round(2))1 [0.33 0.17 0.5 ] 2 [0.5 0.17 0.33] 3 [0.33 0.25 0.42] 4 [0.42 0.17 0.42] 5 [0.42 0.21 0.38]
Shares that never settle
Not every web settles. Two pages that only link to each other just swap their people, round after round. The swap has two eigenvalues, 1 and minus one, the same size, so neither line wins.
swap = np.array([[0, 1], [1, 0]])
shares = np.array([1, 0])
for step in range(4):
shares = swap @ shares
print(shares)[0 1] [1 0] [0 1] [1 0]
Google's twist
PageRank added one more thing. Now and then, the person stops following links and jumps to any page at random. Brin and Page had them follow a link 85% of the time. With the jump, the shares always settle, even for the swap. On the three pages, the third page now comes out a little ahead.
shares = np.array([1, 1, 1]) / 3
for step in range(100):
shares = 0.15 / 3 + 0.85 * (links @ shares)
print(shares.round(3))[0.388 0.215 0.397]
Run on Python 3.13, NumPy 2.5.5. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: A matrix is a transformation
NumPy prints M = np.array([[1, 1], [0, 1]]) as two rows: [1 1], then [0 1]. What does M do to the handwritten zero?
2 of 3
The matrix [[2, 1], [1, 2]] moves the plane. Which of these arrows does it only stretch, so that it lands on its own line?
3 of 3
[[2, 1], [1, 2]] stretches [1, 1] three times. You move [1, 1] with this matrix, then move where it landed once more. What is the first number of where it ends?
The ideas in this lesson
- Eigenvector
- An arrow a matrix only stretches: it lands on its own line, however long.
- Eigenvalue
- How many times a matrix stretches its eigenvector; moved again and again, the biggest one wins.
It leans on
- PCA
Transcript
What the video says, part by part. Press a time to watch from there.
The challenge
Here's a challenge. When a matrix moves the plane, almost every arrow turns. But a few arrows don't turn. They only stretch. Google's first way of ranking the web was built on that idea. Here's a matrix and three arrows. One of them won't turn. Can you find it?
Only stretched
A matrix moves every point of the plane. Its first column says where the arrow one, zero lands, and its second says where zero, one lands. Let's move it. The arrow one, zero lands on the first column, two, one. It points a new way. One, two lands on four, five. It turned too. But one, one lands on three, three... on its own line. It only got longer. If you guessed one, zero: it points along the grid, so it looks special. But it turns.
An arrow that a matrix only stretches... is called an eigenvector.
Eigen is German for own: it's the matrix's own direction.
A whole line
Is it one special arrow? No. Two, two lands on six, six, on the same line. Every arrow on that line only stretches.
Now watch a whole circle of arrows. Almost every one turns. Only the arrows on two lines stay on them: the line of one, one, and the line of one, minus one. The circle becomes a stretched circle. So an eigenvector is really a whole line, a direction the matrix keeps.
How many times
An eigenvector is an arrow a matrix only stretches. So how many times?
One, one became three, three: three times as long. That three is its eigenvalue.
Now the other line, and the arrow one, minus one. Where does it land? Work it out from the columns.
One of the first column, two, one... minus one of the second, one, two.
That's one, minus one: itself.
Its eigenvalue is one. It stays as it is.
Again and again
So what is an eigenvalue good for?
It tells you what happens when a matrix moves something again and again. Take one, zero. Move it, then move where it landed. Again, and again. Two, one... five, four... fourteen, thirteen. Where is it going?
Toward the line of one, one.
Fourteen and thirteen are almost the same.
Each half on its own
Why?
One, zero is half of one, one... plus half of one, minus one.
The matrix moves each half on its own, the same way it moves every walk on the grid. The first half gets three times longer every time. The second half stays the same.
So the line with the biggest eigenvalue... wins.
That's how a computer can find the strongest line: move any arrow again and again, and see where it points.
One line of NumPy
In NumPy, one line finds them all. It gives the eigenvalues, smallest first: one, and three. And the eigenvectors, as columns, each shrunk to length one. Which column goes with the three?
The second one. Its two numbers are the same, so it's the line of one, one.
Does every matrix?
Here's a matrix that turns the whole plane, a quarter of the way around. Does it have an arrow it only stretches? Does every matrix?
A turn keeps no line
The quarter turn. Will any arrow keep its line?
None. Every arrow turns a quarter of the way around, so no line stays.
NumPy agrees. Ask it for this matrix's eigenvalues, and it can't answer with plain numbers. Each answer comes with a j. That's NumPy's sign that no arrow keeps its line.
In space
So does a turn never keep a line?
In space, it does. Turn a box a quarter of the way around a line that points straight up, the way a globe spins. Everything moves... except that line. The arrow up stays where it is: an eigenvector, with eigenvalue one, like the line through a globe's two poles.
Google's first ranking
So where is this used?
Here are three web pages and their links. People click links at random, starting with a third on each page. Every round, they follow the links. The shares change... then stop changing: forty, twenty, forty percent. One more round leaves them as they are.
Written as a matrix, the links have these shares as an eigenvector, with eigenvalue one. A page that ends up with more of the people is more important. That was Google's first way of ranking the web: PageRank.
Machine learning uses eigenvectors as well: they're the directions a table of data spreads out along the most. That's called PCA.
In NumPy
Now let's run those rounds in NumPy. The links go in a matrix, one column for each page. The first page sends half of its people to each of the other two. The second page sends everyone to the third, and the third sends everyone back to the first. The people start a third on each page. Then a loop runs fifty rounds. Each round is the links, times the shares. What will it print?
It prints the same forty, twenty, forty percent. And one more round gives the same shares back: an eigenvector, with eigenvalue one. So the first and the third pages rank highest. And all it took was one line, run again and again.
Only stretched, how many times
So a matrix turns almost every arrow. The few it only stretches are its eigenvectors. And each eigenvalue says how many times. When an arrow is moved again and again, the line with the biggest eigenvalue wins.
The lab
That's the end of this unit. In its lab, you'll use these two lessons to find the best road through the penguins. It's the one that leaves the smallest errors.
Only stretched
So an eigenvector is an arrow a matrix only stretches. Now try it on this lesson's page, and take the quiz. Pick the lean, and find the one line it keeps.
You use this lesson in Lab 3: The penguins' best road, which comes next.
Facts last checked on .
