[SciPy-user] Sparse sub-matrix indexing
James Philbin
philbinj at gmail.com
Fri Oct 10 11:34:40 EDT 2008
Hi,
I've come across this inconsistency with indexing sparse matrices:
In [3]: A = np.arange(100).reshape(10,10)
In [4]: B = spsp.csr_matrix(A)
In [5]: A[np.array([2]),:][:,np.array([2])]
Out[5]: array([[22]])
In [6]: B[np.array([2]),:][:,np.array([2])]
---------------------------------------------------------------------------
<type 'exceptions.TypeError'> Traceback (most recent call last)
/home/james/<ipython console> in <module>()
/usr/lib/python2.5/site-packages/scipy/sparse/csr.py in __getitem__(self, key)
226 #[1:2,??]
227 if isintlike(col) or isinstance(col, slice):
--> 228 return self._get_submatrix(row, col) #[1:2,j]
229 else:
230 P = extractor(col,self.shape[1]).T
#[1:2,[1,2]]
/usr/lib/python2.5/site-packages/scipy/sparse/csr.py in
_get_submatrix(self, row_slice, col_slice)
355
356 i0, i1 = process_slice( row_slice, M )
--> 357 j0, j1 = process_slice( col_slice, N )
358 check_bounds( i0, i1, M )
359 check_bounds( j0, j1, N )
/usr/lib/python2.5/site-packages/scipy/sparse/csr.py in process_slice(sl, num)
346
347 else:
--> 348 raise TypeError('expected slice or scalar')
349
350 def check_bounds( i0, i1, num ):
<type 'exceptions.TypeError'>: expected slice or scalar
However, this works fine:
In [7]: B[np.array([2,3]),:][:,np.array([2,3])]
Out[7]:
<2x2 sparse matrix of type '<type 'numpy.float64'>'
with 4 stored elements in Compressed Sparse Row format>
Also, this works fine:
In [8]: B[[2],:][:,[2]]
Out[8]:
<1x1 sparse matrix of type '<type 'numpy.float64'>'
with 1 stored elements in Compressed Sparse Row format>
Thanks,
James
More information about the SciPy-User
mailing list