本文整理汇总了Python中sppy.csarray函数的典型用法代码示例。如果您正苦于以下问题:Python csarray函数的具体用法?Python csarray怎么用?Python csarray使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了csarray函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testStr
def testStr(self):
nrow = 5
ncol = 7
storagetypes = ["col", "row"]
for storagetype in storagetypes:
A = csarray((nrow, ncol), storagetype=storagetype)
A[0, 1] = 1
A[1, 3] = 5.2
A[3, 3] = -0.2
outputStr = "csarray dtype:float64 shape:(5, 7) non-zeros:3 storage:" + A.storagetype + "\n"
outputStr += "(0, 1) 1.0\n"
outputStr += "(1, 3) 5.2\n"
outputStr += "(3, 3) -0.2"
self.assertEquals(str(A), outputStr)
B = csarray((5, 5), storagetype=storagetype)
outputStr = "csarray dtype:float64 shape:(5, 5) non-zeros:0 storage:" + B.storagetype + "\n"
self.assertEquals(str(B), outputStr)
outputStr = "csarray dtype:float64 shape:(10,) non-zeros:3\n"
outputStr += "(0) 23.0\n"
outputStr += "(3) 1.2\n"
outputStr += "(4) -8.0"
self.assertEquals(str(self.a), outputStr)
outputStr = "csarray dtype:float64 shape:(3,) non-zeros:0\n"
self.assertEquals(str(self.c), outputStr)
示例2: testSub
def testSub(self):
nptst.assert_array_equal((self.A - self.A).toarray(), self.A.toarray() * 0)
nptst.assert_array_equal((self.B - self.B).toarray(), self.B.toarray() * 0)
nptst.assert_array_equal((self.C - self.C).toarray(), self.C.toarray() * 0)
nptst.assert_array_equal((self.D - self.D).toarray(), self.D.toarray() * 0)
nptst.assert_array_equal((self.F - self.F).toarray(), self.F.toarray() * 0)
nptst.assert_array_equal((self.G - self.G).toarray(), self.G.toarray() * 0)
nptst.assert_array_equal((self.H - self.H).toarray(), self.H.toarray() * 0)
nptst.assert_array_equal((self.B * 2 - self.B).toarray(), self.B.toarray())
A = csarray((5, 5))
A[0, 1] = 4
A[1, 3] = 2
A[3, 3] = 1
B = csarray((5, 5))
B[0, 2] = 9.2
B[2, 3] = -5
B[3, 4] = 12
nptst.assert_array_equal((A - B).toarray(), A.toarray() - B.toarray())
nptst.assert_array_equal((self.a - self.a).toarray(), self.a.toarray() * 0)
nptst.assert_array_equal((self.b - self.b).toarray(), self.b.toarray() * 0)
nptst.assert_array_equal((self.c - self.c).toarray(), self.c.toarray() * 0)
示例3: testAdd
def testAdd(self):
# print(self.A.__add__(self.A._array))
nptst.assert_array_equal((self.A + self.A).toarray(), self.A.toarray() * 2)
nptst.assert_array_equal((self.B + self.B).toarray(), self.B.toarray() * 2)
nptst.assert_array_equal((self.C + self.C).toarray(), self.C.toarray() * 2)
nptst.assert_array_equal((self.D + self.D).toarray(), self.D.toarray() * 2)
nptst.assert_array_equal((self.F + self.F).toarray(), self.F.toarray() * 2)
nptst.assert_array_equal((self.G + self.G).toarray(), self.G.toarray() * 2)
nptst.assert_array_equal((self.H + self.H).toarray(), self.H.toarray() * 2)
A = csarray((5, 5))
A[0, 1] = 4
A[1, 3] = 2
A[3, 3] = 1
B = csarray((5, 5))
B[0, 2] = 9.2
B[2, 3] = -5
B[3, 4] = 12
nptst.assert_array_equal((A + B).toarray(), A.toarray() + B.toarray())
nptst.assert_array_equal((self.a + self.a).toarray(), self.a.toarray() * 2)
nptst.assert_array_equal((self.b + self.b).toarray(), self.b.toarray() * 2)
nptst.assert_array_equal((self.c + self.c).toarray(), self.c.toarray() * 2)
示例4: testHadamard
def testHadamard(self):
nptst.assert_array_equal((self.A.hadamard(self.A)).toarray(), (self.A.toarray()) ** 2)
nptst.assert_array_equal((self.B.hadamard(self.B)).toarray(), self.B.toarray() ** 2)
nptst.assert_array_equal((self.C.hadamard(self.C)).toarray(), self.C.toarray() ** 2)
nptst.assert_array_equal((self.D.hadamard(self.D)).toarray(), self.D.toarray() ** 2)
nptst.assert_array_equal((self.F.hadamard(self.F)).toarray(), self.F.toarray() ** 2)
nptst.assert_array_equal((self.G.hadamard(self.G)).toarray(), self.G.toarray() ** 2)
nptst.assert_array_equal((self.H.hadamard(self.H)).toarray(), self.H.toarray() ** 2)
for storagetype in self.storagetypes:
A = csarray((5, 5), storagetype=storagetype)
A[0, 1] = 4
A[2, 3] = -1.2
A[1, 3] = 2
A[3, 3] = 1
B = csarray((5, 5), storagetype=storagetype)
B[0, 2] = 9.2
B[2, 3] = -5
B[3, 4] = 12
B[3, 3] = 12
C = csarray((5, 5), storagetype=storagetype)
nptst.assert_array_equal((A.hadamard(B)).toarray(), A.toarray() * B.toarray())
nptst.assert_array_equal((A.hadamard(C)).toarray(), C.toarray())
nptst.assert_array_equal((self.a.hadamard(self.a)).toarray(), (self.a.toarray()) ** 2)
nptst.assert_array_equal((self.b.hadamard(self.b)).toarray(), (self.b.toarray()) ** 2)
nptst.assert_array_equal((self.c.hadamard(self.c)).toarray(), (self.c.toarray()) ** 2)
示例5: testHadamard
def testHadamard(self):
nptst.assert_array_equal((self.A.hadamard(self.A)).toarray(), (self.A.toarray())**2)
nptst.assert_array_equal((self.B.hadamard(self.B)).toarray(), self.B.toarray()**2)
nptst.assert_array_equal((self.C.hadamard(self.C)).toarray(), self.C.toarray()**2)
nptst.assert_array_equal((self.D.hadamard(self.D)).toarray(), self.D.toarray()**2)
nptst.assert_array_equal((self.F.hadamard(self.F)).toarray(), self.F.toarray()**2)
A = csarray((5, 5))
A[0, 1] = 4
A[2, 3] = -1.2
A[1, 3] = 2
A[3, 3] = 1
B = csarray((5, 5))
B[0, 2] = 9.2
B[2, 3] = -5
B[3, 4] = 12
B[3, 3] = 12
C = csarray((5, 5))
nptst.assert_array_equal((A.hadamard(B)).toarray(), A.toarray()*B.toarray())
nptst.assert_array_equal((A.hadamard(C)).toarray(), C.toarray())
nptst.assert_array_equal((self.a.hadamard(self.a)).toarray(), (self.a.toarray())**2)
nptst.assert_array_equal((self.b.hadamard(self.b)).toarray(), (self.b.toarray())**2)
nptst.assert_array_equal((self.c.hadamard(self.c)).toarray(), (self.c.toarray())**2)
示例6: testDot
def testDot(self):
A = csarray((5, 5))
A[0, 1] = 4
A[2, 3] = -1.2
A[1, 3] = 2
A[3, 3] = 1
B = A.dot(A)
nptst.assert_array_equal(B.toarray(), A.toarray().dot(A.toarray()))
B = self.D.dot(self.D)
nptst.assert_array_equal(B.toarray(), self.D.toarray().dot(self.D.toarray()))
C = csarray((5, 2))
for i in range(5):
for j in range(2):
C[i, j] = 1
self.assertRaises(ValueError, C.dot, C)
B = A.dot(C)
nptst.assert_array_equal(B.toarray(), A.toarray().dot(C.toarray()))
self.assertEquals((self.a.dot(self.a)), (self.a.dot(self.a)))
self.assertEquals((self.b.dot(self.b)), (self.b.dot(self.b)))
self.assertEquals((self.c.dot(self.c)), (self.c.dot(self.c)))
示例7: testStr
def testStr(self):
nrow = 5
ncol = 7
A = csarray((nrow, ncol))
A[0, 1] = 1
A[1, 3] = 5.2
A[3, 3] = -0.2
outputStr = "csarray dtype:float64 shape:(5, 7) non-zeros:3\n"
outputStr += "(0, 1) 1.0\n"
outputStr += "(1, 3) 5.2\n"
outputStr += "(3, 3) -0.2"
self.assertEquals(str(A), outputStr)
B = csarray((5, 5))
outputStr = "csarray dtype:float64 shape:(5, 5) non-zeros:0\n"
self.assertEquals(str(B), outputStr)
outputStr = "csarray dtype:float64 shape:(10,) non-zeros:3\n"
outputStr +="(0) 23.0\n"
outputStr +="(3) 1.2\n"
outputStr +="(4) -8.0"
self.assertEquals(str(self.a), outputStr)
outputStr = "csarray dtype:float64 shape:(3,) non-zeros:0\n"
self.assertEquals(str(self.c), outputStr)
示例8: testGetOmegaListPtr
def testGetOmegaListPtr(self):
import sppy
m = 10
n = 5
X = scipy.sparse.rand(m, n, 0.1)
X = X.tocsr()
indPtr, colInds = SparseUtils.getOmegaListPtr(X)
for i in range(m):
omegai = colInds[indPtr[i]:indPtr[i+1]]
nptst.assert_array_almost_equal(omegai, X.toarray()[i, :].nonzero()[0])
Xsppy = sppy.csarray(X)
indPtr, colInds = SparseUtils.getOmegaListPtr(Xsppy)
for i in range(m):
omegai = colInds[indPtr[i]:indPtr[i+1]]
nptst.assert_array_almost_equal(omegai, X.toarray()[i, :].nonzero()[0])
#Test a zero array (scipy doesn't work in this case)
X = sppy.csarray((m,n))
indPtr, colInds = SparseUtils.getOmegaListPtr(X)
for i in range(m):
omegai = colInds[indPtr[i]:indPtr[i+1]]
示例9: testBiCGSTAB
def testBiCGSTAB(self):
#This doesn't always converge
numRuns = 10
for i in range(numRuns):
n = numpy.random.randint(5, 20)
A = numpy.random.rand(n, n)
x = numpy.random.rand(n)
b = A.dot(x)
A = sppy.csarray(A)
x2, output = sppy.linalg.biCGSTAB(A, b, tol=10**-6, maxIter=n)
if output == 0:
nptst.assert_array_almost_equal(x, x2, 3)
#Try with bad input
m = 3
n = 5
A = numpy.random.rand(n, m)
A = sppy.csarray(A)
x = numpy.random.rand(m)
b = A.dot(x)
self.assertRaises(ValueError, sppy.linalg.biCGSTAB, A, b)
A = numpy.random.rand(n, n)
A = sppy.csarray(A)
b = numpy.array(n+1)
self.assertRaises(ValueError, sppy.linalg.biCGSTAB, A, b)
示例10: testNDim
def testNDim(self):
A = csarray((5, 7))
self.assertEquals(A.ndim, 2)
A = csarray((0, 0))
self.assertEquals(A.ndim, 2)
self.assertEquals(self.a.ndim, 1)
self.assertEquals(self.b.ndim, 1)
示例11: loadMatrix
def loadMatrix(filename):
M = scipy.io.mmread(filename)
if type(M) == numpy.ndarray:
M2 = sppy.csarray(M)
elif scipy.sparse.issparse(M):
M2 = sppy.csarray(M.shape, dtype=M.dtype)
M2[M.nonzero()] = M.data
return M2
示例12: testNonZeroInds
def testNonZeroInds(self):
(rowInds, colInds) = self.B.nonzero()
for i in range(rowInds.shape[0]):
self.assertNotEqual(self.B[rowInds[i], colInds[i]], 0)
self.assertEquals(self.B.getnnz(), rowInds.shape[0])
self.assertEquals(self.B.sum(), self.B[rowInds, colInds].sum())
(rowInds, colInds) = self.C.nonzero()
for i in range(rowInds.shape[0]):
self.assertNotEqual(self.C[rowInds[i], colInds[i]], 0)
self.assertEquals(self.C.getnnz(), rowInds.shape[0])
self.assertEquals(self.C.sum(), self.C[rowInds, colInds].sum())
(rowInds, colInds) = self.F.nonzero()
for i in range(rowInds.shape[0]):
self.assertNotEqual(self.F[rowInds[i], colInds[i]], 0)
self.assertEquals(self.F.getnnz(), rowInds.shape[0])
self.assertEquals(self.F.sum(), self.F[rowInds, colInds].sum())
(inds, ) = self.a.nonzero()
for i in range(inds.shape[0]):
self.assertNotEqual(self.a[inds[i]], 0)
#Try an array with no non zeros
nrow = 5
ncol = 7
A = csarray((nrow, ncol))
(rowInds, colInds) = A.nonzero()
self.assertEquals(A.getnnz(), rowInds.shape[0])
self.assertEquals(rowInds.shape[0], 0)
self.assertEquals(colInds.shape[0], 0)
(inds, ) = self.c.nonzero()
self.assertEquals(inds.shape[0], 0)
#Zero size array
nrow = 0
ncol = 0
A = csarray((nrow, ncol))
(rowInds, colInds) = A.nonzero()
self.assertEquals(A.getnnz(), rowInds.shape[0])
self.assertEquals(rowInds.shape[0], 0)
self.assertEquals(colInds.shape[0], 0)
(inds, ) = self.d.nonzero()
self.assertEquals(inds.shape[0], 0)
示例13: testNDim
def testNDim(self):
A = csarray((5, 7))
self.assertEquals(A.ndim, 2)
A = csarray((5, 7), storagetype="row")
self.assertEquals(A.ndim, 2)
A = csarray((0, 0))
self.assertEquals(A.ndim, 2)
self.assertEquals(self.a.ndim, 1)
self.assertEquals(self.b.ndim, 1)
示例14: setUp
def setUp(self):
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
self.A = csarray((5, 5))
nrow = 5
ncol = 7
self.B = csarray((nrow, ncol))
self.B[0, 1] = 1
self.B[1, 3] = 5.2
self.B[3, 3] = -0.2
self.B[0, 6] = -1.23
self.B[4, 4] = 12.2
nrow = 100
ncol = 100
self.C = csarray((nrow, ncol))
self.C[0, 1] = 1
self.C[10, 3] = 5.2
self.C[30, 34] = -0.2
self.C[0, 62] = -1.23
self.C[4, 41] = 12.2
self.D = csarray((5, 5))
self.D[0, 0] = 23.1
self.D[2, 0] = -3.1
self.D[3, 0] = -10.0
self.D[2, 1] = -5
self.D[3, 1] = 5
self.E = csarray((0, 0))
self.F = csarray((6, 6), dtype=numpy.int)
self.F[0, 0] = 23
self.F[2, 0] = -3
self.F[3, 0] = -10
self.F[2, 1] = -5
self.F[3, 1] = 5
self.a = csarray(10, dtype=numpy.float)
self.a[0] = 23
self.a[3] = 1.2
self.a[4] = -8
self.b = csarray(10, dtype=numpy.int)
self.b[0] = 23
self.b[5] = 1
self.b[8] = -8
self.c = csarray((3, ), dtype=numpy.float)
self.d = csarray((0, ), dtype=numpy.float)
示例15: testSetItem
def testSetItem(self):
nrow = 5
ncol = 7
A = csarray((nrow, ncol))
A[0, 1] = 1
A[1, 3] = 5.2
A[3, 3] = -0.2
self.assertEquals(A[0, 1], 1)
self.assertAlmostEquals(A[1, 3], 5.2)
self.assertAlmostEquals(A[3, 3], -0.2)
for i in range(nrow):
for j in range(ncol):
if (i, j) != (0, 1) and (i, j) != (1, 3) and (i, j) != (3, 3):
self.assertEquals(A[i, j], 0)
self.assertRaises(ValueError, A.__setitem__, (20, 1), 1)
self.assertRaises(TypeError, A.__setitem__, (1, 1), "a")
self.assertRaises(ValueError, A.__setitem__, (1, 100), 1)
self.assertRaises(ValueError, A.__setitem__, (-1, 1), 1)
self.assertRaises(ValueError, A.__setitem__, (0, -1), 1)
result = A[(numpy.array([0, 1, 3]), numpy.array([1, 3, 3]))]
self.assertEquals(result[0], 1)
self.assertEquals(result[1], 5.2)
self.assertEquals(result[2], -0.2)
#Replace value of A
A[0, 1] = 2
self.assertEquals(A[0, 1], 2)
self.assertAlmostEquals(A[1, 3], 5.2)
self.assertAlmostEquals(A[3, 3], -0.2)
for i in range(nrow):
for j in range(ncol):
if (i, j) != (0, 1) and (i, j) != (1, 3) and (i, j) != (3, 3):
self.assertEquals(A[i, j], 0)
#Try setting items with arrays
A = csarray((nrow, ncol))
A[numpy.array([0, 1]), numpy.array([2, 3])] = numpy.array([1.2, 2.4])
self.assertEquals(A.getnnz(), 2)
self.assertEquals(A[0, 2], 1.2)
self.assertEquals(A[1, 3], 2.4)
A[numpy.array([2, 4]), numpy.array([2, 3])] = 5
self.assertEquals(A[2, 2], 5)
self.assertEquals(A[4, 3], 5)