Eigenvalues and eigenvectors are linear algebra concepts with a variety of applications in science and engineering. Eigenvalues and eigenvectors are commonly used for singular value decomposition, dimensional reduction (principal component analysis), low rank factorization and more. These techniques are used by tech giants like Facebook, Google and Netflix for clustering, ranking and summarizing information.
This article explains how to find eigenvalues and eigenvectors of an array using the Python NumPy library.
We won’t delve into the mathematical details of eigenvalues and eigenvectors today. If that’s something you’re interested in, take a look at this resource.
Finding Eigenvalues with NumPy
The linalg module from the NumPy library allows you to perform all kinds of linear algebra operations like matrix multiplication, matrix dot product, matrix inverse, finding eigenvalues, finding eigenvectors and more. Let’s see how we can use it to find eigenvalues.
If you don’t have NumPy installed, you’ll need to first install it using pip install numpy
. Once it’s installed, you can import it using a script like this:
import numpy as np
Let’s create a dummy 2x2 array. You can use the mat()
function of NumPy, but NumPy says this function might be removed in the future in favor of NumPy arrays. Because of that, we’ll use the array()
function. The script below also prints your 2x2 array.
A1 = np.array([[4, 8],[6, 9]]) #equivalent to A1 = np.mat("4 8; 6 9")
print(A1)
Output:
[[4 8]
[6 9]]
To find eigenvalues for a matrix using NumPy, you need to call the eigvals()
method from the linalg module and pass it the matrix. The following script finds and displays eigenvalues for the array
E1 = np.linalg.eigvals(A1)
print(E1)
Output:
[-0.86545993 13.86545993]
Let’s see another example. Here we’ll find eigenvalues for a 3x3 array. The following script creates and prints your 3x3 NumPy array.
A2 = np.array([[4, 3, 9], [8, 2, 9], [1, 8, 3]]) #3x3 NumPy array
print(A2)
Output:
[[4 3 9]
[8 2 9]
[1 8 3]]
The script below finds the eigenvalues for this new
E2 = np.linalg.eigvals(A2)
print(E2)
Output:
[15.2506809 +0.j -3.12534045+2.56113001j -3.12534045-2.56113001j]
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Finding Eigenvectors with NumPy
To find eigenvectors for a matrix, you can use the eig()
method from the linalg module of the NumPy library. The eig()
method returns a tuple where the first item contains eigenvalues, while the second item contains eigenvectors. This means you can actually use eig()
by itself to calculate both eigenvalues and eigenvectors.
The following script uses the eig()
method to find eigenvalues and eigenvectors for the
A2 = np.array([[4, 3, 9], [8, 2, 9], [1, 8, 3]]) #3x3 NumPy array
Eval, Evec = np.linalg.eig(A2)
To see the eigenvalues, you can print the first item of the tuple returned by the eig()
method, which we stored in the
print(Eval)
You can see from the output below that the eigenvalues returned by the eig()
method are the same as those returned by the eigvals()
method in the last section.
Output:
[15.2506809 +0.j -3.12534045+2.56113001j -3.12534045-2.56113001j]
To find eigenvectors, you can print the value of the second item of the tuple returned by the eig()
method as shown in below. Recall we stored the second item in the tuple to a variable named
print(Evec)
Output:
[[ 0.56481192+0.j -0.52321192-0.28919939j -0.52321192+0.28919939j]
[ 0.66908052+0.j -0.42264019+0.24021012j -0.42264019-0.24021012j]
[ 0.48303079+0.j
Before we share the full code, I hope you’ll take a moment to join our free Python training program using the form at the bottom of this article. With that out of the way, here’s the full code for calculating eigenvalues and eigenvectors from a NumPy array:
import numpy as np
A2 = np.array([[4, 3, 9], [8, 2, 9], [1, 8, 3]]) #3x3 NumPy array
print(A2)
E2 = np.linalg.eigvals(A2) #calculate eigenvalues
print(E2)
Eval, Evec = np.linalg.eig(A2) #calculate eigenvalues and eigenvectors
print(Eval)
print(Evec)
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.