The multiply
method does element-wise multiplication.
Here's an example, in which a
and b
are sparse matrices with COO format. (The .A
attribute returns a regular numpy array. I use it to display the values in the sparse matrices.)
In [41]: a
Out[41]:
<5x8 sparse matrix of type '<class 'numpy.int64'>'
with 20 stored elements in COOrdinate format>
In [42]: a.A
Out[42]:
array([[0, 9, 2, 9, 0, 6, 6, 2],
[2, 0, 0, 0, 1, 0, 8, 0],
[0, 3, 0, 0, 2, 9, 0, 4],
[0, 0, 0, 0, 0, 0, 0, 5],
[0, 0, 7, 1, 0, 0, 7, 7]])
In [43]: b
Out[43]:
<5x8 sparse matrix of type '<class 'numpy.int64'>'
with 20 stored elements in COOrdinate format>
In [44]: b.A
Out[44]:
array([[0, 0, 0, 7, 9, 0, 5, 0],
[0, 7, 0, 0, 6, 6, 0, 0],
[3, 0, 2, 0, 3, 0, 0, 0],
[5, 0, 0, 3, 0, 0, 7, 0],
[8, 0, 6, 8, 0, 0, 4, 0]])
Compute the element-wise product of a
and b
. Note that c
uses the CSR format.
In [45]: c = a.multiply(b)
In [46]: c
Out[46]:
<5x8 sparse matrix of type '<class 'numpy.int64'>'
with 7 stored elements in Compressed Sparse Row format>
In [47]: c.A
Out[47]:
array([[ 0, 0, 0, 63, 0, 0, 30, 0],
[ 0, 0, 0, 0, 6, 0, 0, 0],
[ 0, 0, 0, 0, 6, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 42, 8, 0, 0, 28, 0]], dtype=int64)
Verify the result by computing the element-wise product of the corresponding numpy arrays.
In [48]: a.A * b.A
Out[48]:
array([[ 0, 0, 0, 63, 0, 0, 30, 0],
[ 0, 0, 0, 0, 6, 0, 0, 0],
[ 0, 0, 0, 0, 6, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 42, 8, 0, 0, 28, 0]])