Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort eigenvalues and associated eigenvectors after using numpy.linalg.eig in python

I'm using numpy.linalg.eig to obtain a list of eigenvalues and eigenvectors:

A = someMatrixArray from numpy.linalg import eig as eigenValuesAndVectors  solution = eigenValuesAndVectors(A)  eigenValues = solution[0] eigenVectors = solution[1] 

I would like to sort my eigenvalues (e.g. from lowest to highest), in a way I know what is the associated eigenvector after the sorting.

I'm not finding any way of doing that with python functions. Is there any simple way or do I have to code my sort version?

like image 694
Jorge Leitao Avatar asked Nov 11 '11 10:11

Jorge Leitao


People also ask

How do you sort an eigenvector in Python?

Use numpy. argsort. It returns the indices one would use to sort the array. If the eigenvalues are complex, the sort order is lexicographic (that is, complex numbers are sorted according to their real part first, with ties broken by their imaginary part).

What does the Numpy Linalg Eig () function return?

The numpy. linalg. eig function returns a tuple consisting of a vector and an array.

How does Numpy Linalg EIG work?

In NumPy we can compute the eigenvalues and right eigenvectors of a given square array with the help of numpy. linalg. eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array.


1 Answers

Use numpy.argsort. It returns the indices one would use to sort the array.

import numpy as np import numpy.linalg as linalg  A = np.random.random((3,3)) eigenValues, eigenVectors = linalg.eig(A)  idx = eigenValues.argsort()[::-1]    eigenValues = eigenValues[idx] eigenVectors = eigenVectors[:,idx] 

If the eigenvalues are complex, the sort order is lexicographic (that is, complex numbers are sorted according to their real part first, with ties broken by their imaginary part).

like image 133
unutbu Avatar answered Sep 18 '22 22:09

unutbu