按顺序获取 NumPy 数据列的奇异值
Getting Singular Values of NumPy Data Columns in Order
我想计算矩阵的奇异值分解,奇异值的顺序很重要。默认情况下,似乎numpy.linalg.svd
(和scipy.linalg.svd
)对奇异值进行排序,这使我无法分辨每个奇异值对应于哪一列。
示例:
import numpy as np
X = np.array([[-74, 80, 18, -56, -112],
[14, -69, 21, 52, 104],
[66, -72, -5, 764, 1528],
[-12, 66, -30, 4096, 8192],
[3, 8, -7, -13276, -26552],
[4, -12, 4, 8421, 16842]])
U, D, V = np.linalg.svd(X)
print(D)
Returns:
array([3.63684045e+04, 1.70701331e+02, 6.05331879e+01, 7.60190176e+00,
1.17158094e-12])
当我需要时:
array([1.70701331e+02, 6.05331879e+01, 7.60190176e+00, 3.63684045e+04,
1.17158094e-12])
有没有办法获取奇异值 (D) 以使其未排序?关系 X = UDV^T 也必须保留。
编辑: 这里需要一些上下文来阐明我的误解。我试图在 this paper.
中重现第 2.3 节,方差分解方法
当你说:
By default, it seems numpy.linalg.svd (and scipy.linalg.svd) sort the singular values, which makes it impossible for me to tell which column corresponds to each singular value.
我认为你犯了一个错误,"Singular value decomposition" 中的奇异值没有唯一的顺序,重要的是 U、D 和 V 的列向量的顺序是这样的:
U * D * V == X
因此,按照惯例,它们是按降序排列的,但显然酉基U和共轭转置V的垂直向量也按上述公式成立的顺序设置。
如果你想要证明,从 U、D 和 V 计算 X,你必须做:
from scipy import linalg
#decompose
U, D, V = np.linalg.svd(X)
# get dim of X
M,N = X.shape
# Construct sigma matrix in SVD (it simply adds null row vectors to match the dim of X
Sig = linalg.diagsvd(D,M,N)
# Now you can get X back:
assert np.sum(np.dot(U, np.dot(Sig, V)) - X) < 0.00001
我想计算矩阵的奇异值分解,奇异值的顺序很重要。默认情况下,似乎numpy.linalg.svd
(和scipy.linalg.svd
)对奇异值进行排序,这使我无法分辨每个奇异值对应于哪一列。
示例:
import numpy as np
X = np.array([[-74, 80, 18, -56, -112],
[14, -69, 21, 52, 104],
[66, -72, -5, 764, 1528],
[-12, 66, -30, 4096, 8192],
[3, 8, -7, -13276, -26552],
[4, -12, 4, 8421, 16842]])
U, D, V = np.linalg.svd(X)
print(D)
Returns:
array([3.63684045e+04, 1.70701331e+02, 6.05331879e+01, 7.60190176e+00,
1.17158094e-12])
当我需要时:
array([1.70701331e+02, 6.05331879e+01, 7.60190176e+00, 3.63684045e+04,
1.17158094e-12])
有没有办法获取奇异值 (D) 以使其未排序?关系 X = UDV^T 也必须保留。
编辑: 这里需要一些上下文来阐明我的误解。我试图在 this paper.
中重现第 2.3 节,方差分解方法当你说:
By default, it seems numpy.linalg.svd (and scipy.linalg.svd) sort the singular values, which makes it impossible for me to tell which column corresponds to each singular value.
我认为你犯了一个错误,"Singular value decomposition" 中的奇异值没有唯一的顺序,重要的是 U、D 和 V 的列向量的顺序是这样的:
U * D * V == X
因此,按照惯例,它们是按降序排列的,但显然酉基U和共轭转置V的垂直向量也按上述公式成立的顺序设置。
如果你想要证明,从 U、D 和 V 计算 X,你必须做:
from scipy import linalg
#decompose
U, D, V = np.linalg.svd(X)
# get dim of X
M,N = X.shape
# Construct sigma matrix in SVD (it simply adds null row vectors to match the dim of X
Sig = linalg.diagsvd(D,M,N)
# Now you can get X back:
assert np.sum(np.dot(U, np.dot(Sig, V)) - X) < 0.00001