In this Python Scipy tutorial, we will learn, “**Python Scipy Linag Eigh**” which is used to compute the eigenvalues or eigenvectors of the hermitian or real symmetric matrix.

- Python Scipy Linalg Eigh
- Python Scipy Linalg Eigh Tridiagonal
- Python Scipy Linalg Eigh Egvals Only
- Python Scipy Linalg Eigh Return

## Python Scipy Linalg Eigh

The method

of Python Scipy that exists in a module *eigh()*

for a complex Hermitian or real symmetric matrix, to resolve a conventional or generalized eigenvalue problem.*scipy.linalg*

The syntax is given below.

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

Where parameters are:

**a(array_data):**The eigenvalues and eigenvectors of a complex Hermitian or real symmetric matrix will be calculated.**b(array_data):**a real symmetric, complex Hermitian, positive matrix. The identity matrix is presumed if missing.**lower(boolean):**if the relevant array data is drawn from the lower or the upper triangle from a and, if applicable, b.**eigvals_only(boolean):**if just eigenvalues should be calculated and not eigenvectors (by default both are computed).**overwrite_a(boolean):**Whether or not to replace data in a.**overwrite_b(boolean):**Whether or not to replace data in b.**subset_by_index(iterable):**The start and end indices of the desired eigenvalues are defined by this two-element iterable if one is provided (ascending order and 0-indexed). [1, 4] is used to only return the second through fifth smallest eigenvalues. The largest three are returned by [n-3, n-1]. exclusive to “evr,” “evx,” and “gvx” drivers. By using int, the entries are immediately transformed to integers ().**subse_by_value(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 np.inf.**driver(string):**Specifies the appropriate LAPACK driver to use. For ordinary issues, you can choose from “ev”, “evd,” “evr,” or “evx,” whereas for generalised problems (where b is not None), you can choose from “gv,” “gvd,” or “gvx.”. “evr” is the default value for common problems. “gvd” is used for entire sets in generalised problems while “gvx” is used for subset requested circumstances.**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).**type(int):**This parameter identifies the specific kind of problem that needs to be solved for w and v for the generalised problems.

The method

returns two values *eigh()* and

`w`

(Each of the N (1=N=M) selected eigenvalues was repeated a certain number of times)**.**

`v`

(If eigvals_only is False) of type ndarrayLet’s take compute the eigenvalue or eigenvectors of complex Hermitian or real symmetric matrix by following the below steps:

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

```
from scipy import linalg
import numpy as np
```

Create a matrix, so here we are taking the USA district’s first four-digit pin code out of 5 for a specific area as data values for the matrix, like the pin code of USA district `Aleutian East = 99553`

, `Autauga = 36003`

, `Clinton = 45390`

and `Brooks = 31720`

.

`usa_dist_pin = np.array([[9, 9, 5, 5], [3, 6, 0, 0], [4, 5, 3, 9], [3, 1, 7, 2]])`

Now pass the above matrix to the method

for computing the eigenvalues and eigenvectors using the below code.*eigh()*

```
w_, v_ = linalg.eigh(usa_dist_pin)
print("Eigenvalues",w_)
print("Eigenvectors",v_)
```

From the above output, this is how to compute the eigenvalues and eigenvector using the method

of Python Scipy.*eigh()*

Read Python Scipy Distance Matrix

## Python Scipy Linalg Eigh Tridiagonal

The method

of Python Scipy in a module *eigh_tridiagonal()*

is used for a real symmetric tridiagonal matrix, to find the eigenvalues.*scipy.linalg*

The syntax is given below.

`scipy.linalg.eigh_tridiagonal(d, e, select='a', select_range=None, check_finite=True, tol=0.0, lapack_driver='auto')`

Where parameters are:

**d(ndarray):**The array’s diagonal elements.**e(ndarray):**The array’s off-diagonal elements.**select_range(min, max):**Selection of an eigenvalue range.**select(v, i , a):**Which eigenvalues should be computed,`a`

for all eigenvalues,`i`

for eigenvalues with min <= i <= max indices and`v`

for eigenvalues within the range [(min, max].**check_finite(boolean):**Whether or not to make sure the input matrix only has finite numbers. Disabling may improve performance, but if the inputs do contain infinities or NaNs, it may cause issues (crashes, non-termination).**tol(float):**The strictest tolerance that must be met for each eigenvalue (used exclusively when’stebz’ is the lapack driver). If an eigenvalue (or cluster) falls inside this range, it is said to have converged. If = 0. (the default), the value eps*|a| is used, where |a| is the matrix a’s 1-norm and eps is the machine precision.**lapack_driver(string):**To utilise an LAPACK function, choose from “auto,” “stemr,” “stebz,” “sterf,” or “stev.” When select=’a’, it will use’stemr’; if select=’stebz,’ it will use ‘auto’ (the default). A second LAPACK call (to?STEIN) is made to get the matching eigenvectors when “stebz” is used to determine the eigenvalues and eigvals only=False. Only when eigvals only=True and select=’a’ can the keyword “sterf” be used. Only when select=’a’ may ‘stev’ be used.

The method `eigh_tridiagonal()`

returns `w`

(The eigenvalues are repeated in increasing sequence according to their multiplicity) and `v`

(The column v[:,i] contains the normalized eigenvector that corresponds to the eigenvalue w[i]).

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

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

```
from scipy import linalg
import numpy as np
```

Create diagonal and off-diagonal elements of an array using the below code.

```
diag_ele = 3*np.ones(6)
off_diag_ele = -1*np.ones(5)
```

Pass the above arrays to the method

using the below code.*eigh_tridiagonal()*

```
w_, v_ = linalg.eigh_tridiagonal(diag_ele, off_diag_ele)
print("Eigenvalues",w_)
print("Eigenvectors",v_)
```

From the output, we can see that this is how to find the eigenvalues for a real symmetric tridiagonal matrix using the method

of Python Scipy.*eigh_tridiagonal()*

Read Python Scipy Confidence Interval

## Python Scipy Linalg Eigh Eigvals Only

The parameter

is accepted by the method *eigvals_only*

of Python Scipy to compute the eigenvalues, if this parameter is set to *eigh()*

.*True*

Let’s take the same example that we have used in the above subsection “** Python Scipy Linalg Eigh”**.

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

```
from scipy import linalg
import numpy as np
```

Create a matrix, so here we are taking the USA district’s first four-digit pin code out of 5 for a specific area as data values for the matrix, like the pin code of the USA district `Aleutian East = 99553`

, `Autauga = 36003`

, `Clinton = 45390`

and `Brooks = 31720`

.

`usa_dist_pin = np.array([[9, 9, 5, 5], [3, 6, 0, 0], [4, 5, 3, 9], [3, 1, 7, 2]])`

Now pass the above matrix to the method

with parameter *eigh()*equal to

`eigvals_only`

`True`

for computing the eigenvalues only using the below code.`linalg.eigh(usa_dist_pin,eigvals_only=True)`

From the output, we can see the eigenvalues of the matrix

using the method *usa_dist_pin*

with parameter *eigh()*

equal to *eigvals_only*

of Python Scipy*True*

## Python Scipy Linalg Eigh Return

The method `eigh()`

of Python Scipy that we have learned in the above subsection, returns 3 values a ,

`w`

(Each of the N (1=N=M) selected eigenvalues was repeated a certain number of times)**and raises an error**

`v`

(If eigvals_only is False) of type ndarray**.**

`LinAlgError`

(if an error happened during the computation of the eigenvalues, the b matrix is not definite positive or both. Keep in mind that incorrect results will occur even if there is no error notified if the input matrices are not symmetric or Hermitian)Here we will see the error part because we have

learned about the * already*

and *w*

values in the above subsection *v*

.*Python Scipy Linalg Eigh*

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

```
from scipy import linalg
import numpy as np
```

Create a matrix a and b using the below code.

```
a = np.array([[9, 9, 5, 5], [3, 6, 0, 0], [4, 5, 3, 9], [3, 1, 7, 2]])
b =np.array([[-9, -9, -5, -5], [-3, -6, 0, 0], [4, -5, 3, -9], [3, -1, -7, 2]])
```

Pass the above two matrices to the method `eigh()`

using the below code.

```
w_, v_ = linalg.eigh(a,b)
print("Eigenvalues",w_)
print("Eigenvectors",v_)
```

The output shows the error

which says the matrix *LinAlgError*`b`

is not a positive matrix.

So in this tutorial, we have learned how to compute the eigenvalues or eigenvectors using the eigh() of Python Scipy. And also know about the error returned by this method with the following topics.

- Python Scipy Linalg Eigh
- Python Scipy Linalg Eigh Tridiagonal
- Python Scipy Linalg Eigh Egvals Only
- Python Scipy Linalg Eigh Return

You may like the following Python Scipy tutorials:

- Python Scipy Chi-Square Test
- Scipy Find Peaks
- Python Scipy Fcluster
- Python Scipy Linalg Eig
- Python Scipy FFT
- Python Scipy Special Module With Examples
- Python Scipy Matrix

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.