Adding a numpy array to scipy.sparse.dok_matrix

I have scipy.sparse.dok_matrix (dimensions mxn), wanting to add a flat numpy array with length m.

 for col in xrange(n): dense_array = ... dok_matrix[:,col] = dense_array 

However, this code throws an exception in dok_matrix.__setitem__ when trying to delete a non-existing key ( del self[(i,j)] ).

So, at the moment I am doing this unevenly:

 for col in xrange(n): dense_array = ... for row in dense_array.nonzero(): dok_matrix[row, col] = dense_array[row] 

It seems very inefficient. So what is the most efficient way to do this?

Thanks!

+4
source share
2 answers

I am surprised that your unbalanced method does not have the same problems as the fragment. It looks like a bug to me, looking at the Scipy code. When you try to set a specific row and column in dok_matrix to zero, when it is already zero, an error occurs because it tries to delete the value in that row and column without checking if it exists.

In answer to your question, what you are doing in your own private key is exactly what the __setitem__ method is currently doing with your elegant method (after a few checks, isinstance and what not). If you want to use the elegant way, you can fix the error that I mentioned in your own Scipy package by opening dok.py in Lib/site-packages/scipy/sparse/ and changing line 222 from

 if value==0: 

to

 if value==0 and self.has_key((i,j)): 

Then you can use the elegant way, and it should work fine. I went to fix the error, but it has already been fixed for the next version, and it has been fixed.

+2
source

I think this bug was fixed in Scipy 0.8.0

+1
source

Source: https://habr.com/ru/post/1307444/


All Articles