Search code examples
numpyscipysparse-matrix

numpy.sum() gives `TypeError: sum() got an unexpected keyword argument 'dtype'`


The following code generated an unexpected TypeError:

import scipy.sparse
import numpy

coomatrix = scipy.sparse.coo_matrix((100,100))
numpy.sum(coomatrix)

the result:

TypeError: sum() got an unexpected keyword argument 'dtype'

scipy version 0.14.0, numpy version 1.9.0


Solution

  • The problem is that numpy.sum doesn't know how to handle sparse matrices. The following works as expected:

    coomatrix.sum()