Scipy: Do sparse matrices support advanced indexing?

前端 未结 1 1473
不思量自难忘°
不思量自难忘° 2020-12-19 05:22

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.ar         


        
1条回答
  •  离开以前
    2020-12-19 05:38

    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 "", line 1, in 
    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 ''
        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 ''
        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 ''
        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 ''
        with 0 stored elements in LInked List format>
    

    0 讨论(0)
提交回复
热议问题