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


Python SparseUtils.generateSparseLowRank方法代码示例

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


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

示例1: testUncentre

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [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 generateSparseLowRank [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: setUp

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [as 别名]
 def setUp(self): 
     numpy.set_printoptions(suppress=True, precision=3, linewidth=150)
     numpy.random.seed(21)
     shape = (20, 10)
     r = 5 
     k = 100         
     
     #Create an iterator 
     matrixList = [] 
     matrixList.append(SparseUtils.generateSparseLowRank(shape, r, k))
     matrixList.append(SparseUtils.generateSparseLowRank(shape, r, k))
     matrixList.append(SparseUtils.generateSparseLowRank(shape, r, k))
     
     self.matrixList = matrixList
     self.testMatrixList = []
     
     for X in matrixList: 
         self.testMatrixList.append(X.copy())
开发者ID:charanpald,项目名称:wallhack,代码行数:20,代码来源:CenterMatrixIteratorTest.py

示例4: profileGetOmegaList

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [as 别名]
    def profileGetOmegaList(self):
        shape = (20000, 15000)
        r = 50
        k = 1000000

        X = SparseUtils.generateSparseLowRank(shape, r, k)
        import sppy

        X = sppy.csarray(X)

        ProfileUtils.profile("SparseUtils.getOmegaList(X)", globals(), locals())
开发者ID:kentwang,项目名称:sandbox,代码行数:13,代码来源:SparseUtilsProfile.py

示例5: profileSubmatrix

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [as 别名]
    def profileSubmatrix(self):
        shape = (100000, 15000)
        r = 50
        k = 5000000

        X = SparseUtils.generateSparseLowRank(shape, r, k)
        print(X.nnz, type(X))

        inds = numpy.random.permutation(X.nnz)[0:1000000]

        ProfileUtils.profile("SparseUtils.submatrix(X, inds)", globals(), locals())
开发者ID:kentwang,项目名称:sandbox,代码行数:13,代码来源:SparseUtilsProfile.py

示例6: __init__

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [as 别名]
    def __init__(self):
        numpy.random.seed(21)

        # Create a low rank matrix
        n = 100000
        m = 100000
        self.r = 50
        k = 5 * 10 ** 6
        # k = 10**5

        self.X = SparseUtils.generateSparseLowRank((n, m), self.r, k)
        print(self.X.nnz)
开发者ID:kentwang,项目名称:sandbox,代码行数:14,代码来源:SoftImputeProfile.py

示例7: testGenerateSparseLowRank

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

        X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)         
        
        self.assertEquals(U.shape, (shape[0],r))
        self.assertEquals(V.shape, (shape[1], r))
        self.assertTrue(X.nnz <= k)
        
        Y = (U*s).dot(V.T)
        inds = X.nonzero()
        
        for i in range(inds[0].shape[0]):
            self.assertAlmostEquals(X[inds[0][i], inds[1][i]], Y[inds[0][i], inds[1][i]])
开发者ID:charanpald,项目名称:sandbox,代码行数:18,代码来源:SparseUtilsTest.py

示例8: testSvdArpack

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

        X, U, s, V = SparseUtils.generateSparseLowRank(shape, r, k, verbose=True)                
        
        k2 = 10 
        U, s, V = SparseUtils.svdArpack(X, k2)

        U2, s2, V2 = numpy.linalg.svd(X.todense())
        V2 = V2.T

        nptst.assert_array_almost_equal(s, s2[0:k2])
        nptst.assert_array_almost_equal(numpy.abs(U), numpy.abs(U2[:, 0:k2]), 3)
        nptst.assert_array_almost_equal(numpy.abs(V), numpy.abs(V2[:, 0:k2]), 3)
开发者ID:charanpald,项目名称:sandbox,代码行数:18,代码来源:SparseUtilsTest.py

示例9: profilePrecisionAtK

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [as 别名]
 def profilePrecisionAtK(self):
     m = 1000 
     n = 500000 
     r = 30 
     k = m*100
     
     X, U, s, V = SparseUtils.generateSparseLowRank((m,n), r, k, verbose=True)
     mean = X.data.mean()
     X.data[X.data <= mean] = 0
     X.data[X.data > mean] = 1
     
     import sppy 
     X = sppy.csarray(X)
     
     
     ProfileUtils.profile("MCEvaluator.precisionAtK(X, U, V, 10)", globals(), locals())
开发者ID:charanpald,项目名称:sandbox,代码行数:18,代码来源:MCEvaluatorProfile.py

示例10: testCentreRows2

# 需要导入模块: from sandbox.util.SparseUtils import SparseUtils [as 别名]
# 或者: from sandbox.util.SparseUtils.SparseUtils import generateSparseLowRank [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.generateSparseLowRank方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。