8
votes

No problem:

>>> t = np.array([[1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5]])
>>> x = np.arange(5).reshape((-1,1)); y = np.arange(5)
>>> print (t[[x]],t[[y]])

Big problem:

>>> s = scipy.sparse.csr_matrix(t)
>>> print (s[[x]].toarray(),s[[y]].toarray())
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
:               :
:               :
ValueError: data, indices, and indptr should be rank 1

s.toarray()[[x]] works great, but defeats the whole purpose of me using sparse matrices as my arrays are too big. I've checked the Attributes and Methods associated with some of the sparse matrices for anything referencing Advanced Indexing, but no dice. Any ideas?

1
Why do you put an additional pair of square brackets? They are shaky logic, that numpy just happens to ignore by its reasoning. Other then that, try 1-d fancy indexes only, these are matrices, higher dimensional fancy indexing would kill your 2-d matrix anyway likely.seberg
@seberg: The example above is just to illustrate the advanced indexing syntax. In my real code, i need advanced indexing to call on specific rows when needed (i.e., [t[1], t[5], t[6]]) rather than the range or slices.Noob Saibot
Yes, but adding that extra pair could be interpreted also as t[np.array([x])] instead of t[x,] adding an extra dimension to the individual index. And I would not trust the sparse indexing to handle this case necessarily as you want it to.seberg

1 Answers

17
votes

sparse matrices have a very limited indexing support, and what is available depends on the format of the matrix.

For example:

>>> a = scipy.sparse.rand(100,100,format='coo')
>>> a[2:5, 6:8]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'coo_matrix' object has no attribute '__getitem__'

but

>>> a = scipy.sparse.rand(100,100,format='csc')
>>> a[2:5, 6:8]
<3x2 sparse matrix of type '<type 'numpy.float64'>'
    with 0 stored elements in Compressed Sparse Column format>

although

>>> a[2:5:2, 6:8:3]
Traceback (most recent call last):
...
ValueError: slicing with step != 1 not supported

There is also

>>> a = scipy.sparse.rand(100,100,format='dok')
>>> a[2:5:2, 6:8:3]
Traceback (most recent call last):
...
NotImplementedError: fancy indexing supported over one axis only
>>> a[2:5:2,1]
<3x1 sparse matrix of type '<type 'numpy.float64'>'
    with 0 stored elements in Dictionary Of Keys format>

And even

>>> a = scipy.sparse.rand(100,100,format='lil')
>>> a[2:5:2,1]
<2x1 sparse matrix of type '<type 'numpy.int32'>'
    with 0 stored elements in LInked List format>
C:\Python27\lib\site-packages\scipy\sparse\lil.py:230: SparseEfficiencyWarning: Indexing into a lil_matrix with multiple indices is slow. Pre-converting to CSC or CSR beforehand is more efficient.
  SparseEfficiencyWarning)
>>> a[2:5:2, 6:8:3]
<2x1 sparse matrix of type '<type 'numpy.int32'>'
    with 0 stored elements in LInked List format>