[SciPy-dev] feedback on scipy.sparse

Nathan Bell wnbell@gmail....
Wed Dec 12 18:40:18 CST 2007

On Dec 12, 2007 4:03 AM, Stefan van der Walt <stefan@sun.ac.za> wrote:
> > I'd like to know if some fancy indexing will be available soon. I explain
> > myself. I need to populate a sparse matrix with some weights for each line. I'd
> > like to do s[i, indices] = weights but it does not seem to work. I could use a
> > loop, but it would be slower and this is not acceptable (as it is possible to
> > do so in Matlab).
> The lil_matrix is used to construct matrices like that:

Matthieu, if you know all the row and column indices and their
corresponding values then you can use the coo_matrix format like this:

In [1]: from scipy import *
In [2]: from scipy.sparse import *
In [3]: row = array([0,1,2,2,1])
In [4]: col = array([1,2,0,1,1])
In [5]: data = array([1,2,3,4,5])
In [6]: A = coo_matrix((data,(row,col)),dims=(3,3))
In [7]: A.todense()
matrix([[0, 1, 0],
        [0, 5, 2],
        [3, 4, 0]])

Using COO in this manner should be 10x-100x faster than LIL.  LIL and
DOK are the only options that allow efficient insertion into a sparse
matrix, however if you know all the entries a priori then COO is much

Nathan Bell wnbell@gmail.com

More information about the Scipy-dev mailing list