我使用 numpy.linalg.eig 来获取特征值和特征向量的列表:
A = someMatrixArray
from numpy.linalg import eig as eigenValuesAndVectors
solution = eigenValuesAndVectors(A)
eigenValues = solution[0]
eigenVectors = solution[1]
我想对我的特征值进行排序(例如从最低到最高),以某种方式我知道排序后相关的特征向量是什么。
我没有找到任何使用 python 函数来做到这一点的方法。有什么简单的方法或者我必须编写我的排序版本吗?
Use numpy.argsort http://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort。它返回用于对数组进行排序的索引。
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]
如果特征值是复数,排序顺序是字典顺序 https://docs.scipy.org/doc/numpy/reference/generated/numpy.sort.html(也就是说,复数首先根据实部排序,然后由虚部打破联系)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)