[英]numpy array subsetting error
I have a numpy sparse matrix A1 of type 我有一个类型的numpy稀疏矩阵A1
scipy.sparse.dok.dok_matrix
with integer values. 与整数值。 I'm trying to use it to subset another integer matrix A2 of type
我正在尝试使用它来子集另一类型的整数矩阵A2
numpy.matrixlib.defmatrix.matrix
by 通过
A2[A1>0]
Both of them have shape (1,10000). 它们都具有形状(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
IndexError
is telling that A1 > 0
is not an object compatible with indexing. IndexError
告诉A1 > 0
不是与索引兼容的对象。 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()
: 您可以通过使用
toarray()
首先将A1转换为数组来生成bool
数组:
In []: type(A1.toarray() > 0)
Out[]: numpy.ndarray
Then A2[A1.toarray() > 0]
should work just fine. 然后
A2[A1.toarray() > 0]
应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.