Reputation: 18167
I'm a little bit confused by the behavior of the multiplication operator *
when scipy sparse matrices are involved. It seems the operator implements matrix multiplication, not component-wise multiplication as it would with numpy arrays.
Some code to check this:
from scipy.sparse import lil_matrix
A = lil_matrix(-numpy.eye(2))
b = lil_matrix(numpy.ones((2,2)))
print (A * B).toarray()
results in:
[[-1. -1.]
[-1. -1.]]
The documentation of the scipy.sparse
module does not really go into details on this, and I wonder whether there is a clear specification of the multiplication behavior somewhere?
Furthermore, are there some clearly defined rules for multiplication operator with scipy sparse matrices and numpy matrices or arrays?
Upvotes: 4
Views: 2972
Reputation: 305
Documentation is indeed scarse. If you are looking for component-wise multiplication you can use A.multiply(b)
, where b can be an element, vector or matrix:
https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.sparse.csr_matrix.multiply.html
Upvotes: 2