← Back to NumPy Course | Chapter 7: Linear Algebra | Lesson 3 of 6

np.linalg.eig()

Eigenvalues and eigenvectors reveal the directions a matrix only stretches, and by how much.

In this page:

  1. np.linalg.eig()
Syntax
python
eigenvalues, eigenvectors = np.linalg.eig(matrix)

np.linalg.eig()

np.linalg.eig returns a pair: an array of eigenvalues and a matrix whose columns are the matching eigenvectors. They satisfy A v = lambda v. Eigen-decomposition underpins PCA and many physics and data-science methods.

Note: For symmetric matrices use np.linalg.eigh; it is faster and returns real values.

Example: np.linalg.eig()

python
import numpy as np

A = np.array([[2, 0], [0, 3]])
vals, vecs = np.linalg.eig(A)
print(sorted(vals))
v = vecs[:, 0]
print(np.allclose(A @ v, vals[0] * v))

# Output:
# [2.0, 3.0]
# True
Related Topics
Common Mistakes
  1. Reading eigenvectors from rows instead of columns
  2. Assuming eigenvalues come sorted
  3. Ignoring complex results for non-symmetric matrices
Chapter Summary
  • eig returns eigenvalues and eigenvectors
  • Eigenvectors are columns
  • Order is not guaranteed
  • eigh suits symmetric matrices
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.