[SciPy-dev] Sparse matrix indexing.
Sun Apr 6 22:22:06 CDT 2008
We are working to convert Circuitscape (a landscape ecology tool) from
MATLAB to Python.
It exercises a lot of sparse array functionality (indexing,
assignment, concatenation, and MATLAB sparse()/find(), and sparse
linear algebra). I am new to Numpy/Scipy, and I'd be delighted to
figure out if there were a better way to do sparse matrix indexing.
The general array indexing didn't seem to work for arbitrary index
sets, at least in the latest release.
I did come across this on scipy-dev, but it seems like it works for
only CSR ?
I currently use a wrapper that can be used to implement general sparse
matrix indexing and row/col deletion using sparse matrix
multiplication. It implements sparse indexing. Deletion, is then
implemented by simply taking arange(0,len) and removing the indices
you want to delete, and then calling the indexing code.
This may be a useful way to complete the functionality for data
structures that may not have full indexing yet. I'm not sure if this
can be made to handle slices correctly. This is the code snippet I
use. If its useful, I can help make it robust for inclusion.
# Implement B = A[I, J]
nr = A.shape
nc = A.shape
nI = len(I)
nJ = len(J)
IM = sparse.csc_matrix((ones(nI), c_[arange(0,nI), I].T),
JM = sparse.csc_matrix((ones(nJ), c_[J, arange(0,nJ)].T),
B = IM*A*JM
More information about the Scipy-dev