Table of Contents
ToggleEigenvectors are special vectors associated with a matrix that provide information about the matrix’s properties.
In the context of linear algebra, if A is a square matrix, an eigenvector v corresponding to an eigenvalue λ is a non-zero vector that satisfies the equation −
Av = λv
This means that when the matrix A multiplies the vector v, the result is the same as multiplying the vector v by the scalar λ.
NumPy provides the numpy.linalg.eig() function to compute the eigenvalues and eigenvectors of a square matrix. Let us see how this function works with an example.
In this example, the eigenvalues of the matrix A are 3 and 2. The corresponding eigenvectors are shown in the output −
# Open Compiler
import numpy as np
# Define a 2x2 matrix
A = np.array([[4, -2],
[1, 1]])
# Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(A)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Output:
The output from numpy.linalg.eig() function contains two arrays: one for eigenvalues and one for eigenvectors.
The eigenvalues array contains the eigenvalues of the matrix, and each column of the eigenvectors array represents an eigenvector corresponding to the respective eigenvalue −
Eigenvalues: [3. 2.]
Eigenvectors:
[[ 0.89442719 0.70710678]
[ 0.4472136 -0.70710678]]
Eigenvectors have several important properties, they are −
Eigenvectors have numerous applications, they are −
In the following example, we are computing the eigenvalues and eigenvectors of a 3×3 matrix using NumPy −
# Open Compiler
import numpy as np
# Define a 3x3 matrix
B = np.array([[1, 2, 3],
[0, 1, 4],
[5, 6, 0]])
# Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(B)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Output:
This will produce the following result −
Eigenvalues: [-5.2296696 -0.02635282 7.25602242]
Eigenvectors:
[[ 0.22578016 -0.75769839 -0.49927017]
[ 0.52634845 0.63212771 -0.46674201]
[-0.81974424 -0.16219652 -0.72998712]]
A symmetric matrix is a matrix that is equal to its transpose (i.e., A = AT). Symmetric matrices have some special properties regarding their eigenvalues and eigenvectors −
Let us compute the eigenvalues and eigenvectors of a symmetric matrix −
# Open Compiler
import numpy as np
# Define a symmetric matrix
C = np.array([[4, 1, 1],
[1, 4, 1],
[1, 1, 4]])
# Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(C)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
Output:
Following is the output of the above code −
Eigenvalues: [6. 3. 3.]
Eigenvectors:
[[-0.57735027 -0.81649658 -0.15430335]
[-0.57735027 0.40824829 -0.6172134 ]
[-0.57735027 0.40824829 0.77151675]]
A square matrix A is said to be diagonalizable if it can be written as −
A = PDP-1
where, D is a diagonal matrix containing the eigenvalues of A, and P is a matrix whose columns are the eigenvectors of A.
Let us see how to diagonalize a matrix using NumPy −
# Open Compiler
import numpy as np
# Define a matrix
D = np.array([[2, 0, 0],
[1, 3, 0],
[4, 5, 6]])
# Compute the eigenvalues and eigenvectors
eigenvalues, eigenvectors = np.linalg.eig(D)
# Diagonal matrix of eigenvalues
D_diag = np.diag(eigenvalues)
# Reconstruct the original matrix
reconstructed_D = eigenvectors @ D_diag @ np.linalg.inv(eigenvectors)
print("Original matrix:\n", D)
print("Reconstructed matrix:\n", reconstructed_D)
Output:
The original matrix is successfully reconstructed using its eigenvalues and eigenvectors, demonstrating the process of diagonalization −
Original matrix:
[[2 0 0]
[1 3 0]
[4 5 6]]
Reconstructed matrix:
[[2. 0. 0.]
[1. 3. 0.]
[4. 5. 6.]]
Key Takeaway: Master eigenvectors with NumPy at Vista Academy!
