예제 #1
0
    def testRand(self):
        X = sppy.rand((10, 10), 0.1)

        self.assertEquals(X.shape, (10, 10))
        self.assertAlmostEqual(X.getnnz() / float(100), 0.1, 1)

        X = sppy.rand(100, 0.1)

        self.assertEquals(X.shape, (100, ))
        self.assertAlmostEqual(X.getnnz() / float(100), 0.1, 1)
예제 #2
0
    def testRand(self): 
        X = sppy.rand((10, 10), 0.1)

        self.assertEquals(X.shape, (10, 10))
        self.assertAlmostEqual(X.getnnz()/float(100), 0.1, 1)     
        
        X = sppy.rand(100, 0.1)

        self.assertEquals(X.shape, (100, ))
        self.assertAlmostEqual(X.getnnz()/float(100), 0.1, 1)  
예제 #3
0
파일: coreTest.py 프로젝트: stjordanis/sppy
 def testRsvd(self): 
     n = 100 
     m = 80
     A = sppy.rand((m, n), 0.5)
     
     ks = [10, 20, 30, 40] 
     q = 2 
     
     lastError = numpy.linalg.norm(A.toarray())        
     
     for k in ks: 
         U, s, V = sppy.linalg.rsvd(A, k, q)
         
         nptst.assert_array_almost_equal(U.T.dot(U), numpy.eye(k))
         nptst.assert_array_almost_equal(V.T.dot(V), numpy.eye(k))
         A2 = (U*s).dot(V.T)
         
         error = numpy.linalg.norm(A.toarray() - A2)
         self.assertTrue(error <= lastError)
         lastError = error 
         
         #Compare versus exact svd 
         U, s, V = numpy.linalg.svd(numpy.array(A.toarray()))
         inds = numpy.flipud(numpy.argsort(s))[0:k*2]
         U, s, V = Util.indSvd(U, s, V, inds)
         
         Ak = (U*s).dot(V.T)
         
         error2 = numpy.linalg.norm(A.toarray() - Ak)
         self.assertTrue(error2 <= error)
예제 #4
0
 def profileDot2(self): 
     density = 0.01
     m = 10000
     n = 10000
     a_sppy = sppy.rand((m, n), density, storagetype='row')
     a_sppy_T = sppy.csarray(a_sppy.T, storagetype="col")
     ProfileUtils.profile('a_sppy.dot(a_sppy_T)', globals(), locals())
예제 #5
0
 def testRsvd(self): 
     n = 100 
     m = 80
     A = sppy.rand((m, n), 0.5)
     
     ks = [10, 20, 30, 40] 
     q = 2 
     
     lastError = numpy.linalg.norm(A.toarray())        
     
     for k in ks: 
         U, s, V = sppy.linalg.rsvd(A, k, q)
         
         nptst.assert_array_almost_equal(U.T.dot(U), numpy.eye(k))
         nptst.assert_array_almost_equal(V.T.dot(V), numpy.eye(k))
         A2 = (U*s).dot(V.T)
         
         error = numpy.linalg.norm(A.toarray() - A2)
         self.assertTrue(error <= lastError)
         lastError = error 
         
         #Compare versus exact svd 
         U, s, V = numpy.linalg.svd(numpy.array(A.toarray()))
         inds = numpy.flipud(numpy.argsort(s))[0:k*2]
         U, s, V = Util.indSvd(U, s, V, inds)
         
         Ak = (U*s).dot(V.T)
         
         error2 = numpy.linalg.norm(A.toarray() - Ak)
         self.assertTrue(error2 <= error)
예제 #6
0
 def testWrite(self): 
     m = 10 
     n = 5 
     density = 0.5
     A = sppy.rand((m, n), density)
     
     sppy.io.mmwrite(tempfile.tempdir + "/test.mtx", A)
예제 #7
0
    def testWrite(self):
        m = 10
        n = 5
        density = 0.5
        A = sppy.rand((m, n), density)

        sppy.io.mmwrite(tempfile.tempdir + "/test.mtx", A)
예제 #8
0
 def setUp(self):
     numpy.set_printoptions(suppress=True, precision=3)
     logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
     
     shape = (15, 15)
     density = 0.5        
     self.A = rand(shape, density)
     self.A = self.A + self.A.T
예제 #9
0
    def setUp(self):
        numpy.set_printoptions(suppress=True, precision=3)
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

        shape = (15, 15)
        density = 0.5
        self.A = rand(shape, density)
        self.A = self.A + self.A.T
예제 #10
0
 def testNorm(self): 
     n = 100 
     m = 80
     numRuns = 10         
     
     for i in range(numRuns): 
         A = sppy.rand((m, n), 0.5)
         
         self.assertAlmostEquals(numpy.linalg.norm(A.toarray()), sppy.linalg.norm(A))
예제 #11
0
파일: coreTest.py 프로젝트: stjordanis/sppy
 def testNorm(self): 
     n = 100 
     m = 80
     numRuns = 10         
     
     for i in range(numRuns): 
         A = sppy.rand((m, n), 0.5)
         
         self.assertAlmostEquals(numpy.linalg.norm(A.toarray()), sppy.linalg.norm(A))
예제 #12
0
 def profileRowSlice(self): 
     numpy.random.seed(21)
     m = 100000
     n = 1000000
     #numInds = 10000000
     
     X = sppy.rand((m, n), density=0.001, storagetype="row")
     
     
     #ProfileUtils.profile('X[0:1000, :] ', globals(), locals())
     ProfileUtils.profile('X.submatrix(0, 0, 1000, n)', globals(), locals())
예제 #13
0
    def testAsLinearOperatorSum(self):

        n = 10
        m = 5
        density = 0.3
        A = rand((n, m), density)
        B = rand((n, m), density)

        L = GeneralLinearOperator.asLinearOperator(A)
        M = GeneralLinearOperator.asLinearOperator(B)

        N = GeneralLinearOperator.asLinearOperatorSum(L, M)

        k = 3
        V = numpy.random.rand(m, k)
        W = numpy.random.rand(n, k)

        U = N.matmat(V)
        U2 = A.dot(V) + B.dot(V)
        nptst.assert_array_almost_equal(U, U2)

        U = N.rmatmat(W)
        U2 = A.T.dot(W) + B.T.dot(W)
        nptst.assert_array_almost_equal(U, U2)

        v = numpy.random.rand(m)
        w = numpy.random.rand(n)

        u = N.matvec(v)
        u2 = A.dot(v) + B.dot(v)
        nptst.assert_array_almost_equal(u, u2)

        u = N.rmatvec(w)
        u2 = A.T.dot(w) + B.T.dot(w)
        nptst.assert_array_almost_equal(u, u2)

        #See if we get an error if A, B are different shapes
        B = rand((m, n), 0.1)
        M = GeneralLinearOperator.asLinearOperator(B)
        self.assertRaises(ValueError,
                          GeneralLinearOperator.asLinearOperatorSum, L, M)
예제 #14
0
    def testAsLinearOperatorSum(self): 
        
        n = 10 
        m = 5 
        density = 0.3
        A = rand((n, m), density)    
        B = rand((n, m), density)
        
        L = GeneralLinearOperator.asLinearOperator(A)
        M = GeneralLinearOperator.asLinearOperator(B)

        N = GeneralLinearOperator.asLinearOperatorSum(L, M)
        
        k = 3
        V = numpy.random.rand(m, k)
        W = numpy.random.rand(n, k)
        
        U = N.matmat(V)
        U2 = A.dot(V) + B.dot(V)
        nptst.assert_array_almost_equal(U, U2)
       
        U = N.rmatmat(W)
        U2 = A.T.dot(W) + B.T.dot(W)
        nptst.assert_array_almost_equal(U, U2)     
        
        v = numpy.random.rand(m)
        w = numpy.random.rand(n)        
        
        u = N.matvec(v)
        u2 = A.dot(v) + B.dot(v)
        nptst.assert_array_almost_equal(u, u2)
        
        u = N.rmatvec(w)
        u2 = A.T.dot(w) + B.T.dot(w)
        nptst.assert_array_almost_equal(u, u2)
        
        #See if we get an error if A, B are different shapes 
        B = rand((m, n), 0.1)
        M = GeneralLinearOperator.asLinearOperator(B)
        self.assertRaises(ValueError, GeneralLinearOperator.asLinearOperatorSum, L, M)
예제 #15
0
 def syntheticDataset1(m=500, n=200, k=8, u=0.1, sd=0, noise=5): 
     """
     Create a simple synthetic dataset 
     """
     w = 1-u
     X, U, s, V, wv = SparseUtils.generateSparseBinaryMatrix((m,n), k, w, sd=sd, csarray=True, verbose=True, indsPerRow=200)
     X = X + sppy.rand((m, n), noise/float(n), storagetype="row")
     X[X.nonzero()] = 1
     X.prune()
     X = SparseUtils.pruneMatrixRows(X, minNnzRows=10)
     logging.debug("Non zero elements: " + str(X.nnz) + " shape: " + str(X.shape))
     U = U*s
     
     return X, U, V
예제 #16
0
 def testCenterRowsCsarray(self):
     
     numRuns = 10        
     
     for i in range(numRuns): 
         density = numpy.random.rand()
         m = numpy.random.randint(10, 100) 
         n = numpy.random.randint(10, 100) 
         X = sppy.rand((m,n), density)
         
 
         SparseUtilsCython.centerRowsCsarray(X)
 
         
         nptst.assert_array_almost_equal(X.sum(1), numpy.zeros(m))
예제 #17
0
    def testPruneMatrixRows(self):
        m = 30
        n = 20
        density = 0.5
        X = sppy.rand((m, n), density)
        X[X.nonzero()] = 1

        newX, rowInds = SparseUtils.pruneMatrixRows(X, 10, verbose=True)

        nnzRows = numpy.zeros(m)
        for i in range(m):
            nnzRows[i] = X.toarray()[i, :].nonzero()[0].shape[0]

            if nnzRows[i] >= 10:
                self.assertTrue(i in rowInds)

        self.assertTrue((newX.sum(1) >= 10).all())
예제 #18
0
 def testPruneMatrixRows(self): 
     m = 30 
     n = 20 
     density = 0.5 
     X = sppy.rand((m, n), density)
     X[X.nonzero()] = 1        
             
     newX, rowInds = SparseUtils.pruneMatrixRows(X, 10, verbose=True)
     
     nnzRows = numpy.zeros(m)
     for i in range(m): 
         nnzRows[i] = X.toarray()[i, :].nonzero()[0].shape[0]
         
         if nnzRows[i] >= 10: 
             self.assertTrue(i in rowInds)
         
         
     self.assertTrue((newX.sum(1) >= 10).all())
예제 #19
0
    def testPruneMatrixCols(self):
        m = 30
        n = 20
        density = 0.5
        X = sppy.rand((m, n), density)
        X[X.nonzero()] = 1

        newX, rowInds = SparseUtils.pruneMatrixCols(X, maxNnz=10, verbose=True)

        nnzCols = numpy.zeros(n)
        for i in range(n):
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]

            if nnzCols[i] <= 10:
                self.assertTrue(i in rowInds)

        self.assertTrue((newX.sum(0) <= 10).all())

        newX, rowInds = SparseUtils.pruneMatrixCols(X, minNnz=10, verbose=True)

        nnzCols = numpy.zeros(n)
        for i in range(n):
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]

            if nnzCols[i] >= 10:
                self.assertTrue(i in rowInds)

        self.assertTrue((newX.sum(0) >= 10).all())

        newX, rowInds = SparseUtils.pruneMatrixCols(X,
                                                    minNnz=10,
                                                    maxNnz=15,
                                                    verbose=True)

        nnzCols = numpy.zeros(n)
        for i in range(n):
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]

            if nnzCols[i] >= 10 and nnzCols[i] <= 15:
                self.assertTrue(i in rowInds)

        self.assertTrue(
            numpy.logical_and(newX.sum(0) >= 10,
                              newX.sum(0) <= 15).all())
예제 #20
0
    def testPruneMatrixCols(self): 
        m = 30 
        n = 20 
        density = 0.5 
        X = sppy.rand((m, n), density)
        X[X.nonzero()] = 1        
                
        newX, rowInds = SparseUtils.pruneMatrixCols(X, maxNnz=10, verbose=True)
        
        nnzCols = numpy.zeros(n)
        for i in range(n): 
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]
            
            if nnzCols[i] <= 10: 
                self.assertTrue(i in rowInds)
            
        self.assertTrue((newX.sum(0) <= 10).all()) 


        newX, rowInds = SparseUtils.pruneMatrixCols(X, minNnz=10, verbose=True)
        
        nnzCols = numpy.zeros(n)
        for i in range(n): 
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]
            
            if nnzCols[i] >= 10: 
                self.assertTrue(i in rowInds)
            
        self.assertTrue((newX.sum(0) >= 10).all()) 

        newX, rowInds = SparseUtils.pruneMatrixCols(X, minNnz=10, maxNnz=15, verbose=True)
        
        nnzCols = numpy.zeros(n)
        for i in range(n): 
            nnzCols[i] = X.toarray()[:, i].nonzero()[0].shape[0]
            
            if nnzCols[i] >= 10 and nnzCols[i] <= 15: 
                self.assertTrue(i in rowInds)
            
        self.assertTrue(numpy.logical_and(newX.sum(0) >= 10, newX.sum(0) <= 15).all()) 
예제 #21
0
 def testRead(self): 
     m = 10 
     n = 5 
     density = 0.5
     A = sppy.rand((m, n), density)
     
     fileName = tempfile.tempdir + "/test.mtx"
     sppy.io.mmwrite(fileName, A)
     
     B = sppy.io.mmread(fileName)        
     
     nptst.assert_array_almost_equal(A.toarray(), B.toarray(), 5)
     self.assertEquals(A.dtype, B.dtype)
     
     #Now try an integer array 
     A = sppy.csarray((m,n), dtype=numpy.int)
     A[0, 1] = 5 
     A[2, 2] = -2
     A[6, 3] = 1
     
     sppy.io.mmwrite(fileName, A)
     B = sppy.io.mmread(fileName) 
     nptst.assert_array_almost_equal(A.toarray(), B.toarray(), 5)
     self.assertEquals(A.dtype, B.dtype)
예제 #22
0
    def testRead(self):
        m = 10
        n = 5
        density = 0.5
        A = sppy.rand((m, n), density)

        fileName = tempfile.tempdir + "/test.mtx"
        sppy.io.mmwrite(fileName, A)

        B = sppy.io.mmread(fileName)

        nptst.assert_array_almost_equal(A.toarray(), B.toarray(), 5)
        self.assertEquals(A.dtype, B.dtype)

        #Now try an integer array
        A = sppy.csarray((m, n), dtype=numpy.int)
        A[0, 1] = 5
        A[2, 2] = -2
        A[6, 3] = 1

        sppy.io.mmwrite(fileName, A)
        B = sppy.io.mmread(fileName)
        nptst.assert_array_almost_equal(A.toarray(), B.toarray(), 5)
        self.assertEquals(A.dtype, B.dtype)