[SciPy-User] fancy indexation of sparse matrices is terrible slow

Nathan Bell wnbell@gmail....
Tue Dec 15 03:31:02 CST 2009

2009/12/14 Dmitrey <tmp50@ukr.net>:
> I have tried all 3, lil works faster than any others, but casting to numpy
> ndarray and invoking it on the dense array obtained works several times
> faster, that is extremely important for me. Thus on my problem with sparse
> matrix of shape 151* 1178
> each time casting it to dense before applying fancy indexing makes numerical
> optimization ipopt solver works faster from 130 to 60 sec. Here's the code
> that demonstrates the difference:
> So, I have filed a ticket for it.

Hi Dmitrey,

I think what you're asking for is simply not possible.  The time spent
looking up elements in a dense matrix (a completely trivial operation)
will always be less than the lookup in sparse matrix data structures
where indexing elements is inherently more expensive.

If you want something that does the above operation really fast then you can do
>>> M = coo_matrix(M)
>>> M.data # same as M[I,J]
>>> M = csr_matrix(M)
>>> M.data # same as M[I,J]

Why do you want to index sparse matrices in the first place?  For many
operations there are better formulations that avoid the need to do
indexing of any kind.

Nathan Bell wnbell@gmail.com

More information about the SciPy-User mailing list