NumPy的错误特征值/向量 [英] Incorrect EigenValues/Vectors with Numpy

查看:115
本文介绍了NumPy的错误特征值/向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找以下矩阵的特征值/向量:

I am trying to find the eigenvalues/vectors for the following matrix:

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]]

但是,我相信以下应该是答案.

However, I believe the following should be the answer.

[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

也就是说,特征值-特征向量问题表明以下内容应成立:

That is, the eigenvalue-eigenvector problem says that the follow should hold true:

# 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 transpose (自迭代以来)在2D数组上返回默认的行向量):

The eigenvalues returned by linalg.eig are columns vectors, so you need to iterate over the transpose of e_vecs (since iteration over a 2D array returns row vectors by default):

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)

这篇关于NumPy的错误特征值/向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆