【发布时间】:2015-03-03 03:05:55
【问题描述】:
考虑奇异值分解 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))
最后一个断言失败。为什么?
【问题讨论】:
-
你可以给所有的对角元素加上一个正数,即使 M2=M+a*I,其中 a 大到足以使 M2 为半正数。那么 SVD 和 eigh 应该更一致。
标签: python numpy svd eigenvector