.eig()
The .eig()
function in the numpy.linalg
module computes the eigenvalues and right eigenvectors of a square matrix. Eigendecomposition is a fundamental matrix factorization method that breaks down a matrix into its constituent eigenvalues and eigenvectors, revealing important properties about the linear transformation represented by the matrix.
Eigenvalues and eigenvectors are essential in various applications, including Principal Component Analysis (PCA), solving differential equations, quantum mechanics, vibration analysis, and stability analysis. They provide insights about a matrix’s behavior, such as its scaling properties, diagonalizability, and characteristic dynamics.
Syntax
numpy.linalg.eig(a)
Parameters:
a
: A square matrix of shape (M, M) used as input to compute its eigenvalues and corresponding right eigenvectors.
Return value:
w
: An ndarray of shape (M,), containing the eigenvalues of the matrix.v
: An ndarray of shape (M, M), containing the eigenvectors of the matrix, where each columnv[:, i]
corresponds to the eigenvaluew[i]
.
Notes:
.eig()
computes right eigenvectors. To obtain left eigenvectors, compute the right eigenvectors of the transposed matrix:v_left = np.linalg.eig(a.T)[1]
.- Eigenvalues may be complex even for real matrices.
- The eigenvectors are normalized to have a unit length.
Example 1: Basic Eigenvalue Decomposition
This example demonstrates computing eigenvalues and eigenvectors of a simple square matrix:
import numpy as np# Create a 2x2 matrixA = np.array([[4, 2],[1, 3]])print("Original matrix:")print(A)# Compute eigenvalues and eigenvectorseigenvalues, eigenvectors = np.linalg.eig(A)print("\nEigenvalues:")print(eigenvalues)print("\nEigenvectors (column-wise):")print(eigenvectors)# Verify the eigendecomposition: A * v = lambda * v# For the first eigenvalue-eigenvector pairprint("\nVerification for first eigenvector:")print("A * v:", np.dot(A, eigenvectors[:, 0]))print("lambda * v:", eigenvalues[0] * eigenvectors[:, 0])print("Are they equal?",np.allclose(np.dot(A, eigenvectors[:, 0]), eigenvalues[0] * eigenvectors[:, 0]))
The output for the example will be:
Original matrix:[[4 2][1 3]]Eigenvalues:[5. 2.]Eigenvectors (column-wise):[[0.89442719 0.70710678][0.4472136 0.70710678]]Verification for first eigenvector:A * v: [4.4721359 2.236068 ]lambda * v: [4.4721359 2.236068 ]Are they equal? True
The code creates a 2×2 matrix, calculates its eigenvalues and eigenvectors, and verifies that the eigendecomposition satisfies the equation A⋅v = λ⋅v, where A is the matrix, v is an eigenvector, and λ is the corresponding eigenvalue.
Example 2: Complex Eigenvalues
This example shows how eigenvalues can be complex for certain matrices:
import numpy as np# Create a matrix with complex eigenvaluesB = np.array([[0, -1],[1, 0]])print("Matrix B:")print(B)# Compute eigenvalues and eigenvectorseigenvalues, eigenvectors = np.linalg.eig(B)print("\nEigenvalues:")print(eigenvalues)print("\nEigenvectors:")print(eigenvectors)# Verify the eigendecomposition for complex eigenvaluesprint("\nVerification for first eigenvector:")print("B * v:", np.dot(B, eigenvectors[:, 0]))print("lambda * v:", eigenvalues[0] * eigenvectors[:, 0])print("Are they equal?",np.allclose(np.dot(B, eigenvectors[:, 0]), eigenvalues[0] * eigenvectors[:, 0]))
The output for the example will be:
Matrix B:[[ 0 -1][ 1 0]]Eigenvalues:[0.+1.j 0.-1.j]Eigenvectors:[[0.70710678+0.j 0.70710678-0.j ][0. +0.70710678j 0. -0.70710678j]]Verification for first eigenvector:B * v: [0. -0.70710678j 0.70710678+0.j ]lambda * v: [0. +0.70710678j 0.70710678+0.j ]Are they equal? True
This example demonstrates how the .eig()
function handles matrices with complex eigenvalues. The rotation matrix B has eigenvalues 1j
and -1j
, illustrating how complex eigenvalues often indicate rotational transformations.
Codebyte Example: Eigendecomposition of a Symmetric Matrix
This example explores the special properties of eigendecomposition for symmetric matrices:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours