Eigenvectors, briefly
Last updated
Most arrows get turned when a matrix acts on them. A few special ones don't: they stay on their own line and only stretch or shrink. Those are eigenvectors, and they're how principal component analysis (PCA, lesson 4.10) finds the directions in which data spreads out most. This lesson is short on purpose: what they are, how NumPy finds them, and why symmetric matrices are the friendly case.
By the end of this lesson you can
- Say what an eigenvector and its eigenvalue are, in words and as
- Find eigenvalues and eigenvectors with NumPy and check them
- Explain why PCA uses the eigenvectors of a symmetric covariance matrix
Arrows that only stretch
Take the matrix . Feed it the arrow and you get : longer, and turned off its line. Most arrows go that way. Turn the dial and watch where each one lands:
v = (0.94, 0.34) goes to Av = (2.22, 1.62). Av points off v’s line, so the matrix turned it. Keep turning v to find the directions it doesn’t turn.
At 45° something different happens: goes to , the same direction, three times as long. At 135°, goes to : it doesn't move at all. Those two lines are special for this matrix.
(The notebook uses , which points the opposite way along the same line as , so it's the same special line.)
The equation Av = λv
An arrow that the matrix only stretches is an eigenvector, and the amount it stretches by is its eigenvalue, written (lambda):
In words: multiplying by the matrix gives the same answer as multiplying it by the plain number . In NumPy: np.allclose(A @ v, lam * v) is True.
For our : is an eigenvector with , and is one with . Any multiple of an eigenvector is one too (the whole line is special), so people usually pick the one of length 1. A negative would flip the arrow round on its line; would squash it to nothing, which is what a singular matrix does to some direction (lesson 1.4).
Finding them with NumPy
np.linalg.eig returns the eigenvalues and a matrix whose columns are the eigenvectors, each of length 1:
Column 0, , is shrunk to length 1, paired with eigenvalue 3. Column 1 is , paired with eigenvalue 1. The last cell checks the first pair against the equation. It's a column, vectors[:, 0], not a row: taking vectors[0] is a classic slip that gives the wrong vector.
Symmetric matrices, and why PCA needs them
A matrix that equals its own transpose (lesson 1.4) is symmetric, and symmetric matrices are the friendly case. Their eigenvalues are always real numbers and their eigenvectors are always at right angles to each other, so they make a fresh set of axes. NumPy's np.linalg.eigh is the version for them; it returns the eigenvalues smallest first.
Where do symmetric matrices come from? The covariance matrix of some data (lesson 4.10 builds it properly) says how each pair of features varies together, and it's always symmetric. Here are 500 made-up points where tends to rise with :
The eigenvector with the biggest eigenvalue (1.75, much bigger than 0.083) points along the direction the cloud of points is stretched in. It comes out as ; its sign is arbitrary, and is the same line. That's PCA in one sentence: the eigenvectors of the covariance matrix, biggest eigenvalue first, are the directions the data spreads along most. Lesson 4.8 uses them to squash many features down to two you can plot.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 2: Linear Algebra: eigendecomposition (2.7) and PCA (2.12)
- NumPy: numpy.linalg.eig: eigenvalues and eigenvectors, one vector per column
- NumPy: numpy.linalg.eigh: for symmetric matrices, eigenvalues in ascending order
- 3Blue1Brown, Essence of Linear Algebra: eigenvectors as arrows that stay on their line