Domanda

I would like to know the best way to check if a scipy sparse matrix, if CSC or CSR. Right now I'm using.

rows, cols = X.shape()
indptr = X.indptr()
if len(indptr) == cols + 1:
    print "csc"
else:
    print "csr"

Thanks.

È stato utile?

Soluzione

It looks like you could use the .getformat() method:

>>> m0 = scipy.sparse.csc_matrix([1])
>>> m0.getformat()
'csc'
>>> m1 = scipy.sparse.csr_matrix([1])
>>> m1.getformat()
'csr'

Altri suggerimenti

You could check the class

m0=sparse.csc_matrix([1])

In [4]: type(m0).__name__
Out[4]: 'csc_matrix'
In [5]: isinstance(m0,sparse.csc_matrix)
Out[5]: True

In [6]: isinstance(m0,sparse.csr_matrix)
Out[6]: False

In [9]: sparse.isspmatrix_csc(m0)
Out[9]: True

In [10]: sparse.isspmatrix_csc??
...
def isspmatrix_csc(x):
    return isinstance(x, csc_matrix)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top