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

Stefan van der Walt stefan@sun.ac...
Mon Oct 8 15:17:36 CDT 2007


Hi Robin

On Mon, Oct 08, 2007 at 01:06:34PM +0100, Robin wrote:
> On further reading I realized I was providing the arguments incorrectly -
> specifying them by name worked.
> 
> Now I am just stuck on the part where I convert the allocated csr array to lil
> format, to fill it and then convert it back to csr. I really need this to be in
> place, since there won't be enough memory to hold two copies of the array. Is
> this possible?

It isn't necessary to first create a csr matrix.  Start by creating the
lil_matrix, fill it, and then convert to csc_array.  As you noticed,
if you don't have enough memory to fit all the data into memory twice,
this won't work.

Another route is to allocate the csr matrix directly:

z = sp.csr_matrix((3,3),nzmax=5)
z[0,0] = 3
z[1,1] = 4

etc.

You can investigate the underlying data:

In [57]: z.data
Out[57]: array([ 3.,  4.,  0.,  0.,  0.])

In [58]: z.indices
Out[58]: array([0, 1, 0, 0, 0])

Cheers
Stéfan


More information about the SciPy-user mailing list