Joa*_*nge 5 python numpy matrix linear-algebra svd
我传递了100万个3d点,numpy.linalg.svd但内存很快耗尽.有没有办法将此操作分解为更小的块?
我不知道它在做什么但是我只应该传递代表3x3,4x4矩阵的数组?因为我已经看到它在线使用它们传递具有任意数量元素的数组.
如果您的情况有 MxN,则 1000000x3 矩阵numpy.linalg.svd不需要 M==N。事实上,这正是 SVD 可以用来计算诸如秩和伪逆之类的东西的地方。linalg.inv 等方法需要方阵(和满秩)才能获得定义的结果。
@Saullo Castro 说得对。full_matrices=False 可以将棘手变为易于处理,因为 U 矩阵不再是 1Mx1M 元素,而是 1Mx3,这是一个巨大的节省。我不确定 numpy 使用哪种简化的 SVD 算法(我认为它可能是 Compact SVD,或者 Thin):维基百科上有 3 个广泛使用的简单描述:http : //en.wikipedia.org/wiki/Singular_value_decomposition简化的 SVD 部分。它们都以将完整 U 矩阵的计算减少为简化形式为中心,这对于某些甚至许多问题来说已经足够了。当 numberObservations>>numberFeatures 时节省最多
关于是否得到相同的结果。简短的答案可能是肯定的,具体取决于您将如何处理 SVD 结果。例如,您将得到与原始矩阵相同的简化形式的矩阵(到浮点容差级别),如下面的代码所示。请注意,在最上面的情况下,U 的大小为 numberObservations x numberObservations,而在 full_matrices=False 中,U 的大小为 numberObservations x numberFeatures
该代码改编自 numpy.linalg.svd 文档,允许用户尝试任意行/列、选择奇异值。
人们总是可以将 U 矩阵的大小减小到 M x min(M,N)。根据数据的结构和存在的噪声量,可能会进一步减少。仅仅因为 numpy.isclose 为 false 并不意味着计算出的 SV 对所有上下文都是不利的。您可以使用mostSignificantSingularValues变量进行试验,该变量从完整的 SVD 中获取顶部 SV。
numberObservations = 900
numberFeatures = 600
mostSignificantSingularValues = 600
a = np.random.randn( numberObservations, numberFeatures) + 1j*np.random.randn(numberObservations, numberFeatures)
#Reconstruction based on full SVD:
U, s, V = np.linalg.svd(a, full_matrices=True)
print(U.shape, V.shape, s.shape)
S = np.zeros((numberObservations, numberFeatures), dtype=complex)
S[:mostSignificantSingularValues, :mostSignificantSingularValues] = np.diag(s[:mostSignificantSingularValues])
print(np.allclose(a, np.dot(U, np.dot(S, V))))
d1 = a - np.dot(U, np.dot(S, V))#
#True
#Reconstruction based on reduced SVD:
U, s, V = np.linalg.svd(a, full_matrices=False)
print(U.shape, V.shape, s.shape)
S = np.diag(s)
print(np.allclose(a, np.dot(U, np.dot(S, V))))
d2 = a - np.dot(U, np.dot(S, V))#
Run Code Online (Sandbox Code Playgroud)