本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.centerCols方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.centerCols方法的具体用法?Python SparseUtils.centerCols怎么用?Python SparseUtils.centerCols使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sandbox.util.SparseUtils.SparseUtils
的用法示例。
在下文中一共展示了SparseUtils.centerCols方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testUncentre
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerCols [as 别名]
def testUncentre(self):
shape = (50, 10)
r = 5
k = 100
X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)
rowInds, colInds = X.nonzero()
Y = X.copy()
inds = X.nonzero()
X, mu1 = SparseUtils.centerRows(X)
X, mu2 = SparseUtils.centerCols(X, inds=inds)
cX = X.copy()
Y2 = SparseUtils.uncenter(X, mu1, mu2)
nptst.assert_array_almost_equal(Y.todense(), Y2.todense(), 3)
#We try softImpute on a centered matrix and check if the results are the same
lmbdas = numpy.array([0.1])
softImpute = SoftImpute(lmbdas)
Z = softImpute.learnModel(cX, fullMatrices=False)
Z = softImpute.predict([Z], cX.nonzero())[0]
error1 = MCEvaluator.rootMeanSqError(cX, Z)
X = SparseUtils.uncenter(cX, mu1, mu2)
Z2 = SparseUtils.uncenter(Z, mu1, mu2)
error2 = MCEvaluator.rootMeanSqError(X, Z2)
self.assertAlmostEquals(error1, error2)
示例2: testCentreCols
# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerCols [as 别名]
def testCentreCols(self):
shape = (50, 10)
r = 5
k = 100
X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)
rowInds, colInds = X.nonzero()
mu2 = numpy.array(X.sum(0)).ravel()
numNnz = numpy.zeros(X.shape[1])
for i in range(X.shape[0]):
for j in range(X.shape[1]):
if X[i,j]!=0:
numNnz[j] += 1
mu2 /= numNnz
mu2[numNnz==0] = 0
X, mu = SparseUtils.centerCols(X)
nptst.assert_array_almost_equal(numpy.array(X.mean(0)).ravel(), numpy.zeros(X.shape[1]))
nptst.assert_array_almost_equal(mu, mu2)