I have been trying to divide a python scipy sparse matrix by a vector sum of its rows. Here is my code
sparse_mat = bsr_matrix((l_data, (l_row, l_col)), dtype=float)
sparse_mat = sparse_mat / (sparse_mat.sum(axis = 1)[:,None])
However, it throws an error no matter how I try it
sparse_mat = sparse_mat / (sparse_mat.sum(axis = 1)[:,None])
File "/usr/lib/python2.7/dist-packages/scipy/sparse/base.py", line 381, in __div__
return self.__truediv__(other)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/compressed.py", line 427, in __truediv__
raise NotImplementedError
NotImplementedError
Anyone with an idea of where I am going wrong?
(sparse_mat.sum(axis = 1)[:,None]
is not a single number. – Dschonisparse_mat = sparse_mat*(1 / (sparse_mat.sum(axis = 1)[:,None]))
. It seems the division of sparse matrices is the problem. You may also have to convert the divisor to a dense arraysparse_mat = sparse_mat*(1 / (sparse_mat.sum(axis = 1).toarray()[:,None]))
– Daniel F