I'm wondering whether there is a way to simply add a dense vector to all the rows of a sparse matrix represented as a csr_matrix
in scipy.sparse
and returning a sparse matrix, ie trying to sum only the non zero element of the sparse matrix.
If I do something like this:
import numpy as np
import scipy.sparse as sp
W = sp.csr_matrix(np.array([[0,0,1],[0,1,0]]))
v = np.array([2,3,4])
and then
sum = W + v
sum
is obviously a dense matrix but with the zero numbers summed too. While, when I try to do:
b = (W != 0)
s = b.multiply(v)
sum = W + s
I obtain the correct result, but as a dense matrix. b
is a sparse matrix, but s
is not.
b == True
andAttributeError: 'bool' object has no attribute 'multiply'
. – Fred Foo