这个问题困扰我好久了,每次写都不知道用特征值分解好还是用奇异值分解好
计算特征向量不匹配
参考https://blog.csdn.net/weixin_43383558/article/details/83145867
https://zhuanlan.zhihu.com/p/43578482
https://codeday.me/bug/20181026/336042.html
所以SVD的V和特征值分解的P应该相同
但是matlab和svd中是不一样的,具体原因在于软件求解问题,那么如何才能一样呢?
eigh 奇迹般竟然相同了
但是eig就不相同了
哈哈哈以后用eigh吧
eigvalues
Out[88]: array([ 0.28251688, 0.59034175, 1.14891126, 1.45970816, 10.79185466])
index = np.argsort(eigvalues)[::-1]
index
Out[90]: array([4, 3, 2, 1, 0], dtype=int64)
eigvalues = eigvalues[index]
eigvalues
Out[92]: array([10.79185466, 1.45970816, 1.14891126, 0.59034175, 0.28251688])
eigvectors = eigvectors[:,index]
eigvectors
Out[94]:
array([[-0.60480295, -0.51573258, 0.24030538, 0.40956589, -0.37781265],
[-0.45461252, 0.42466685, -0.67842382, -0.08945472, -0.38042673],
[-0.3539136 , -0.24283785, 0.17407516, -0.88488594, 0.04949405],
[-0.32673754, -0.25846818, -0.4100625 , 0.16537152, 0.79431592],
[-0.44218615, 0.65414215, 0.53248571, 0.11782517, 0.28132838]])
u,sigma,vt = np.linalg.svd(X)
V = vt.T
V
Out[97]:
array([[-0.60480295, -0.51573258, 0.24030538, -0.40956589, 0.37781265],
[-0.45461252, 0.42466685, -0.67842382, 0.08945472, 0.38042673],
[-0.3539136 , -0.24283785, 0.17407516, 0.88488594, -0.04949405],
[-0.32673754, -0.25846818, -0.4100625 , -0.16537152, -0.79431592],
[-0.44218615, 0.65414215, 0.53248571, -0.11782517, -0.28132838]])
下面使用PCA进行验证
妥了,困扰我多年的问题解决了
================================================================================================
最后提醒大家千万不要用Python包一个一个去求ATA以及AAT的特征值、特征向量,你会发现你求得的UΣVT与原矩阵不相等!!!
本文只是帮大家试下错,Python一定要用np.linalg.svd(A)求解SVD,千万不要单独求解,切记!!