Numpy似乎产生了错误的特征向量 [英] Numpy seems to produce incorrect eigenvectors

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

问题描述

我想使用Numpy来计算特征值和特征向量.这是我的代码:

I want to use Numpy to calculate eigenvalues and eigenvectors. Here is my code:

import numpy as np
from numpy import linalg as LA

lapl = np.array(
       [[ 2, -1, -1,  0,  0,  0],
        [-1,  3,  0, -1,  0, -1],
        [-1,  0,  2, -1,  0,  0],
        [ 0, -1, -1,  3, -1,  0],
        [ 0,  0,  0, -1,  2, -1],
        [ 0, -1,  0,  0, -1,  2]])

w, v = LA.eigh(lapl)

print ('Eigenvalues:', np.round(w,0))
print ('Eigenvectors:', np.round(v,2))

这是结果:

Eigenvalues: [ 0.  1.  2.  3.  3.  5.]
Eigenvectors: [[ 0.41  0.5   0.41 -0.46  0.34  0.29]
               [ 0.41  0.    0.41  0.53  0.23 -0.58]
               [ 0.41  0.5  -0.41 -0.07 -0.57 -0.29]
               [ 0.41  0.   -0.41  0.53  0.23  0.58]
               [ 0.41 -0.5  -0.41 -0.46  0.34 -0.29]
               [ 0.41 -0.5   0.41 -0.07 -0.57  0.29]]

但是,当我在Wolfram Alpha中运行相同的矩阵时,得到的结果却有所不同-特征值相同,但特征向量却不同:

However, when I run the same matrix in Wolfram Alpha, I am getting a different result - eigenvalues are the same, but the eigenvectors are different:

v1 = ( 1, -2, -1,  2, -1,  1)
v2 = ( 0, -1,  1, -1,  0,  1)
v3 = ( 1, -1,  0, -1,  1,  0)
v4 = ( 1,  1, -1, -1, -1,  1)
v5 = (-1,  0, -1,  0,  1,  1)
v6 = ( 1,  1,  1,  1,  1,  1)

以下是Wolfram Alpha计算的链接: http://bit.ly/1wC9EHJ

Here is the link to the Wolfram Alpha calculations: http://bit.ly/1wC9EHJ

为什么我得到不同的结果?我应该在Python中做什么才能获得与Alpha产生的结果相同的结果?

Why am I getting a different result? What should I do in Python to get the same result as produced by Alpha?

非常感谢您的帮助!

推荐答案

由于多种原因,结果有所不同:

The results are different due to multiple reasons:

  1. 您可能已经注意到,在将Wolfram结果v1v6打印为v包含特征向量为水平堆叠的. >行.

  1. You probably noticed, that the numpy matrix v contains the eigenvectors as horizontally stacked columns, while you're printing the Wolfram results v1 to v6 as rows.

特征向量的比例(或长度)未定义.因此,通常将其缩放为长度1.Wolfram结果的缩放比例不同,我猜这会引起一些混乱.

The scale (or length) of an eigenvector is undefined. So it's usually scaled to length 1. The Wolfram result is scaled differently, which causes some confusion, I guess.

请注意,通过缩放矢量,甚至符号也可以更改.这就是正负元素可能会翻转的原因.

Note that by scaling the vectors, even the sign can change. That's why positive and negative elements might get flipped.

最后但并非最不重要的一点:特征向量的顺序是未定义的,只要它们的顺序与其对应的特征值相同即可.因此,您还需要查看Wolfram的特征值,并可能相应地将v1重新排列为v6. (按特征值大小排序是一种常见的约定.Wolfram似乎按降序排序,而numpy则按升序排序.)

And last but not least: The order of eigenvectors is undefined, as long as they are in the same order like their corresponding eigenvalues. So you'd need to look at Wolfram's eigenvalues as well and possibly reorder v1 to v6 accordingly. (It's a common convention to sort by size of the eigenvalues. Wolfram seems to sort in descending order, while numpy sorts in ascending order.)

如果矩阵具有非唯一特征值,则只要它们跨越相应的子空间,相应的特征向量就可以任意旋转.但是,在您的示例中似乎并非如此.

In case of matrices with non-unique eigenvalues, the corresponding eigenvectors can arbitrarily rotate, as long as they span the corresponding subspace. However, this doesn't seem to be the case in your example.

考虑到前4个问题,结果实际上非常接近.奇异性无关紧要,因为只有一个零本征值,因此相应的本征向量是唯一的(不超过符号和长度).

Considering the first 4 issues the results are actually pretty close. The singularity shouldn't matter, since there is only one zero eigenvalue, thus the corresponding eigenvector is unique (up to sign and length).

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

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