Why does isspmatrix return False?

18 views
Skip to first unread message

Briana Young

unread,
Nov 7, 2018, 1:56:56 PM11/7/18
to SciPy-user
Hi there,

I'm trying to use the isspmatrix (alias issparse) function.

Constructed a sparse matrix using csr_matrix, tried converting it to a standard python list of lists, then a flat 1-D list (the last two, just to see if it was a formatting thing).

In each case, the function returns false. Can anyone see why?

import numpy as np
from scipy.sparse import csr_matrix, isspmatrix, issparse

indptr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
indices = np.array([1, 3, 4, 0, 2, 5, 0, 1, 2, 3, 5])
data = np.array([9, 4, 8, 5, 1, 7, 1, 5, 3, 5, 9])

sparse_array = csr_matrix((data, indices, indptr), shape=(11, 6)).toarray()
print(sparse_array)
print(isspmatrix(sparse_array))

sparse_list = np.array(sparse_array).tolist()
print(sparse_list)
print(isspmatrix(sparse_list))

sparse_matrix = []
for l in sparse_list:
for i in l:
sparse_matrix.append(i)
print(sparse_matrix)
print(isspmatrix(sparse_matrix))

Briana Young

unread,
Nov 8, 2018, 12:56:18 PM11/8/18
to SciPy-user
For anyone interested, I figured this out. Re-compressing the matrix returns True. Looks like all sparse matrices need to be in csr or csc form to be read properly:

import numpy as np
from scipy.sparse import csr_matrix, isspmatrix, issparse

indptr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
indices = np.array([1, 3, 4, 0, 2, 5, 0, 1, 2, 3, 5])
data = np.array([9, 4, 8, 5, 1, 7, 1, 5, 3, 5, 9])

sparse_array = csr_matrix((data, indices, indptr), shape=(11, 6)).toarray()
print(sparse_array)
print(isspmatrix(sparse_array))

SParray = csr_matrix(sparse_array)
print(SParray)
print(isspmatrix(SParray))
Reply all
Reply to author
Forward
0 new messages