Eigenvalues and Eigenvectors
Most vectors change direction when a matrix acts on them. Eigenvectors are the special directions that only get stretched, and the stretch factor is the eigenvalue. In ML, the eigenvectors of a covariance matrix are the principal components, the top eigenvector of a link matrix is PageRank, and the eigenvalues of a Hessian say whether a training run sits in a valley or on a saddle.
Definition and the characteristic polynomial
is an eigenvalue of with eigenvector if
is the characteristic polynomial; its roots are the eigenvalues. For each eigenvalue, the eigenvectors (plus ) form the eigenspace .
Worked example: .
import numpy as np
A = np.array([[4.0, 1.0], [2.0, 3.0]])
lam, V = np.linalg.eig(A)
order = np.argsort(lam)[::-1]
lam, V = lam[order], V[:, order]
print("eigenvalues:", lam)
for i in range(2):
v = V[:, i] / V[0, i] # scale so the first entry is 1
print(f"lambda={lam[i]:.0f}: v = {v}, A v = {A @ v}")eigenvalues: [5. 2.]
lambda=5: v = [1. 1.], A v = [5. 5.]
lambda=2: v = [ 1. -2.], A v = [ 2. -4.]
NumPy returns unit-length eigenvectors with an arbitrary sign; any nonzero multiple is equally valid.
Symmetric matrices are the friendly case
Covariance matrices, Gram matrices, kernel matrices and Hessians are symmetric. For them the spectral theorem guarantees: all eigenvalues are real, and there is an orthonormal basis of eigenvectors. Use np.linalg.eigh (faster, more accurate, sorted ascending) for symmetric input.
A symmetric matrix is positive definite exactly when all its eigenvalues are positive, and positive semidefinite when they are all . Every covariance matrix is positive semidefinite.
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 3)) @ np.array([[2.0, 0, 0], [1.0, 1.0, 0], [0, 0.5, 0.2]])
S = np.cov(X.T)
lam, Q = np.linalg.eigh(S)
print("eigenvalues (ascending):", np.round(lam, 4))
print("all >= 0:", bool(np.all(lam >= 0)), " Q orthogonal:", np.allclose(Q.T @ Q, np.eye(3)))
print("top eigenvector:", np.round(Q[:, -1], 4))eigenvalues (ascending): [0.0287 1.0338 5.2745]
all >= 0: True Q orthogonal: True
top eigenvector: [-0.9778 -0.2095 0.0045]
The top eigenvector is the direction in which this dataset varies most, the first principal component (PCA: Maximum Variance).
Power iteration
Repeatedly applying to a vector amplifies the component along the eigenvector with the largest . Normalizing after each step gives power iteration, the simplest eigenvalue algorithm and the core of PageRank:
It converges at a rate set by . See PageRank and Markov Chains.
In software
np.linalg.eig(general) andnp.linalg.eigh(symmetric) call LAPACK; large sparse problems usescipy.sparse.linalg.eigsh.- The top eigenvalue of a loss Hessian (the sharpness) limits stable learning rates in gradient descent.
- Spectral clustering uses eigenvectors of a graph Laplacian to embed nodes before running k-means.
Exercises
Exercise 1: Triangular matrix
What are the eigenvalues of ?
Answer
3 and .
Solution
because the matrix is triangular; the eigenvalues of a triangular matrix are its diagonal entries.
Exercise 2: Eigenvalues of and
If , what are and ?
Answer
and .
Solution
. Multiply by : , so . Same eigenvectors, eigenvalues and .
Exercise 3: Power iteration (code)
Run 50 steps of power iteration on and compare with eigh.
Answer
Both give the top eigenvalue .
Solution
The characteristic polynomial is , with roots .
import numpy as np
A = np.array([[2.0, 1.0], [1.0, 3.0]])
x = np.array([1.0, 0.0])
for _ in range(50):
x = A @ x; x /= np.linalg.norm(x)
print(round(float(x @ A @ x), 6), round(float(np.linalg.eigh(A)[0][-1]), 6), round((5 + 5**0.5) / 2, 6))3.618034 3.618034 3.618034
Test yourself
is an eigenvalue of exactly when…
The eigenvalues of a real symmetric matrix are…
- always complex
- always real
- always positive
- always 0 or 1
Which NumPy function should you use for a covariance matrix?
np.linalg.eignp.linalg.eighnp.linalg.detnp.linalg.inv