本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.svdSparseLowRank方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.svdSparseLowRank方法的具体用法?Python SparseUtils.svdSparseLowRank怎么用?Python SparseUtils.svdSparseLowRank使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.svdSparseLowRank方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSvdSparseLowRank
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import svdSparseLowRank [as 别名]
def testSvdSparseLowRank(self):
numRuns = 10
n = 10
density = 0.2
for i in range(numRuns):
A = scipy.sparse.rand(n, n, density)
A = A.tocsc()
B = numpy.random.rand(n, n)
U, s, V = numpy.linalg.svd(B)
V = V.T
r = numpy.random.randint(2, n)
U = U[:, 0:r]
s = s[0:r]
V = V[:, 0:r]
#B is low rank
B = (U*s).dot(V.T)
k = numpy.random.randint(1, r)
U2, s2, V2 = SparseUtils.svdSparseLowRank(A, U, s, V)
U2 = U2[:, 0:k]
s2 = s2[0:k]
V2 = V2[:, 0:k]
nptst.assert_array_almost_equal(U2.T.dot(U2), numpy.eye(U2.shape[1]))
nptst.assert_array_almost_equal(V2.T.dot(V2), numpy.eye(V2.shape[1]))
#self.assertEquals(s2.shape[0], r)
A2 = (U2*s2).dot(V2.T)
#Compute real SVD
C = numpy.array(A.todense()) + B
U3, s3, V3 = numpy.linalg.svd(C)
V3 = V3.T
U3 = U3[:, 0:k]
s3 = s3[0:k]
V3 = V3[:, 0:k]
A3 = (U3*s3).dot(V3.T)
#self.assertAlmostEquals(numpy.linalg.norm(A2 - A3), 0)
nptst.assert_array_almost_equal(s2, s3, 3)
nptst.assert_array_almost_equal(numpy.abs(U2), numpy.abs(U3), 3)
nptst.assert_array_almost_equal(numpy.abs(V2), numpy.abs(V3), 3)