How to Find Eigenvalues and Eigenvectors in Numpy : 3 Steps Only

How to Find Eigenvalues and Eigenvectors in Numpy

Eigenvalues and Eigenvectors have many applications. It is used in communication systems, designing bridges, designing car stereo systems, and many more. But the question is how you can find eigenvalues and eigenvectors? In this entire tutorial, you will know how to find numpy eigenvalues and eigenvectors in python.

In NumPy, there is a method for finding the eigenvalues and eigenvectors and it is linalg.eig(). The syntax of this function is below.

linalg.eig(a)

Here “a” is the input square matrix. This function returns two values w and v. The w is the eigenvalues and v is the eigenvector. In the next section, you will learn how to find them with steps.

Steps to find eigenvalues and eigenvectors in NumPy

Step 1: Import the necessary libraries

The first step is to import all the required libraries. In this entire tutorial, I am using NumPy packages only. So let’s import them using the import statement.

import numpy as np

Step 2: Create a Sample Numpy array

Now let’s create a NumPy array for demonstration purposes. You can use your own data points if you want. You can create a NumPy array using the numpy.array() method. Let’s create it.

array = np.array([[10,20,30],[40,50,60],[70,80,90]])
print(array)

Output

Sample Numpy array for finding eigenvalues and eigenvectors
Sample Numpy array for finding eigenvalues and eigenvectors

 

Step 3: Find  the Numpy eigenvalues and eigenvectors

Now the last step is to find the eigenvalues and eigenvectors of a square matrix. To find it you have to pass your input square matrix in linalg.eig() method

results = np.linalg.eig(array)
print(results[0])
print("########################################")
print(results[1])

When you use this function then it returns eigenvalues and eigenvectors as a tuple. The values at index 0 output the eigenvalues and the values at index 1 output the eigenvectors. When you will run the above code then you will get the following output.

Output

Eigenvalues and Eigenvectors of a Matrix
Eigenvalues and Eigenvectors of a Matrix

You can also get the output directly by assigning two variables in the np.linalg.eig(aray). Use the below lines of code.

evalues,evectors = np.linalg.eig(array)
print(evalues)
print("########################################")
print(evectors)

Output

Getting Eigenvalues and Eigenvectors of a Matrix directly
Getting Eigenvalues and Eigenvectors of a Matrix directly

If you are getting complex eigenvalues and want to find the real part of it then you can use the evalues.real . 

 

Conclusion

Eigenvalues are useful to simplify or find the solution of the complex matrix equations. These are the steps to find the eigenvalues and eigenvectors of a Square matrix. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

Numpy Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner