奇异值分解 (SVD) 输出一维奇异值数组,而不是二维对角矩阵 [Python]

Singular Value Decomposition (SVD) outputs a 1-D singular value array, instead of 2-D diagonal matrix [Python]

我在发布类似主题的问题时,遇到了另一个更重要的问题。

当我将 SVD 应用于矩阵 'A'(下面的代码)时,我得到的输出是预期的二维特征向量矩阵('U' 和 'V')和意外的 1 -D 奇异值数组'S'。

U,S,V=np.linalg.svd(A)

对于上下文:它出乎意料的原因是奇异值分解应该产生三个矩阵的乘积。中间矩阵(在本例中为一维数组)应该是对角矩阵,以降序排列非负奇异值。

为什么Python'transform'矩阵变成数组?有解决办法吗?

谢谢!

这在 docs 中说得很清楚,你会看到:

s : (…, K) array: Vector(s) with the singular values, within each vector sorted in descending order. The first a.ndim - 2 dimensions have the same size as those of the input a.

所以基本上 S 只是你提到的矩阵的对角线,即奇异值。你可以用它构造一个对角矩阵:

np.diag(S)

使用np.diag (https://docs.scipy.org/doc/numpy/reference/generated/numpy.diag.html)

>>> np.diag([0, 4, 8])
array([[0, 0, 0],
       [0, 4, 0],
       [0, 0, 8]])