计算SVD分量之和

时间:2019-05-23 16:21:41

标签: python numpy svd

我们现在A_(m x n) = U_(m x k) * S_(k x k) * V_(k x n)^T = u_(1) * s_1 * v_(1) + u_(2) * s_2 * v_(2) + ...,,其中u_(i)v(i)UV的第i列,s_i-的第i个对角元素S.我试图在不使用循环的情况下获得数组a,其中a[i] = u_(1) * s_1 * v_(1) + u_(2) * s_2 * v_(2) + ... + u_(i) * s_i * v_(i).接下来应该做什么?

from numpy.linalg import svd

U, S, VT = svd(A, full_matrices=False)
A_1 = U[:, 0:1] * S[0] @ VT[0:1]

1 个答案:

答案 0 :(得分:1)

一种简单的方法是einsum

A_ = np.einsum("ij,j,jk->jik",U[:,:len(VT)],S,VT[:len(U)]).cumsum(0)

如果您明智地用svd调用了full_matrices=False,则简化为

A_ = np.einsum("ij,j,jk->jik",U,S,VT).cumsum(0)

您的A_1对应于A_[0]等,A_[-1]是(向上舍入)原始数组A

P.S .:基本上没有einsum

A_ = ((U*S).T[:,:,None]*VT[:,None,:]).cumsum(0)