[SciPy-user] sparse matrices - list assignment to rows and columns
Thu Apr 9 03:51:01 CDT 2009
On Wed, Apr 8, 2009 at 11:51 PM, Gergely Imreh <email@example.com> wrote:
> I was trying figure out the scipy sparse matrix handling, but run
> into some difficulties assigning a list of values to rows and columns.
> The scipy tutorial has the following example :
> from scipy import sparse
> Asp = sparse.lil_matrix((50000,50000))
> Asp[20,100:250] = 10*random.rand(150)
> Asp[200:250,30] = 10*random.rand(50)
> That looks straightforward enough, make a large, diagonal sparse
> matrix, and set some additional elements to non-zero. What I get,
> however, is different:
> Asp[20,100:250] = 10*random.rand(150) sets the matrix elements at row
> 20, column 100-249 to random values.
> Asp[200:250,30] = 10*random.rand(50) sets the matrix element at row
> 200, column 30 to a 50 element row vector with random values....
> (elements at row 201-249, column 30 are still 0)
> If I reshape the results of random.rand(50) to be in a column
> instead of row, the assignment will results in the elements of row
> 200-249, column 30 to be set to a single element array values (So, for
> exaple Asp[200,30] will be an array, which will have a single random
> value at [0,0])
> I'm using Python 2.6 (that comes with my distro, or 2.4 for which
> I'd have to recompile scipy) and scipy 0.7.0. Is this kind of
> behaviour due to the changes (and incompatibilites) of 2.6 (since I
> know scipy is writtend to be compatible up to 2.5) or something else?
> The other sparse matrix types would handle this differently?
> A workaround is to do single element assignments but I'd think
> that's probably slower in general.
>  http://www.scipy.org/SciPy_Tutorial
There is an assignment error:
Asp[200:250,30] seems to assign all 50 elements to to the position Asp[200,30]
<1x50 sparse matrix of type '<type 'numpy.float64'>'
with 50 stored elements in LInked List format>
>>> Aspc = Asp.tocrc()
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
Aspc = Asp.tocrc()
File "c:\josef\_progs_scipy\scipy\sparse\base.py", line 429, in __getattr__
raise AttributeError, attr + " not found"
AttributeError: tocrc not found
this is with
there is a related assignment error that got fixed in trunk,
I don't know if it also handles this case, a bug report might be
useful to make sure this case is handled correctly
I think, for this example dok format would be better to build the
matrix, since column slices need to access many lists
Asp = sparse.dok_matrix((50000,50000))
Aspr = Asp.tocsr()
works without problems
I checked the history of the scipy tutorial that you linked to, the
main editing has been done in 2006, and maybe it isn't up to date.
The current docs are being written and are available at
More information about the SciPy-user