TExcelTAD
Home › Math for Machine Learning › Eigenvalues and Eigenvectors

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

λ∈R\lambda \in \mathbb{R} is an eigenvalue of A∈Rn×nA \in \mathbb{R}^{n \times n} with eigenvector x≠0\mathbf{x} \ne \mathbf{0} if

Ax=λx  ⟺  (A−λI)x=0  ⟺  det⁡(A−λI)=0A\mathbf{x} = \lambda\mathbf{x} \iff (A - \lambda I)\mathbf{x} = \mathbf{0} \iff \det(A - \lambda I) = 0

p(λ)=det⁡(A−λI)p(\lambda) = \det(A - \lambda I) is the characteristic polynomial; its roots are the eigenvalues. For each eigenvalue, the eigenvectors (plus 0\mathbf{0}) form the eigenspace ker⁡(A−λI)\ker(A - \lambda I).

Worked example: A=[4123]A = \begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix}.

p(λ)=(4−λ)(3−λ)−2=λ2−7λ+10=(λ−2)(λ−5)λ=5:[−112−2]x=0  ⟹  x=(1,1)λ=2:[2121]x=0  ⟹  x=(1,−2)\begin{aligned} p(\lambda) &= (4 - \lambda)(3 - \lambda) - 2 = \lambda^2 - 7\lambda + 10 = (\lambda - 2)(\lambda - 5) \\ \lambda = 5: &\quad \begin{bmatrix} -1 & 1 \\ 2 & -2 \end{bmatrix}\mathbf{x} = \mathbf{0} \implies \mathbf{x} = (1, 1) \\ \lambda = 2: &\quad \begin{bmatrix} 2 & 1 \\ 2 & 1 \end{bmatrix}\mathbf{x} = \mathbf{0} \implies \mathbf{x} = (1, -2) \end{aligned}
python · try it yourself
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 ≥0\ge 0. Every covariance matrix is positive semidefinite.

python · try it yourself
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 AA to a vector amplifies the component along the eigenvector with the largest ∣λ∣\lvert \lambda \rvert. Normalizing after each step gives power iteration, the simplest eigenvalue algorithm and the core of PageRank:

xk+1=Axk∥Axk∥,λ≈xkTAxk\mathbf{x}_{k+1} = \frac{A\mathbf{x}_k}{\lVert A\mathbf{x}_k \rVert}, \qquad \lambda \approx \mathbf{x}_k^T A\mathbf{x}_k

It converges at a rate set by ∣λ2/λ1∣\lvert \lambda_2 / \lambda_1 \rvert. See PageRank and Markov Chains.

In software

  • np.linalg.eig (general) and np.linalg.eigh (symmetric) call LAPACK; large sparse problems use scipy.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 [370−1]\begin{bmatrix} 3 & 7 \\ 0 & -1 \end{bmatrix}?

Answer

3 and −1-1.

Solution

det⁡(A−λI)=(3−λ)(−1−λ)\det(A - \lambda I) = (3 - \lambda)(-1 - \lambda) because the matrix is triangular; the eigenvalues of a triangular matrix are its diagonal entries.

Exercise 2: Eigenvalues of A2A^2 and A−1A^{-1}

If Ax=4xA\mathbf{x} = 4\mathbf{x}, what are A2xA^2\mathbf{x} and A−1xA^{-1}\mathbf{x}?

Answer

16x16\mathbf{x} and 14x\frac{1}{4}\mathbf{x}.

Solution

A2x=A(4x)=4Ax=16xA^2\mathbf{x} = A(4\mathbf{x}) = 4A\mathbf{x} = 16\mathbf{x}. Multiply Ax=4xA\mathbf{x} = 4\mathbf{x} by A−1A^{-1}: x=4A−1x\mathbf{x} = 4A^{-1}\mathbf{x}, so A−1x=14xA^{-1}\mathbf{x} = \frac{1}{4}\mathbf{x}. Same eigenvectors, eigenvalues λ2\lambda^2 and 1/λ1/\lambda.

Exercise 3: Power iteration (code)

Run 50 steps of power iteration on [2113]\begin{bmatrix} 2 & 1 \\ 1 & 3 \end{bmatrix} and compare with eigh.

Answer

Both give the top eigenvalue 5+52≈3.618\frac{5 + \sqrt{5}}{2} \approx 3.618.

Solution

The characteristic polynomial is λ2−5λ+5\lambda^2 - 5\lambda + 5, with roots 5±52\frac{5 \pm \sqrt{5}}{2}.

python · try it yourself
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

λ\lambda is an eigenvalue of AA exactly when…

  1. det⁡(A−λI)=0\det(A - \lambda I) = 0
  2. tr⁡(A)=λ\operatorname{tr}(A) = \lambda
  3. A=λIA = \lambda I
  4. λ=0\lambda = 0

The eigenvalues of a real symmetric matrix are…

  1. always complex
  2. always real
  3. always positive
  4. always 0 or 1

Which NumPy function should you use for a covariance matrix?

  1. np.linalg.eig
  2. np.linalg.eigh
  3. np.linalg.det
  4. np.linalg.inv