python – 具有Numpy的不正确的EigenValues /向量
作者:互联网
我试图找到以下矩阵的特征值/向量:
A = np.array([[1, 0, 0],
[0, 1, 0],
[1, 1, 0]])
使用代码:
from numpy import linalg as LA
e_vals, e_vecs = LA.eig(A)
我得到这个作为答案:
print(e_vals)
[ 0. 1. 1.]
print(e_vecs)
[[ 0. 0.70710678 0. ]
[ 0. 0. 0.70710678]
[ 1. 0.70710678 0.70710678]]
但是,我认为以下应该是答案.
[1] Real Eigenvalue = 0.00000
[1] Real Eigenvector:
0.00000
0.00000
1.00000
[2] Real Eigenvalue = 1.00000
[2] Real Eigenvector:
1.00000
0.00000
1.00000
[3] Real Eigenvalue = 1.00000
[3] Real Eigenvector:
0.00000
1.00000
1.00000
也就是说,特征值 – 特征向量问题表明以下应该成立:
# A * e_vecs = e_vals * e_vecs
print(A.dot(e_vecs))
[[ 0. 0.70710678 0. ]
[ 0. 0. 0.70710678]
[ 0. 0.70710678 0.70710678]]
print(e_vals.dot(e_vecs))
[ 1. 0.70710678 1.41421356]
解决方法:
linalg.eig返回的特征值是列向量,因此您需要迭代e_vecs的转置(因为默认情况下,2D数组上的迭代会返回行向量):
import numpy as np
import numpy.linalg as LA
A = np.array([[1, 0, 0], [0, 1, 0], [1, 1, 0]])
e_vals, e_vecs = LA.eig(A)
print(e_vals)
# [ 0. 1. 1.]
print(e_vecs)
# [[ 0. 0. 1. ]
# [ 0.70710678 0. 0.70710678]
# [ 0. 0.70710678 0.70710678]]
for val, vec in zip(e_vals, e_vecs.T):
assert np.allclose(np.dot(A, vec), val * vec)
标签:python,matrix,scipy,eigenvector,eigenvalue 来源: https://codeday.me/bug/20190624/1282393.html