考虑奇异值分解M=USV*。然后M*M的特征值分解得到M*M=V(S*S)V*=VS*U*USV*。我希望通过展示eigh
函数返回的特征向量与svd
函数返回的特征向量相同,来验证这个等式:
import numpy as np
np.random.seed(42)
# create mean centered data
A=np.random.randn(50,20)
M= A-np.array(A.mean(0),ndmin=2)
# svd
U1,S1,V1=np.linalg.svd(M)
S1=np.square(S1)
V1=V1.T
# eig
S2,V2=np.linalg.eigh(np.dot(M.T,M))
indx=np.argsort(S2)[::-1]
S2=S2[indx]
V2=V2[:,indx]
# both Vs are in orthonormal form
assert np.all(np.isclose(np.linalg.norm(V1,axis=1), np.ones(V1.shape[0])))
assert np.all(np.isclose(np.linalg.norm(V1,axis=0), np.ones(V1.shape[1])))
assert np.all(np.isclose(np.linalg.norm(V2,axis=1), np.ones(V2.shape[0])))
assert np.all(np.isclose(np.linalg.norm(V2,axis=0), np.ones(V2.shape[1])))
assert np.all(np.isclose(S1,S2))
assert np.all(np.isclose(V1,V2))
最后一个断言失败。为什么?在
用小数字来调试你的问题。在
从
A=np.random.randn(3,2)
开始,而不是更大的矩阵(50,20)
在我的随机案例中,我发现
对于
^{pr2}$v2
:它们只对符号不同,很明显,即使规范化为有单位模,向量对于符号也可能不同。在
现在如果你试试这个把戏
对于你最初的大矩阵,它失败了。。。再说一遍,这没关系。结果是有些向量被
-1
相乘,有些则没有检查矢量之间是否相等的正确方法是:
实际上,要想了解它是如何工作的,你可以打印这个数量:
这实际上是},取决于载体:
+1
或{编辑:这在大多数情况下都会发生,尤其是从随机矩阵开始。但是请注意,如果一个或多个特征值的本征空间的维数大于
1
,正如@Sven Marnach在下面的评论中指出的那样,这个测试可能会失败:相关问题 更多 >
编程相关推荐