numpy返回空数组 [英] Numpy where returns empty array
问题描述
我有一个数组,例如
a = [5,1,3,0,2]
我应用where函数:
I apply the where function:
np.where(a == 2)
输出为空数组
(array([], dtype=int64),)
我发现了同样的问题在这里,但就我而言,这真的没有任何意义或影响吗?
I found kind of the same problem here, but in my case it really dosen't make any sense or dose it?
Btw。我在使用Python 2.7.10的Mac上
Btw. i'm on a Mac using Python 2.7.10
推荐答案
您要将列表传递到 ()
函数而不是Numpy数组。改用数组:
You're passing a list to where()
function not a Numpy array. Use an array instead:
In [20]: a = np.array([5,1,3,0,2])
In [21]: np.where(a == 2)
Out[21]: (array([4]),)
也如注释中所述,在这种情况下, a == 2
是 False
,这是传递给 where
的值。如果 a
是一个numpy数组,则 a == 2
的值是一个numpy的布尔数组,并且 where
函数将为您提供所需的结果。
Also as mentioned in comments, in this case the value of a == 2
is False
, and this is the value passed to where
. If a
is a numpy array, then the value of a == 2
is a numpy array of bools, and the where
function will give you the desire results.
这篇关于numpy返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!