numpy.where(condition)的输出不是数组,而是数组的元组:为什么? [英] output of numpy.where(condition) is not an array, but a tuple of arrays: why?

查看:89
本文介绍了numpy.where(condition)的输出不是数组,而是数组的元组:为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy.where(condition[, x, y])函数.
numpy文档中,我了解到如果您只给一个数组作为输入,它将返回数组非零的索引(即"True"):

I am experimenting with the numpy.where(condition[, x, y]) function.
From the numpy documentation, I learn that if you give just one array as input, it should return the indices where the array is non-zero (i.e. "True"):

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

If only condition is given, return the tuple condition.nonzero(), the indices where condition is True.

但是如果尝试,它会返回两个元素的 tuple ,其中第一个是所需的索引列表,第二个是空元素:

But if try it, it returns me a tuple of two elements, where the first is the wanted list of indices, and the second is a null element:

>>> 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

所以问题是:为什么?这种行为的目的是什么?在什么情况下这很有用? 确实,要获取所需的索引列表,我必须添加索引,如np.where(array>4)[0]所示,这似乎是丑陋的".

so the question is: why? what is the purpose of this behaviour? in what situation this is useful? Indeed, to get the wanted list of indices I have to add the indexing, as in np.where(array>4)[0], which seems... "ugly".

附录

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

I understand (from some answers) that it is actually a tuple of just one element. Still I don't understand why to give the output in this way. To illustrate how this is not ideal, consider the following error (which motivated my question in the first place):

>>> 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

,因此您需要做一些索引操作才能访问实际的索引数组:

so that you need to do some indexing to access the actual array of indices:

>>> pippo[0] + 1
array([5, 6, 7, 8, 9])

推荐答案

在Python中,(1)仅表示1.可以自由地将()添加到组号和表达式中以提高可读性(例如(1+3)*3 v (1+3,)*3).因此,要表示一个1元素元组,它使用(1,)(并且也需要使用它).

In Python (1) means just 1. () can be freely added to group numbers and expressions for human readability (e.g. (1+3)*3 v (1+3,)*3). Thus to denote a 1 element tuple it uses (1,) (and requires you to use it as well).

因此

(array([4, 5, 6, 7, 8]),)

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

is a one element tuple, that element being an array.

如果将where应用于2d数组,结果将是2个元素的元组.

If you applied where to a 2d array, the result would be a 2 element tuple.

where的结果是可以将其直接插入到索引槽中,例如

The result of where is such that it can be plugged directly into an indexing slot, e.g.

a[where(a>0)]
a[a>0]

应该返回相同的内容

I,J = where(a>0)   # a is 2d
a[I,J]
a[(I,J)]

或以您的示例为例:

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])

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

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

np.flatnonzero显示了仅返回一个数组的正确方法,而不管输入数组的尺寸如何.

np.flatnonzero shows the correct way of returning just one array, regardless of the dimensions of the input array.

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)

医生说:

这等效于a.ravel().nonzero()[0]

This is equivalent to a.ravel().nonzero()[0]

实际上,这就是函数的作用.

In fact that is literally what the function does.

通过展平a,消除了如何处理多个尺寸的问题.然后,它将响应从元组中删除,从而为您提供一个简单的数组.通过展平,它对于一维数组并没有特殊的要求.

By flattening a removes the question of what to do with multiple dimensions. And then it takes the response out of the tuple, giving you a plain array. With flattening it doesn't have make a special case for 1d arrays.

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

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

@Divakar建议np.argwhere:

@Divakar suggests np.argwhere:

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

具有np.transpose(np.where(a>4))

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

Or if you don't like the column vector, you could transpose it again

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

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

except now it is a 1xn array.

我们也可以将where包装在array中:

We could just as well have wrapped where in array:

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

where元组([0]i,=transposearray等)中提取数组的很多方法.

Lots of ways of taking an array out the where tuple ([0], i,=, transpose, array, etc).

这篇关于numpy.where(condition)的输出不是数组,而是数组的元组:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆