Search code examples
pythonarraysnumpysparse-matrix

numpy array subsetting error


I have a numpy sparse matrix A1 of type

 scipy.sparse.dok.dok_matrix

with integer values. I'm trying to use it to subset another integer matrix A2 of type

 numpy.matrixlib.defmatrix.matrix

by

 A2[A1>0]

Both of them have shape (1,10000). Although it works well to use

 A1[A1>0]

I get the following error:

>> A2[A1>0]

Traceback (most recent call last):

File "<ipython-input-250-19959d659dc5>", line 1, in <module>
  edge_counts[nodes>0]

File "//anaconda/lib/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 284, in __getitem__
out = N.ndarray.__getitem__(self, index)

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Solution

  • IndexError is telling that A1 > 0 is not an object compatible with indexing. You can investigate easily with:

    In []: type(A1 > 0)
    Out[]: scipy.sparse.csr.csr_matrix
    

    And you can turn in to a bool array by converting A1 to an array first, using toarray():

    In []: type(A1.toarray() > 0)
    Out[]: numpy.ndarray
    

    Then A2[A1.toarray() > 0] should work just fine.