numpy.where(condition)的输出不是一个数组,而是一个数组的元组:为什么?

Fab*_*bio 32 python arrays numpy

我正在试验这个numpy.where(condition[, x, y])功能.
numpy文档中,我了解到如果只给出一个数组作为输入,它应该返回数组非零的索引(即"True"):

如果只给出条件,则返回元组condition.nonzero(),条件为True的索引.

但是如果尝试它,它会返回一个包含两个元素的元组,其中第一个是索引的通缉列表,第二个是null元素:

>>> import numpy as np
>>> array = np.array([1,2,3,4,5,6,7,8,9])
>>> np.where(array>4)
(array([4, 5, 6, 7, 8]),) # notice the comma before the last parenthesis
Run Code Online (Sandbox Code Playgroud)

所以问题是:为什么?这种行为的目的是什么?在什么情况下这是有用的?实际上,为了获得所需的索引列表,我必须添加索引,因为np.where(array>4)[0]它看起来像......"丑陋".


附录

我理解(从一些答案)它实际上只是一个元素的元组.我仍然不明白为什么要以这种方式提供输出.为了说明这是不理想的,请考虑以下错误(这首先激发了我的问题):

>>> import numpy as np
>>> array = np.array([1,2,3,4,5,6,7,8,9])
>>> pippo = np.where(array>4)
>>> pippo + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "int") to tuple
Run Code Online (Sandbox Code Playgroud)

所以你需要做一些索引来访问实际的索引数组:

>>> pippo[0] + 1
array([5, 6, 7, 8, 9])
Run Code Online (Sandbox Code Playgroud)

hpa*_*ulj 34

在Python中(1)意味着1. ()可以自由地添加到组号和表达式中以供人类阅读(例如(1+3)*3v (1+3,)*3).因此,表示它使用的1元素元组(1,)(并要求您也使用它).

从而

(array([4, 5, 6, 7, 8]),)
Run Code Online (Sandbox Code Playgroud)

是一个元素元组,该元素是一个数组.

如果您应用于where2d数组,结果将是2元素元组.

结果where是它可以直接插入索引槽,例如

a[where(a>0)]
a[a>0]
Run Code Online (Sandbox Code Playgroud)

应该返回相同的东西

就像那样

I,J = where(a>0)   # a is 2d
a[I,J]
a[(I,J)]
Run Code Online (Sandbox Code Playgroud)

或者用你的例子:

In [278]: a=np.array([1,2,3,4,5,6,7,8,9])
In [279]: np.where(a>4)
Out[279]: (array([4, 5, 6, 7, 8], dtype=int32),)  # tuple

In [280]: a[np.where(a>4)]
Out[280]: array([5, 6, 7, 8, 9])

In [281]: I=np.where(a>4)
In [282]: I
Out[282]: (array([4, 5, 6, 7, 8], dtype=int32),)
In [283]: a[I]
Out[283]: array([5, 6, 7, 8, 9])

In [286]: i, = np.where(a>4)   # note the , on LHS
In [287]: i
Out[287]: array([4, 5, 6, 7, 8], dtype=int32)  # not tuple
In [288]: a[i]
Out[288]: array([5, 6, 7, 8, 9])
In [289]: a[(i,)]
Out[289]: array([5, 6, 7, 8, 9])
Run Code Online (Sandbox Code Playgroud)

======================

np.flatnonzero 显示返回一个数组的正确方法,无论输入数组的大小如何.

In [299]: np.flatnonzero(a>4)
Out[299]: array([4, 5, 6, 7, 8], dtype=int32)
In [300]: np.flatnonzero(a>4)+10
Out[300]: array([14, 15, 16, 17, 18], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

它的医生说:

这相当于a.ravel().nonzero()[0]

事实上,这实际上就是函数所做的事情.

通过展平a消除了如何处理多个维度的问题.然后它从元组中获取响应,给你一个简单的数组.扁平化不会使1d阵列成为特殊情况.

===========================

@Divakar建议np.argwhere:

In [303]: np.argwhere(a>4)
Out[303]: 
array([[4],
       [5],
       [6],
       [7],
       [8]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

哪个 np.transpose(np.where(a>4))

或者,如果您不喜欢列向量,则可以再次转置它

In [307]: np.argwhere(a>4).T
Out[307]: array([[4, 5, 6, 7, 8]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

除了现在它是一个1xn数组.

我们也可以同样都包裹wherearray:

In [311]: np.array(np.where(a>4))
Out[311]: array([[4, 5, 6, 7, 8]], dtype=int32)
Run Code Online (Sandbox Code Playgroud)

大量的以阵列出来的方式where元组([0],i,=,transpose,array,等等).


jak*_*vdp 7

简答:np.where无论阵列的尺寸如何,都可以提供一致的输出.

二维数组有两个索引,因此结果np.where是包含相关索引的长度为2的元组.这推广为3维的长度为3的元组,4维的长度为4的元组,或N维的长度为N的元组.通过这个规则,很明显在1维中,结果应该是长度为1的元组.