To expand on converting a coo
matrix to csr
to index, here are some timings for a small sparse matrix
Make the matrix
In [158]: M=sparse.coo_matrix([[0,1,2,0,0],[0,0,0,1,0],[0,1,0,0,0]])
In [159]: timeit M[1,2]
TypeError: 'coo_matrix' object is not subscriptable
In [160]: timeit M.tocsc()[1,2]
1000 loops, best of 3: 375 µs per loop
In [161]: timeit M.tocsr()[1,2]
1000 loops, best of 3: 241 µs per loop
In [162]: timeit M.todok()[1,2]
10000 loops, best of 3: 65.8 µs per loop
In [163]: timeit M.tolil()[1,2]
1000 loops, best of 3: 270 µs per loop
Apparently for selecting a single element, dok
, is fastests (counting the conversion time). This format is actually a dictionary, which of course has fast element access.
But if you are frequently accessing whole rows, or whole columns, or iterating over rows or columns, you need to read the docs more carefully, and may be do your own time tests of typical arrays.
If you are setting values, not just reading them, timings and even implementation can be different. You will get an efficiency warning if you try to change a 0
element of a csr
or csc
format.