Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError with accessing to coo_matrix by index

I have coo_matrix X and indexes trn_idx by which I would like to get access of that maxtrix

print (type(X  ), X.shape)
print (type(trn_idx), trn_idx.shape)

<class 'scipy.sparse.coo.coo_matrix'> (1503424, 2795253)
<class 'numpy.ndarray'> (1202739,)

Calling this way:

X[trn_idx]
TypeError: only integer scalar arrays can be converted to a scalar index

Either this way:

 X[trn_idx.astype(int)] #same error

How to access by index?

like image 719
Rocketq Avatar asked Jun 17 '18 17:06

Rocketq


1 Answers

The coo_matrix class does not support indexing. You'll have to convert it to a different sparse format.

Here's an example with a small coo_matrix:

In [19]: import numpy as np

In [20]: from scipy.sparse import coo_matrix

In [21]: m = coo_matrix([[0, 0, 0, 1], [2, 0, 0 ,0], [0, 0, 0, 0], [0, 3, 4, 0]])

Attempting to index m fails:

In [22]: m[0,0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-1f78c188393f> in <module>()
----> 1 m[0,0]

TypeError: 'coo_matrix' object is not subscriptable

In [23]: idx = np.array([2, 3])

In [24]: m[idx]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-24-a52866a6fec6> in <module>()
----> 1 m[idx]

TypeError: only integer scalar arrays can be converted to a scalar index

If you convert m to a CSR matrix, you can index it with idx:

In [25]: m.tocsr()[idx]
Out[25]: 
<2x4 sparse matrix of type '<class 'numpy.int64'>'
    with 2 stored elements in Compressed Sparse Row format>

If you are going to do more indexing, it would be better to save the new array in a variable, and use it as needed:

In [26]: a = m.tocsr()

In [27]: a[idx]
Out[27]: 
<2x4 sparse matrix of type '<class 'numpy.int64'>'
    with 2 stored elements in Compressed Sparse Row format>

In [28]: a[0,0]
Out[28]: 0
like image 77
Warren Weckesser Avatar answered Nov 16 '22 23:11

Warren Weckesser