本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.nonzeroRowColsProbs方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.nonzeroRowColsProbs方法的具体用法?Python SparseUtils.nonzeroRowColsProbs怎么用?Python SparseUtils.nonzeroRowColsProbs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.nonzeroRowColsProbs方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testNonzeroRowColProbs
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import nonzeroRowColsProbs [as 别名]
def testNonzeroRowColProbs(self):
m = 10
n = 5
X = scipy.sparse.rand(m, n, 0.5)
X = X.tocsc()
u, v = SparseUtils.nonzeroRowColsProbs(X)
self.assertEquals(u.sum(), 1.0)
self.assertEquals(v.sum(), 1.0)
X = numpy.diag(numpy.ones(5))
X = scipy.sparse.csc_matrix(X)
u, v = SparseUtils.nonzeroRowColsProbs(X)
nptst.assert_array_almost_equal(u, numpy.ones(5)/5)
nptst.assert_array_almost_equal(v, numpy.ones(5)/5)
self.assertEquals(u.sum(), 1.0)
self.assertEquals(v.sum(), 1.0)
示例2: next
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import nonzeroRowColsProbs [as 别名]
def next(self):
X = self.XIterator.next()
logging.debug("Learning on matrix with shape: " + str(X.shape) + " and " + str(X.nnz) + " non-zeros")
if self.iterativeSoftImpute.weighted:
#Compute row and col probabilities
up, vp = SparseUtils.nonzeroRowColsProbs(X)
nzuInds = up==0
nzvInds = vp==0
u = numpy.sqrt(1/(up + numpy.array(nzuInds, numpy.int)))
v = numpy.sqrt(1/(vp + numpy.array(nzvInds, numpy.int)))
u[nzuInds] = 0
v[nzvInds] = 0
if self.rhos != None:
self.iterativeSoftImpute.setRho(self.rhos.next())
if not scipy.sparse.isspmatrix_csc(X):
raise ValueError("X must be a csc_matrix not " + str(type(X)))
#Figure out what lambda should be
#PROPACK has problems with convergence
Y = scipy.sparse.csc_matrix(X, dtype=numpy.float)
U, s, V = ExpSU.SparseUtils.svdArpack(Y, 1, kmax=20)
del Y
#U, s, V = SparseUtils.svdPropack(X, 1, kmax=20)
maxS = s[0]
logging.debug("Largest singular value : " + str(maxS))
(n, m) = X.shape
if self.j == 0:
self.oldU = numpy.zeros((n, 1))
self.oldS = numpy.zeros(1)
self.oldV = numpy.zeros((m, 1))
else:
oldN = self.oldU.shape[0]
oldM = self.oldV.shape[0]
if self.iterativeSoftImpute.updateAlg == "initial":
if n > oldN:
self.oldU = Util.extendArray(self.oldU, (n, self.oldU.shape[1]))
elif n < oldN:
self.oldU = self.oldU[0:n, :]
if m > oldM:
self.oldV = Util.extendArray(self.oldV, (m, self.oldV.shape[1]))
elif m < oldN:
self.oldV = self.oldV[0:m, :]
elif self.iterativeSoftImpute.updateAlg == "zero":
self.oldU = numpy.zeros((n, 1))
self.oldS = numpy.zeros(1)
self.oldV = numpy.zeros((m, 1))
else:
raise ValueError("Unknown SVD update algorithm: " + self.updateAlg)
rowInds, colInds = X.nonzero()
gamma = self.iterativeSoftImpute.eps + 1
i = 0
self.iterativeSoftImpute.measures = numpy.zeros((self.iterativeSoftImpute.maxIterations, 4))
while gamma > self.iterativeSoftImpute.eps:
if i == self.iterativeSoftImpute.maxIterations:
logging.debug("Maximum number of iterations reached")
break
ZOmega = SparseUtilsCython.partialReconstructPQ((rowInds, colInds), self.oldU*self.oldS, self.oldV)
Y = X - ZOmega
#Y = Y.tocsc()
#del ZOmega
Y = csarray(Y, storagetype="row")
gc.collect()
#os.system('taskset -p 0xffffffff %d' % os.getpid())
if self.iterativeSoftImpute.svdAlg=="propack":
L = LinOperatorUtils.sparseLowRankOp(Y, self.oldU, self.oldS, self.oldV, parallel=False)
newU, newS, newV = SparseUtils.svdPropack(L, k=self.iterativeSoftImpute.k, kmax=self.iterativeSoftImpute.kmax)
elif self.iterativeSoftImpute.svdAlg=="arpack":
L = LinOperatorUtils.sparseLowRankOp(Y, self.oldU, self.oldS, self.oldV, parallel=False)
newU, newS, newV = SparseUtils.svdArpack(L, k=self.iterativeSoftImpute.k, kmax=self.iterativeSoftImpute.kmax)
elif self.iterativeSoftImpute.svdAlg=="svdUpdate":
newU, newS, newV = SVDUpdate.addSparseProjected(self.oldU, self.oldS, self.oldV, Y, self.iterativeSoftImpute.k)
elif self.iterativeSoftImpute.svdAlg=="rsvd":
L = LinOperatorUtils.sparseLowRankOp(Y, self.oldU, self.oldS, self.oldV, parallel=True)
newU, newS, newV = RandomisedSVD.svd(L, self.iterativeSoftImpute.k, p=self.iterativeSoftImpute.p, q=self.iterativeSoftImpute.q)
elif self.iterativeSoftImpute.svdAlg=="rsvdUpdate":
L = LinOperatorUtils.sparseLowRankOp(Y, self.oldU, self.oldS, self.oldV, parallel=True)
if self.j == 0:
newU, newS, newV = RandomisedSVD.svd(L, self.iterativeSoftImpute.k, p=self.iterativeSoftImpute.p, q=self.iterativeSoftImpute.q)
else:
newU, newS, newV = RandomisedSVD.svd(L, self.iterativeSoftImpute.k, p=self.iterativeSoftImpute.p, q=self.iterativeSoftImpute.qu, omega=self.oldV)
elif self.iterativeSoftImpute.svdAlg=="rsvdUpdate2":
if self.j == 0:
L = LinOperatorUtils.sparseLowRankOp(Y, self.oldU, self.oldS, self.oldV, parallel=True)
newU, newS, newV = RandomisedSVD.svd(L, self.iterativeSoftImpute.k, p=self.iterativeSoftImpute.p, q=self.iterativeSoftImpute.q)
else:
#.........这里部分代码省略.........