Python 手册 SVD 仅适用于某些矩阵 - 如何稳定它?

问题描述 投票:0回答:1

我使用以下代码使用 numpy 进行手动单值分解。根据我选择的阵列,它有时效果很好,我可以验证 svd,有时它不能立即起作用,需要进行符号翻转。

代码

import numpy as np

array = np.array([[-3,3,6],
                  [3,8,7]])  # here you can choose any matrix values. Some matrices work, some don't.

# left singular vectors
AAT = np.matmul(array, array.T)
eAAT_values, eAAT_vectors = np.linalg.eig(AAT)
idx = eAAT_values.argsort()[::-1]      #sorting: largest singular values first
eAAT_values = eAAT_values[idx]
eAAT_vectors = eAAT_vectors[:,idx]
SL = eAAT_vectors
# Some arrays require an additional line:
# SL[:,0] = -SL[:,0]


# right singular vectors
ATA = np.matmul(array.T,array)
eATA_values, eATA_vectors = np.linalg.eig(ATA)
idx = eATA_values.argsort()[::-1]     #sorting: largest singular values first
eATA_values = eATA_values[idx]
eATA_vectors = eATA_vectors[:,idx]
SR = eATA_vectors.T


# singular values
S0 = np.zeros(np.shape(array))
np.fill_diagonal(S0, np.sqrt(eATA_values), wrap=True)

# verifying. Expected proof == array
Proof = np.matmul(SL,np.matmul(S0,SR))     # works out with some arrays with others it does not

# Python linalg.svd works consistently
U, S, Vt = np.linalg.svd(array) 
Sm = np.zeros(np.shape(array))
np.fill_diagonal(Sm, S, wrap=True)
proof2 = np.matmul(U, np.matmul(Sm,Vt))    # always works out

上面代码中的数组运行良好。这里提供了相反的例子,其中 svd 在没有额外的符号翻转的情况下无法工作。

array_not_working1 = np.array([[4,5,9],
                               [3,2,6]])

array_not_working2 = np.array([[3,-1,4],
                               [1,5,9]])

如何稳定结果或确定何时需要对

eAAT_vectors
的第一个特征向量进行符号翻转?

另一个没有额外符号翻转的工作数组:

array = np.array([[7,-12,2], [-4,3,1]])
.

python-3.x numpy linear-algebra svd eigenvector
1个回答
0
投票

这看起来与我实现了SVD相同,但有时无法恢复原始矩阵

你可以看看我的回答和下面的评论。

© www.soinside.com 2019 - 2024. All rights reserved.