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

Robin robince@gmail....
Mon Oct 8 06:10:08 CDT 2007

```On 10/8/07, Robin <robince@gmail.com> wrote:

> How would I achieve a similar thing in numpy (to first allocate with fixed
> nnz, then fill in sections), preferably with slicing/fancy indexing?
> (I was reading about lil_matrix etc. but it wasn't clear to me how to do
> the above)

So as I understand to allocate the sparse array with a specified nnz I have
to use sparse.csr_matrix, since lil_matrix constructor doesn't seem to
support this.

Then I need to convert my allocated csr matrix  in place to lil format so
that I can do fancy indexing to fill it as required  - how can I do this in
place without making another copy of the data?
Once filled I need to convert back to csr again to do matrix vector
multiplication and save to matlab file.

Does this seem correct?

Unfortunately I have a problem with the first step, trying to allocate an
empty csr matrix:
>From the doc string:
- csr_matrix((M, N), [nzmax, dtype])
to construct a container, where (M, N) are dimensions and
nzmax, dtype are optional, defaulting to nzmax=sparse.NZMAX
and dtype='d'.

However when I try to include the nzmax argument I get the following error:

In [102]: B = sparse.csr_matrix((10,10),[10,'b'])
---------------------------------------------------------------------------
<type 'exceptions.NotImplementedError'>   Traceback (most recent call last)

/home/robince/<ipython console> in <module>()

/usr/lib/python2.5/site-packages/scipy/sparse/sparse.py in __init__(self,
arg1, dims, nzmax, dtype, copy, check)
1307                 N = max(oldN, N)
1308
-> 1309         self.shape = (M, N)
1310
1311         self._check(check)

/usr/lib/python2.5/site-packages/scipy/sparse/sparse.py in set_shape(self,
shape)
111             except NotImplementedError:
112                 raise NotImplementedError("Reshaping not implemented
for %s." %
--> 113                                           self.__class__.__name__)
114         self._shape = shape
115

<type 'exceptions.NotImplementedError'>: Reshaping not implemented for
csr_matrix.

Am I doing something wrong? Is there a better way to achieve what I'm trying
to do?

Thanks,

Robin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20071008/2c4b83f5/attachment.html
```