[SciPy-user] more help with sparse matrix: creating

Stefan van der Walt stefan at sun.ac.za
Mon Oct 8 16:27:10 EDT 2007


On Mon, Oct 08, 2007 at 07:26:03PM +0100, Robin wrote:
> Actually the other issue is that you can't specify dtype with pysparse as far
> as I can tell. My matrix contains only integer values (0 or 1) so I was hoping
> to use dtype=byte. I think it might use less ram to do the scipy.sparse way of
> allocating csr with small dtype, converting to lil to fill, then converting
> back.

This is still on the TODO list:

http://projects.scipy.org/scipy/scipy/ticket/225

> Is it correct that if I create an empty csr from scipy.sparse with a specified
> nnz and convert it lil that lil will have memory reserved for appropriate
> number of entries? (or does lil not have a notion of nnz allocation and just
> expands dynamically as needed?)

Lil stands for "list of lists".  The internal structures are not
allocated beforehand, but are grown on demand, as needed.  Take a
look:

In [69]: z = sp.lil_matrix((4,4))

In [70]: z.data
Out[70]: array([[], [], [], []], dtype=object)

In [71]: z.rows
Out[71]: array([[], [], [], []], dtype=object)

In [72]: z[:2,:2] = 4

In [73]: z.data
Out[73]: array([[array(4.0), array(4.0)], [array(4.0), array(4.0)], [], []], dtype=object)

In [74]: z.rows
Out[74]: array([[0, 1], [0, 1], [], []], dtype=object)


Hrm.  I think the output of line 73 may be a bug.  That should be

array([[4.0,4.0],[4.0,4.0],[],[]])


Cheers
Stéfan



More information about the SciPy-User mailing list