Course Content
Mathematics for Machine Learning
5 sections · 13 lessons
Eigenvalues, Eigenvectors, and Applications
Two coffee brands share a market. Every month, 90% of brand A's customers stay and 10% defect to B; 80% of B's customers stay and 20% come back to A. Written as a matrix that takes this month's market share to next month's:
Start with everyone on brand A, so the share vector is [1,0], and run it forward. Month one: [0.9, 0.1]. Month two: [0.83, 0.17]. Month three: [0.781, 0.219]. Month four: [0.7467, 0.2533]. Keep going and it settles at [0.667, 0.333] and stops moving.
Now start from the opposite extreme, everyone on brand B, [0,1]. Month one: [0.2, 0.8]. Month two: [0.34, 0.66]. Carry on and it converges to [0.667, 0.333] — the identical answer. Try any starting split you like. Same destination. The market has a two-thirds/one-third equilibrium baked into it, and the starting point is irrelevant.
Check what happens once you are there: 0.9(0.667)+0.2(0.333)=0.6+0.0667=0.667. The transformation moved that vector to itself. Almost every vector gets shifted and turned by M, but this one direction is fixed. Find those special directions for a matrix and you have found the skeleton the whole transformation hangs on. They are called eigenvectors, and they are the closest thing linear algebra has to a superpower.
The definition, and what it is really saying
A matrix normally does two things to a vector: turns it and changes its length. An eigenvector is a vector the matrix cannot turn. It comes out pointing exactly the way it went in, only scaled. The scale factor is its eigenvalue.
The left side is a full matrix-vector multiply, all the mixing and combining. The right side is one number times the original vector. Saying they are equal is saying the matrix acts like simple multiplication along that particular direction. The condition v=0 is not fussiness: the zero vector satisfies the equation for every λ and tells you nothing, so it is excluded by definition.
Read the eigenvalue as a verdict on that direction:
- λ>1: vectors along it get stretched, and repeated application blows them up.
- λ=1: fixed. Our coffee equilibrium.
- 0<λ<1: shrunk, and repeated application drives it to zero.
- λ<0: flipped to point the other way, and scaled by ∣λ∣.
- λ=0: that direction is crushed flat — the matrix is singular.
Eigenvectors come in families. If v is an eigenvector then so is 3v and −v, with the same eigenvalue, because only the direction matters. Software returns the unit-length member of each family so the answer is well defined.
An eigenvector is a direction the transformation respects; the eigenvalue is what it does to things pointing that way.
Finding them: the characteristic equation
Rearranging Av=λv gives Av−λv=0, and factoring out v — carefully, inserting the identity I so the subtraction is matrix-shaped — gives
We need a non-zero v that the matrix (A−λI) sends to zero. A matrix can only flatten a non-zero vector to nothing if it collapses space, and a matrix collapses space exactly when its determinant is zero. So:
That is the characteristic equation. In plain terms: find the values of λ that make A−λI singular, because those are the only values with a direction to spare. Work it on
Subtract λ from the diagonal and take the determinant:
Expand: 12−4λ−3λ+λ2−2=λ2−7λ+10. Set it to zero and factor: (λ−5)(λ−2)=0, so λ1=5 and λ2=2.
Getting the eigenvector for each eigenvalue
Substitute λ=5 back in:
The system is −v1+v2=0 and 2v1−2v2=0 — the second row is −2 times the first, which is expected, because if the rows were independent the only solution would be zero. So the single real constraint is v1=v2, and any multiple of v1=[1,1] works. Confirm: A[1,1]=[4+1, 2+3]=[5,5]=5[1,1].
Now λ=2:
Both rows say 2v1+v2=0, so v2=−2v1 and v2=[1,−2]. Confirm: A[1,−2]=[4−2, 2−6]=[2,−4]=2[1,−2]. Correct.
Two quick sanity checks, both free. The trace — the sum of the diagonal — equals the sum of the eigenvalues: 4+3=7=5+2. The determinant equals their product: (4)(3)−(1)(2)=10=5×2. If either fails, you have made an arithmetic slip.
Diagonalisation, and why matrix powers become free
Put the eigenvectors in the columns of a matrix P and the eigenvalues down the diagonal of D. Then
Read right to left, this is a three-step story. P−1 rewrites any vector in terms of the eigenvector directions. D scales each of those directions by its own eigenvalue — the easiest possible operation. P translates back to ordinary coordinates. The tangled matrix A turns out to be plain stretching, viewed in the wrong coordinate system.
The payoff is powers. Computing A20 directly means nineteen matrix multiplications. But
because every inner P−1P collapses to the identity: A2=PDP−1PDP−1=PD2P−1, and so on. Raising a diagonal matrix to a power just means raising each diagonal entry to that power. For our A, D20=diag(520,220), done in two scalar operations. This is exactly why the coffee market converged: run the transition matrix k times and each eigen-direction is multiplied by λk. The λ=1 direction holds steady while everything else decays, so after enough months only the steady state survives.
When it works, and when it does not
Diagonalisation requires n independent eigenvectors for an n×n matrix. Some matrices do not have them — a pure shear like [1011] has λ=1 twice but only one eigenvector direction, and cannot be diagonalised. Others have complex eigenvalues, which is a rotation showing up in the algebra.
Symmetric matrices are the happy exception, and they are the ones that matter most in machine learning because covariance matrices, kernel matrices and Hessians are all symmetric. The spectral theorem guarantees that a real symmetric matrix always has a full set of real eigenvalues and eigenvectors that are mutually perpendicular. So P can be built from orthonormal columns, giving P−1=PT and
with no inverse to compute at all. Perpendicular eigenvectors are what make principal components independent of one another rather than partly redundant.
| If the matrix is... | Then its eigenvalues... | Practical meaning |
|---|---|---|
| Symmetric (A=AT) | Are all real, with orthogonal eigenvectors | Safe to decompose; use eigh |
| Positive definite | Are all >0 | A bowl-shaped surface with one minimum |
| Positive semi-definite | Are all ≥0 | Valid covariance; any zero eigenvalue is a flat direction |
| Singular | Include at least one zero | Rank deficient; features are redundant |
| Triangular | Are the diagonal entries | Read them off directly |
Computing them in practice
1import numpy as np23A = np.array([[4.0, 1.0],4 [2.0, 3.0]])56vals, vecs = np.linalg.eig(A) # general square matrices7# vals -> [5.+0.j, 2.+0.j] (NumPy 2.5+ always returns complex here)8# vecs -> columns, unit length: vecs[:, 0] is the eigenvector for vals[0]910C = np.array([[5.0, 2.0],11 [2.0, 2.0]]) # symmetric: a covariance matrix12vals, vecs = np.linalg.eigh(C) # ascending order, guaranteed realThree things trip people up here. Eigenvectors are columns of the returned array, not rows, so pairing vals[i] with vecs[i] is wrong and vecs[:, i] is right. eig can return complex numbers even for a real matrix — recent NumPy releases give a complex array every time, with +0.j on real eigenvalues — and can return them in any order. Use eigh whenever the matrix is symmetric: it is faster, it exploits the structure, it guarantees real output, and it sorts ascending — so the last column is the dominant direction. Finally, a sign flip in an eigenvector is not a bug; v and −v are equally valid answers.
Principal component analysis is just this
PCA is the flagship application. Centre your data, form the covariance matrix C=n−11XTX, and decompose it. The eigenvectors are the directions of greatest variance in your data — the principal components — and each eigenvalue is the amount of variance along its direction.
Take a two-feature dataset whose covariance came out as
Characteristic equation: (5−λ)(2−λ)−4=λ2−7λ+6=0, giving λ=6 and λ=1. For λ=6, (C−6I)=[−122−4] gives v1=2v2, so the first component points along [2,1]. For λ=1 the direction is [1,−2] — perpendicular, as the spectral theorem promised, since 2(1)+1(−2)=0.
The variance explained ratio is each eigenvalue over their total:
6+16=85.7%,6+11=14.3%So a single number per sample — the projection onto [2,1] — retains 85.7% of everything the two original features told you. That is the whole logic of dimensionality reduction: sort the eigenvalues, keep enough of the top ones to reach the variance you need, discard the rest. With 200 features you might find 20 components carrying 95% of the variance, and the discarded 180 directions were mostly noise.
Eigenvalues rank directions by how much they matter. Keeping the big ones and dropping the small ones is compression with a guarantee attached.
The same idea, wearing four other hats
PageRank. Build a matrix where entry (i,j) is the probability a random surfer moves from page j to page i. Columns sum to one, exactly like the coffee market. The dominant eigenvector — the one with λ=1 — is the long-run fraction of time spent on each page, which is the ranking. Google computes it by power iteration: multiply a starting vector by the matrix over and over. Every other component shrinks by ∣λ∣<1 each round, so the dominant direction takes over. That is precisely what you watched happen with two coffee brands, at the scale of billions of pages.
Spectral clustering. Turn your data into a graph where similar points are connected, build its Laplacian matrix, and look at the eigenvectors with the smallest eigenvalues. Those coordinates pull apart clusters that k-means cannot separate, because they respond to connectivity rather than to straight-line distance — two interleaved crescent shapes, for instance.
Optimisation. At a point where the gradient is zero, the Hessian — the matrix of second derivatives — tells you what kind of point it is, and its eigenvalues give the verdict.
Hessian eigenvalues Critical point What training does there All positive Local minimum Curves upward everywhere; you have arrived All negative Local maximum Curves down everywhere; gradient ascent's target Mixed signs Saddle point Down in some directions, up in others — gradients go near zero and progress stalls Some zero Flat direction Inconclusive; the loss is unchanged along it Saddle points, not local minima, are what actually slow down training in high dimensions: with a thousand parameters, needing all thousand eigenvalues to share a sign is vastly less likely than getting a mixture. The ratio of largest to smallest eigenvalue also sets how badly the loss surface is stretched, which is why plain gradient descent zigzags down long narrow valleys and why adaptive optimisers help.
Deep networks. A signal passing through many layers is repeatedly multiplied by weight matrices, so it is repeatedly scaled by their eigenvalues. If the dominant magnitude sits above 1, then λk explodes across k layers; below 1, it vanishes. That is the exploding and vanishing gradient problem stated exactly. Orthogonal initialisation, which forces eigenvalue magnitudes to 1, and gradient clipping, which caps the damage, are both direct responses to this arithmetic.
Using this on a real problem
When you next reach for PCA, do not stop at the transformed data. Print the eigenvalues. Their pattern is a diagnosis of your feature set: a sharp drop after a handful of components means your 200 columns were secretly describing about six things, and a nearly flat spectrum means every feature carries independent information and reduction will cost you. A near-zero eigenvalue is a hard signal that two columns are duplicates — the same unit measured twice, or a one-hot encoding where you forgot to drop a category — and it will make any matrix inverse downstream unstable.
Three habits are worth forming. Use
eigh, nevereig, on anything symmetric, and treat a complex eigenvalue from a matrix you believed was symmetric as evidence you built it wrong. Standardise features before PCA, because eigenvalues chase raw variance and a column measured in millions will monopolise the first component for no better reason than its units. And when a model trains but will not converge, remember that the shape of the loss surface is an eigenvalue question before it is a hyperparameter question.The pattern under all of it is the one the coffee market showed in four lines of arithmetic: find the directions a transformation leaves alone, and the transformation stops being complicated.