Reputation: 8203
I need to prune a large number of entries from a SciPy sparse matrix. Currently I convert the matrix to the DOK format and individually assign each entry to 0.
m = m.todok()
for i, j in pruneme:
m[i,j] = 0
This is extremely slow.
Is there a faster way?
Upvotes: 3
Views: 207
Reputation: 1461
You can set elements of CSR sparse arrays efficiently, as long as you do not add new nonzeros, simply by subscripting the array with tuples:
i, j = zip(*pruneme) # assuming that pruneme is a python list
m[i, j] = 0.
m.eliminate_zeros()
That should be much faster than constructing two arrays.
Upvotes: 4