Noo*_*bot 8 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]])
Run Code Online (Sandbox Code Playgroud)
大问题:
>>> 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
Run Code Online (Sandbox Code Playgroud)
s.toarray()[[x]]效果很好,但由于我的数组太大,使用稀疏矩阵打败了我的整个目的.我已经检查了与某些稀疏矩阵相关的属性和方法,以获取引用高级索引的任何内容,但没有骰子.有任何想法吗?
Jai*_*ime 14
稀疏矩阵具有非常有限的索引支持,可用的内容取决于矩阵的格式.
例如:
>>> 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__'
Run Code Online (Sandbox Code Playgroud)
但
>>> 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>
Run Code Online (Sandbox Code Playgroud)
虽然
>>> a[2:5:2, 6:8:3]
Traceback (most recent call last):
...
ValueError: slicing with step != 1 not supported
Run Code Online (Sandbox Code Playgroud)
还有
>>> 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>
Run Code Online (Sandbox Code Playgroud)
乃至
>>> 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>
Run Code Online (Sandbox Code Playgroud)