[SciPy-user] sparse matrices - list assignment to rows and columns
Gergely Imreh
imrehg@gmail....
Wed Apr 8 22:51:40 CDT 2009
Hi,
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 [1]:
from scipy import sparse
Asp = sparse.lil_matrix((50000,50000))
Asp.setdiag(ones(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.
Cheers!
Greg
[1] http://www.scipy.org/SciPy_Tutorial
More information about the SciPy-user
mailing list