Python Scipy:稀疏矩阵支持高级索引吗?

Python Scipy:稀疏矩阵支持高级索引吗?,python,numpy,scipy,Python,Numpy,Scipy,没问题: >>> 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]]) 大问题: >>> s = scipy.sparse.csr_matrix(t) >>

没问题:

>>> 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]])
大问题:

>>> 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=scipy.sparse.csr\u矩阵(t)
>>>打印(s[[x]].toarray(),s[[y]].toarray())
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
:               :
:               :
ValueError:数据、索引和indptr应为排名1

s.toarray()[[x]]
效果很好,但由于数组太大,无法实现使用稀疏矩阵的全部目的。我已经检查了与一些稀疏矩阵相关联的属性和方法,以查找任何引用高级索引的内容,但没有骰子。有什么想法吗?

稀疏矩阵的索引支持非常有限,可用的内容取决于矩阵的格式

例如:

>>> 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__'
还有

>>> 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>
>a=scipy.sparse.rand(100100,format='dok')
>>>a[2:5:2,6:8:3]
回溯(最近一次呼叫最后一次):
...
NotImplementedError:仅支持一个轴上的索引
>>>a[2:5:2,1]
甚至

>>> 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>
>a=scipy.sparse.rand(100100,format='lil')
>>>a[2:5:2,1]
C:\Python27\lib\site packages\scipy\sparse\lil.py:230:SparseEfficiencyWarning:索引到具有多个索引的lil_矩阵的速度很慢。预先转换为CSC或CSR更有效。
sparseeficiency(警告)
>>>a[2:5:2,6:8:3]

那么你的意思是……只有上面概述的索引可以工作,而我的方法不行?@NoobSaibot是的,我认为上面总结了稀疏矩阵的索引可能性。我几乎可以肯定,使用2D数组进行索引对于任何格式都不起作用,尽管我相信LIL将使用两个1D数组进行索引,并返回一个行向量。也可能有用。为什么还要加上一对方括号?他们的逻辑是不可靠的,numpy只是碰巧通过推理忽略了这一点。除此之外,只尝试一维奇特索引,这些是矩阵,高维奇特索引很可能会杀死你的二维矩阵。@seberg:上面的例子只是为了说明语法。在我的实际代码中,我需要高级索引在需要时调用特定的行(即[t[1],t[5],t[6]),而不是范围或切片。是的,但是添加额外的对也可以解释为
t[np.array([x])]
而不是
t[x,]
向单个索引添加额外维度。我不相信稀疏索引能够像您希望的那样处理这种情况。
>>> 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>
>>> 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>