[SciPy-User] Help: Multiplying two sparse matrices

Pranava Swaroop Madhyastha pranava at lsi.upc.edu
Tue Dec 25 13:34:11 EST 2012


Hi,

I have a problem multiplying two sparse matrices. Here is a sample:

import scipy.sparse as ss

D = ss.rand(49837, 1000, format='csr', density=0.1)

E = ss.rand(1000, 49837, format='csr', density=0.1)

D * E

This is the error that I get:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-c94d439b5bf5> in <module>()
----> 1 D * E

/usr/local/lib/python2.7/dist-packages/scipy/sparse/base.pyc in
__mul__(self, other)
    254             if self.shape[1] != other.shape[0]:
    255                 raise ValueError('dimension mismatch')
--> 256             return self._mul_sparse_matrix(other)
    257
    258         try:

/usr/local/lib/python2.7/dist-packages/scipy/sparse/compressed.py in
_mul_sparse_matrix(self, other)
    296
    297         nnz = indptr[-1]
--> 298         indices = np.empty(nnz, dtype=np.intc)
    299         data    = np.empty(nnz, dtype=upcast(self.dtype,other.dtype))
    300

ValueError: negative dimensions are not allowed


Apparently, the error seems to be because of dtype=np.intc. Is there a
work around for this?

Thanks and Regards,
Pranava



More information about the SciPy-User mailing list