Python Scipy Eigenvalues [7 Useful Examples]

In this Python tutorial, we will learn about “Python Scipy Eigenvalues” where we will know how to find eigenvalues and eigenvectors of the given matrix. And we will also cover the following topics.

  • What is Eigenvalues
  • Python Scipy Eigenvalues
  • Python Scipy Eigenvalues Hermitian
  • Python Scipy Eigenvalues Eigvals_only
  • Python Scipy Eigenvalues Subset_by_value
  • Python Scipy Eigenvalues Subset_by_index
  • Python Scipy Eigenvalues and Eigenvectors

Also, check how to install Scipy in Python: Installation of Scipy

What is Eigenvalues

In the context of the system of linear equations, eigenvalues refer to a unique set of scalars. Most often, matrix equations use it. The word “Eigen” in German implies “proper” or “characteristic.” Eigenvalues can therefore also be referred to as appropriate values, latent roots, characteristic values, and characteristic roots.

  • The eigenvalue is a scalar that is used to alter the eigenvector, In simple words. The fundamental formula is Ax = λx, A’s eigenvalue “λ” is an integer or scalar value.

Let’s also know about the term ‘eigenvector’ which is related to eigenvalues.

An eigenvector in mathematics is equivalent to real non-zero eigenvalues that point in the direction extended by the transformation, whereas an eigenvalue is thought of as a factor by which it is stretched. The transformation’s direction is reversed if the eigenvalue is negative.

The non-zero vectors known as eigenvectors remain in the same direction after applying any linear transformation. Only one scalar factor is changed. If A is a linear transformation from vector space V and x is a vector there that is not zero, then v is an eigenvector of A if A(X) is a scalar multiple of x.

An eigenspace for a given vector x is made up of all the eigenvectors that collectively have an identical eigenvalue to the zero vector. The zero vector is not, however, an eigenvector.

  • Let’s assume that A is an “nxn” matrix and that is an eigenvalue of matrix A. If x, a non-zero vector, matches the given expression below, it is said to be an eigenvector.

Ax = λx

Its eigenvector is x. A is the same as the eigenvalue(λ).

In this tutorial, we will learn about how to use the method of Python Scipy to compute the eigenvalues and eigenvectors of the given array or matrix.

Also, read: Scipy Optimize – Helpful Guide

Python Scipy Eigenvalues

The method eigvals() of Python Scipy exists in a module scipy.linalg() that Identifies the eigenvalues in a regular or generalized eigenvalue problem.

The syntax is given below.

scipy.linalg.eigvals(a, b=None, check_finite=False, overwrite_a=True, homogeneous_eigvals=True)

Where parameters are:

  • a(array_data,(M,M)): A real or complex matrix whose eigenvalues and eigenvectors have to be determined.
  • b(array_data,(M,M)): Matrix on the right side of a generalised eigenvalue issue. The identity matrix is deemed to exist if removed.
  • check_finte(boolean): Whether or not to make sure that the input matrices only have finite numbers. If the inputs actually contain infinities or NaNs, disabling them could improve performance but cause issues (non-termination, crashes).
  • overwrite_a(boolean): which data should be overwritten in a.
  • homogeneous_eigvals(boolean): In the case when True, give the eigenvalues in homogeneous coordinates.
READ:  How to Filter DataFrame in Python Pandas | 11 Ways to Filter Pandas DataFrame

The method eigvals() returns w(The eigenvalues, which are not in any particular order but are each repeated according to their multiplicity. Unless homogeneous eigvals=True, the shape is (M,)) of type complex ndarray or double.

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import eigvals

Create an array or matrix using the below code.

matrix = np.array([[-1, .0], [0., 1.]])

Now compute the eigenvalues of the above-created matrix using the below code.

eigvals(matrix)
Scipy Eigenvalues
Scipy Eigenvalues

In the above output, the eigenvalues of the matrix are [-1.+0.j, 1.+0.j].

This is how to compute the eigenvalues from a given matrix using the method eigvals() of Python Scipy.

Read: Scipy Rotate Image + Examples

Python Scipy Eigenvalues Hermitian

First, we need to know “What is the Hermitian matrix?” A square matrix, which is the same as its conjugate transpose matrix, is a hermitian matrix. A hermitian matrix’s nondiagonal components are all complex integers. A hermitian matrix’s complex numbers are set up so that the ith row and jth column’s element are the complex conjugates of the jth row and ith column’s element.

Python Scipy Eigenvalues Hermitian
Python Scipy Eigenvalues Hermitian

If A = AT, then matrix A is a hermitian matrix. Similar to a symmetric matrix, a hermitian matrix differs from one in that the components of its non-principal diagonal are complex numbers.

The Python Scipy has a method eigh() within the module scipy.linalg to deal with standard ordinary eigenvalue problems for real symmetric or Hermitian matrices.

The syntax is given below.

scipy.linalg.eigh(a, b=None, eigvals_only=True, lower=False, overwrite_a=True, overwrite_b=True, turbo=False, eigvals=None, type=1, check_finite=False, subset_by_index=None, subset_by_value=None, driver=None)

Where parameters are:

  • a(array_data): The computation of the eigenvalues and eigenvectors of a complex Hermitian or real symmetric matrix.
  • b(array_data): A real symmetric, complex Hermitian, positive matrix. The identity matrix is presumed if missing.
  • lower(bool): Whether the triangles in lower or upper thirds of a, and, if appropriate, b, are where the relevant array data is obtained from. Lower by default
  • egvals_only(boolean): If just eigenvalues should be calculated and not eigenvectors. (by default: both are computed)
  • overwrite_a(boolean): To overwrite a.
  • overwrite_b(boolean): To overwrite b.
  • check_finite(boolean): If it is necessary to verify that the input matrices only contain finite numbers. Disabling may improve performance, but if the inputs do contain infinities or NaNs, it may cause issues (crashes, non-termination).
  • subset_by_index(iterable): This two-element iterable, if given, specifies the half-open interval (a, b] within which, if any, only the eigenvalues between these values are returned. exclusive to “evr,” “evx,” and “gvx” drivers. For the unconstrained endpoints, use i9 bnp.inf.]-0965\
  • subset_by_value(iterable): To define the half interval to get only eigenvalues using a two-element that is iterable.

The method eigh() returns the w(selected eigenvalues) in increasing size of type ndarray.

Let’s understand with an example by following the below steps:

Import the required libraries using the below code.

import numpy as np
from scipy.linalg import eigh

Create an array of data as a matrix using the below code.

matrix_data = np.array([[5, 3, 6, 1], [5, 1, 3, 0], [2, 6, 5, 1], [1, 5, 2, 2]])

Pass the created matrix data to the method eigh() using the below code.

eigh(matrix_data)
Python Scipy Eigenvalues Hermitian
Python Scipy Eigenvalues Hermitian

The output shows both eigenvalues and eigenvectors of the given matrix. What will happen, if we need only eigenvalues and no eigenvectors. The next sub-section is all about it.

READ:  Matplotlib two y axes

Read: Scipy Stats – Complete Guide

Python Scipy Eigenvalues Eigvals_only

The Python Scipy method eigh() returns both eigenvalues and eigenvectors, sometimes we need only one value like eigenvalues. To get only eigenvalues, the method eigh() has a parameter eigvals_only of type boolean or it accepts the True or False value.

If we set the eigvals_only equal to True, then it returns only the eigenvalues, otherwise returns both eigenvalues and eigenvectors.

Now understand with example by following the below steps:

Import the required libraries or methods using the below python code.

import numpy as np
from scipy.linalg import eigh

Create a matrix containing values by using the below code.

matrix_data = np.array([[6, 3, 5, 1], [3, 1, 0, 3], [1, 6, 5, 6], [2, 5, 2, 2]])

To compute and get only eigenvalues, then set the parameter eigvals_only to True using the below code.

eigh(matrix_data,eigvals_only = True)
Python Scipy Eigenvalues Eigvals only
Python Scipy Eigenvalues Eigvals only

When we pass the matrix to a method eigh() with a parameter eigvals_only equal to True, as we can see in the output, the method returns only the eigenvalues of the matrix.

This is how to compute the eigenvalues of the given matrix using the method eigh() of Python Scipy.

Read: Python Scipy FFT

Python Scipy Eigenvalues Subset_by_value

The subset_by_value is another parameter of method eigh() to inquire about eigenvalues that are under a specific range. For instance, if we need eigenvalues higher than 5, or lower than 8, then the method returns all the eigenvalues higher than 5, or lower than 8.

Let’s see with an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import eigh

Generate a matrix of data using the method np.array() as shown in the below code.

matrix_data = np.array([[6, 3, 5, 1], [3, 1, 0, 3], [1, 6, 5, 6], [2, 5, 2, 2]])

Now pass the above matrix to a method eigh() with a parameter subset_by_value equal to [-np.inf, 5], to get eigenvalues less than 5 only.

eigh(matrix_data,eigvals_only = True, subset_by_value= [-np.inf, 5])

Again change the value of the parameter subset_by_value to [10, 20], to get the eigenvalues between 10 and 20 using the below code.

eigh(matrix_data,eigvals_only = True, subset_by_value= [10, 20])
Python Scipy Eigenvalues Subset by value
Python Scipy Eigenvalues Subset by value

This is how to get the specific range of eigenvalues using the method eigh() with parameter subset_by_value of Python Scipy.

Read: Scipy Linalg – Helpful Guide

Python Scipy Eigenvalues Subset_by_index

We already know that method eigh() returns the as ndarray type, and we also know that the array elements or values can be accessed by its index value. So the method eigh() has a parameter subset_by_index that allows us to access the eigenvalues or eigenvectors of the ndarray using its index value.

READ:  Matplotlib multiple plots

Now we are going to understand, how we can use the parameter subset_by_index with help of an example.

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import eigh

Generate a matrix of data using the method np.array() as shown in the below code.

matrix_data = np.array([[6, 3, 5, 1], [3, 1, 0, 3], [1, 6, 5, 6], [2, 5, 2, 2]])

Now pass the above matrix to a method eigh() with a parameter subset_by_index equal to [0, 2], to get eigenvalues from index 0 to 2.

eigh(matrix_data,eigvals_only = True, subset_by_value= [0, 2])
Python Scipy Eigenvalues Subset by index
Python Scipy Eigenvalues Subset by index

This is how to get the specific range of eigenvalues using the method eigh() with parameter subset_by_value of Python Scipy.

Read: Scipy Stats Zscore + Examples

Python Scipy Eigenvalues and Eigenvectors

The Python method eig() that exist in a module scipy.linalg identify and resolve a square matrix’s ordinary or generalized eigenvalue problem.

The syntax is given below.

scipy.linalg.eig(a, b=None, left=True, right=False, overwrite_a=True, overwrite_b=True, check_finite=False, homogeneous_eigvals=True)

Where parameters are :

  • a(array_data): A real or complex matrix whose eigenvalues and eigenvectors have to be determined.
  • b(array_data): To input the right-hand side matrix.
  • right(boolean): An extended eigenvalue problem’s right-side matrix. The identity matrix is presumed when None is the default.
  • left(boolean): Whether to calculate the left eigenvectors and return them. False is the default.
  • overwrite_a(boolean): To overwrite a.
  • overwrite_b(boolean): To overwrite b.
  • check_finite(boolean): To check if the provided matrix as input has finite numbers.
  • homogeneous_eigvals(boolean): To get the eigenvalues in homogenous coordinates

The method eig() returns the w(the eigenvalues), vr(the right vector that is normalized) and vl(the left vector that is normalized) of type complex ndarray or double

Let’s take an example by following the below steps:

Import the required libraries using the below python code.

import numpy as np
from scipy.linalg import eig

Generate matrix data using the below code.

matrix_data = np.array([[-2.,0],[0.1,2]])

Now compute the eigenvalues and eigenvectors of the above-created matrix using the below code.

eig(matrix_data)
Python Scipy Eigenvalues and Eigenvectors
Python Scipy Eigenvalues and Eigenvectors

This is how to compute the eigenvalues and eigenvectors of the given matrix using the method eig() of Python Scipy.

You may also like to read the following Python SciPy tutorials.

So, in this tutorial, we have learned about “Python Scipy Eigenvalues” and covered the following topics.

  • What is Eigenvalues
  • Python Scipy Eigenvalues
  • Python Scipy Eigenvalues Hermitian
  • Python Scipy Eigenvalues Eigvals_only
  • Python Scipy Eigenvalues Subset_by_value
  • Python Scipy Eigenvalues Subset_by_index
  • Python Scipy Eigenvalues and Eigenvectors