An eigenvector v of matrix A is a special vector whose direction is unchanged by the transformation A represents — A applied to v is exactly a scalar multiple of v, and that scalar is the corresponding eigenvalue. np.linalg.eig(A) returns a tuple of eigenvalues and eigenvectors, where eigenvalues is a 1D array and eigenvectors is a 2D array whose columns, not rows, are the corresponding eigenvectors — a common source of indexing mistakes. Eigenvalues can be complex even for a real-valued input matrix, so the returned arrays may have a complex dtype.
1Understanding np.linalg.eig()
An eigenvector v of matrix A is a special vector whose direction is unchanged by the transformation A represents — A applied to v is exactly a scalar multiple of v, and that scalar is the corresponding eigenvalue. np.linalg.eig(A) returns a tuple of eigenvalues and eigenvectors, where eigenvalues is a 1D array and eigenvectors is a 2D array whose columns, not rows, are the corresponding eigenvectors — a common source of indexing mistakes. Eigenvalues can be complex even for a real-valued input matrix, so the returned arrays may have a complex dtype.
Remember eigenvectors are returned as the columns of the eigenvectors matrix, not the rows — indexing a specific column gives the eigenvector for the eigenvalue at that same position, not indexing a row.
import numpy as np
A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)2Practical Example
Here is a real-world application of np.linalg.eig() showing how it is used in production NumPy code.
import numpy as np
A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
v = eigenvectors[:, 0]
print(np.allclose(A @ v, eigenvalues[0] * v))3Best Practices
Follow these guidelines when working with np.linalg.eig():
1. Extract eigenvectors by column, not by row, since that's how np.linalg.eig() actually returns them
2. Check whether the result's dtype is complex, since even a real input matrix can have complex eigenvalues
3. Use np.linalg.eigvals() instead of eig() when you only need the eigenvalues and not the eigenvectors, since it's slightly cheaper to compute
Tip: Remember eigenvectors are returned as the columns of the eigenvectors matrix, not the rows — indexing a specific column gives the eigenvector for the eigenvalue at that same position, not indexing a row.
import numpy as np
A = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)