[SciPy-user] sparse matrices -- slicing and fancy indexing for assignment

Christopher MacMinn cmac@mit....
Tue Sep 30 09:09:52 CDT 2008

Hey folks -

I installed SciPy 0.7.0.dev4753 from svn yesterday to get access to  
some of the upcoming improvements to scipy.sparse.

In particular, I was excited about the prospect of slicing and fancy  
indexing for CSC and CSR matrices.  Unfortunately, I find now that  
slicing and fancy indexing are only supported for viewing and copying,  
not for assignment.  E.g.:

# Create a sparse CSC matrix A

 >>> from scipy import sparse
 >>> A = sparse.eye(10,10,format="csc")

# Have a look at the first row of A

 >>> print A[0,:].todense()
[[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]

# Now try to set the first row equal to 5

 >>> A[0,:] = 5.
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/Library/Python/2.5/site-packages/scipy/sparse/ 
compressed.py", line 601, in __setitem__
     raise NotImplementedError("Fancy indexing in assignment not "
NotImplementedError: Fancy indexing in assignment not supported for  
csr matrices.

# Fail.

Is slicing / fancy indexing for assignment planned for the final 0.7.0  
release, or is this (above) the best we're going to get?

Best, Chris MacMinn

More information about the SciPy-user mailing list