本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.norm方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.norm方法的具体用法?Python SparseUtils.norm怎么用?Python SparseUtils.norm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.norm方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMatrixApprox
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import norm [as 别名]
def testMatrixApprox(self):
tol = 10**-6
A = numpy.random.rand(10, 10)
A = A.dot(A.T)
n = 5
inds = numpy.sort(numpy.random.permutation(A.shape[0])[0:n])
AHat = Nystrom.matrixApprox(A, inds)
n = 10
AHat2 = Nystrom.matrixApprox(A, n)
self.assertTrue(numpy.linalg.norm(A - AHat2) < numpy.linalg.norm(A - AHat))
self.assertTrue(numpy.linalg.norm(A - AHat2) < tol)
#Test on a sparse matrix
As = scipy.sparse.csr_matrix(A)
n = 5
inds = numpy.sort(numpy.random.permutation(A.shape[0])[0:n])
AHat = Nystrom.matrixApprox(As, inds)
n = 10
AHat2 = Nystrom.matrixApprox(As, n)
self.assertTrue(SparseUtils.norm(As - AHat2) < SparseUtils.norm(As - AHat))
self.assertTrue(SparseUtils.norm(As - AHat2) < tol)
#Compare dense and sparse solutions
for n in range(1, 9):
inds = numpy.sort(numpy.random.permutation(A.shape[0])[0:n])
AHats = Nystrom.matrixApprox(As, inds)
AHat = Nystrom.matrixApprox(A, inds)
self.assertTrue(numpy.linalg.norm(AHat - numpy.array(AHats.todense())) < tol)
示例2: learnModel2
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import norm [as 别名]
def learnModel2(self, X):
"""
Learn the matrix completion using a sparse matrix X. This is the simple
version of the soft impute algorithm in which we store the entire
matrices, newZ and oldZ.
"""
#if not scipy.sparse.isspmatrix_lil(X):
# raise ValueError("Input matrix must be lil_matrix")
oldZ = scipy.sparse.lil_matrix(X.shape)
omega = X.nonzero()
tol = 10**-6
ZList = []
for rho in self.rhos:
gamma = self.eps + 1
i = 0
while gamma > self.eps:
Y = oldZ.copy()
Y[omega] = 0
Y = X + Y
Y = Y.tocsc()
U, s, V = ExpSU.SparseUtils.svdSoft(Y, rho)
#Get an "invalid value encountered in sqrt" warning sometimes
newZ = scipy.sparse.lil_matrix((U*s).dot(V.T))
oldZ = oldZ.tocsr()
normOldZ = SparseUtils.norm(oldZ)**2
normNewZmOldZ = SparseUtils.norm(newZ - oldZ)**2
#We can get newZ == oldZ in which case we break
if normNewZmOldZ < tol:
gamma = 0
elif abs(normOldZ) < tol:
gamma = self.eps + 1
else:
gamma = normNewZmOldZ/normOldZ
oldZ = newZ.copy()
logging.debug("Iteration " + str(i) + " gamma="+str(gamma))
i += 1
logging.debug("Number of iterations for lambda="+str(rho) + ": " + str(i))
ZList.append(newZ)
if self.rhos.shape[0] != 1:
return ZList
else:
return ZList[0]
示例3: testNorm
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import norm [as 别名]
def testNorm(self):
numRows = 10
numCols = 10
for k in range(10):
A = scipy.sparse.rand(numRows, numCols, 0.1, "csr")
norm = SparseUtils.norm(A)
norm2 = 0
for i in range(numRows):
for j in range(numCols):
norm2 += A[i, j]**2
norm2 = numpy.sqrt(norm2)
norm3 = numpy.linalg.norm(numpy.array(A.todense()))
self.assertAlmostEquals(norm, norm2)
self.assertAlmostEquals(norm, norm3)