当前位置: 首页>>代码示例>>Python>>正文


Python SparseUtils.centerRows方法代码示例

本文整理汇总了Python中sandbox.util.SparseUtils.SparseUtils.centerRows方法的典型用法代码示例。如果您正苦于以下问题:Python SparseUtils.centerRows方法的具体用法?Python SparseUtils.centerRows怎么用?Python SparseUtils.centerRows使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sandbox.util.SparseUtils.SparseUtils的用法示例。


在下文中一共展示了SparseUtils.centerRows方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testUncentre

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerRows [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)
开发者ID:charanpald,项目名称:sandbox,代码行数:37,代码来源:SparseUtilsTest.py

示例2: testCentreRows

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerRows [as 别名]
    def testCentreRows(self): 
        shape = (50, 10)
        r = 5 
        k = 100 

        X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)   
        rowInds, colInds = X.nonzero()
        
        for i in range(rowInds.shape[0]): 
            self.assertEquals(X[rowInds[i], colInds[i]], numpy.array(X[X.nonzero()]).ravel()[i])
        
        mu2 = numpy.array(X.sum(1)).ravel()
        numNnz = numpy.zeros(X.shape[0])
        
        for i in range(X.shape[0]): 
            for j in range(X.shape[1]):     
                if X[i,j]!=0:                 
                    numNnz[i] += 1
                    
        mu2 /= numNnz 
        mu2[numNnz==0] = 0
        
        X, mu = SparseUtils.centerRows(X)      
        nptst.assert_array_almost_equal(numpy.array(X.mean(1)).ravel(), numpy.zeros(X.shape[0]))
        nptst.assert_array_almost_equal(mu, mu2)
开发者ID:charanpald,项目名称:sandbox,代码行数:27,代码来源:SparseUtilsTest.py

示例3: centerMatrix

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerRows [as 别名]
 def centerMatrix(self, X):
     """
     Center a test matrix given we have already centered the training one. 
     """
     logging.debug("Centering test matrix of size: " + str(X.shape))
     tempRowInds, tempColInds = X.nonzero()    
     X, muRows = SparseUtils.centerRows(X, self.muRows)
     X.eliminate_zeros()
     X.prune() 
     
     return X     
开发者ID:charanpald,项目名称:wallhack,代码行数:13,代码来源:CenterMatrixIterator.py

示例4: next

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerRows [as 别名]
 def next(self): 
     X = next(self.iterator)
     logging.debug("Centering train matrix of size: " + str(X.shape) + " and dtype " + str(X.dtype))
     tempRowInds, tempColInds = X.nonzero()    
     X, self.muRows = SparseUtils.centerRows(X)
     X.eliminate_zeros()
     X.prune() 
     gc.collect()
     
     self.i += 1 
     
     return X 
开发者ID:charanpald,项目名称:wallhack,代码行数:14,代码来源:CenterMatrixIterator.py

示例5: testCentreRows2

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import centerRows [as 别名]
    def testCentreRows2(self): 
        shape = (50, 10)
        r = 5 
        k = 100 
        
        #Test if centering rows changes the RMSE
        X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)   
 
        Y = X.copy() 
        Y.data = numpy.random.rand(X.nnz)
        
        error = ((X.data - Y.data)**2).sum()
        
        X, mu = SparseUtils.centerRows(X)
        Y, mu = SparseUtils.centerRows(Y, mu)
        
        error2 = ((X.data - Y.data)**2).sum()
        self.assertAlmostEquals(error, error2)
        
        error3 = numpy.linalg.norm(X.todense()- Y.todense())**2
        self.assertAlmostEquals(error2, error3)        
开发者ID:charanpald,项目名称:sandbox,代码行数:23,代码来源:SparseUtilsTest.py


注:本文中的sandbox.util.SparseUtils.SparseUtils.centerRows方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。