| by Arround The Web | No comments

NumPy Eigenvalues

“Numpy eigenvalues are the function in the Python script which allows us to compute the eigenvalues for a given matrix. Eigenvalues have a vast number of applications in the field of machine learning, datasets, and control systems. These values define the system stability in control systems, help in extracting the features such as dimensionality reduction, and also allow to find of the best fit line for the data using the machine learning algorithms. Numpy belongs to those available packages that are provided by python for dealing with various functions that are relevant to nd-arrays and matrices. To compute the eigenvalues for any nd-array, we use the built-in function provided by the numpy package “numpy. linalg ()”. We may compute the eigenvectors for the eigenvalues using the same formula since they are interrelated.”

Procedure

This article comprises all the details to implement the numpy eigenvalue function in the python script. The article first gives a brief introduction to the eigenvalue and the numpy library, and then it shows the method of implementation of this function on the distinguished examples. To work with this function, we have to download the python compiler and install and import the numpy packages.

Syntax

The syntax to call the function of numpy eigenvalue is quite simple and is given as follows:

\$ numpy. linalg.eig()

This function takes in any matrix or nd-array that is square in nature and returns the eigenvalues and the eigenvectors for that matrix. A multi-dimensional array is known as the square matrix, and this matrix represents all the information related to the system or the data set. Now that we have learned about the syntax for this function call, so now we should try implementing this function on the various examples.

Example # 01

To compute the eigenvalues of any system, we should know its matrix. So, we will hypothetically define a square matrix or 2d (two-dimensional) array since the matrix and the nd-array are almost the same, but their declaration method varies a little bit from each other. To create an nd-array or matrix for the system, we will first import the Numpy library as the “np” so that we can utilize this name, where we will be required to call the Numpy. After importing the numpy, we will now step forward and will declare and initialize a 2d- array with the values or its elements as “[2, 2], [4, 4]”. This declaration will be done by calling the “np. array()” method, and then we will pass these values to this function as its parameter and will save the results in some variable “a”. This variable “a” now has the system matrix stored in it. After initializing the 2d-array, we will now compute eigenvalues and eigenvectors by calling the function “np”. linalg. eig(array)”.To this function, we will pass the nd-array that we have already created, and it will return the two parameters one eigenvalue, which we will store in the variable as “eigenvalue”, and the second parameter would be eigenvectors which would be then stored in the variable, as “evec” and then we will display these two parameters by calling the print function as “print (name of the parameter)”. We have described this whole explained example in the form of the python code in the following figure, and now we will try to execute this script to verify whether our code is built correctly or not.

import numpy as np
a = np.array([[2, 2],
[4, 4]])
eigenvalue, evec = np.linalg.eig(a)
print(" The Eigen values:\n",
eigenvalue )
print("The  eigenvectors :\n",
evec)

After the execution of the code, for example, number 1, the code build was created successfully, and the code returned and displayed the two parameters for the system’s nd-array as the “eigenvector” and “eigenvalues” that can be seen in the snippet of the output.

Example # 02

The previous example has taken a square matrix of the order 2×2, which is, in fact, the 2-d array. So, in this example, we will try to upgrade the concept a step further and will compute the eigenvalues for the system having the system matrix of order 3×3. To begin with this example, we will create a new project in the Python compiler, and then we will import the basic python libraries and the packages that we will require to work with later in the project.

We will install the Numpy package from the python libraries, and then from these installed packages, we will import the numpy as the word “np”. Now we will use this np instead of Numpy in the code. Let us move further and create a 3-dimensional array for the system. A 3-dimensional array consists of three rows and three columns. We will call the function “np. array()” and pass the elements to the array in the 3×3 order as “[2, 2, 3], [3, 2, 4], [5, 4, 6]”. Once the 3-d array is initialized, then we will try to find the eigenvalues for this array, and we will again call the function as we have done in the previous example as “np.linalg.eig(array)”. To the function, we will pass the array, and it will return the eigenvalues and the vectors for the system’s matrix.

import numpy as np
array = np.array([[2, 2, 3],
[3, 2, 4],
[5, 4, 6]])
eigenvalue, evec = np.linalg.eig(array)
print(" The Eigen values:\n",
eigenvalue )
print("The  eigenvectors :\n",
evec)

The above figure represents the code snippet of the scenario that we have just discussed in the second example of this article in the form of the python script. We can simply copy this code and try to run it on our compilers and check the output. The output of the code has returned exactly the two parameters that are eigenvalues and eigenvectors, in the form of the complex numbers that we wanted to be computed for our system’s matrix, which was a square matrix of 3×3 dimensions (3d- array).

Conclusion

We will summarize the article by once again reviewing the steps that we have taken in this article. We have given a brief history of the concept of eigenvalues with numpy packages. Then we discussed the syntax for the implementation of the eigenvalues using the Numpy package, and finally, we explained and implemented in detail the eigenvalues for the nd-arrays or the system matrices.

Source: linuxhint.com