Paul's Python CoursePPC

Eigenvectors and eigenvalues

Not met yet

Multiply a direction by a matrix and it usually turns. An eigenvector is a direction that doesn't: matrix @ v comes out as value * v, the same arrow stretched by its eigenvalue. For a covariance matrix, the eigenvectors are the directions the readings spread along, and each eigenvalue is the variance along its direction. values, vectors = np.linalg.eigh(matrix), for symmetric matrices like covariances, gives the eigenvalues smallest first, and each eigenvector as a column: vectors[:, j] goes with values[j]. An eigenvector and its negative are the same line, so eigh may hand back either.

Try it

main.py
import numpy as np

spread = np.array([[9.0, 6.0],
                   [6.0, 9.0]])
values, vectors = np.linalg.eigh(spread)
print(values)
print(vectors[:, 1])
print(spread @ vectors[:, 1], values[1] * vectors[:, 1])

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks np.linalg.eigh hands back the eigenvalues biggest first, rather than smallest first.
  • Thinks the eigenvalues of a matrix are the numbers on its diagonal.
  • Thinks np.linalg.eigh gives each eigenvector as a row, rather than as a column.
  • Thinks an eigenvector and its negative are different directions, so the sign eigh gives means something.
  • Thinks multiplying by a matrix turns every direction, so no arrow keeps pointing the same way.

Where it comes up