简体   繁体   English

numpy.where()究竟是如何选择此示例中的元素的?

[英]How exactly does numpy.where() select the elements in this example?

From numpy docs 来自numpy docs

>>> np.where([[True, False], [True, True]],
    ...          [[1, 2], [3, 4]],
    ...          [[9, 8], [7, 6]])
array([[1, 8],
       [3, 4]])

Am I right in assuming that the [[True, False], [True, True]] part is the condition and [[1, 2], [3, 4]] and [[9, 8], [7, 6]] are x and y respectively according to the docs parameters. 假设[[True, False], [True, True]]部分是条件[[1, 2], [3, 4]][[9, 8], [7, 6]] [[True, False], [True, True]]我是正确的吗? [[9, 8], [7, 6]]分别是根据docs参数的x和y。

Then how exactly is the function choosing the elements in the following examples? 那么在以下示例中选择元素的功能究竟如何呢?

Also, why is the element type in these examples a list? 另外,为什么这些例子中的元素类型是一个列表?

>>> np.where([[True, False,True], [False, True]], [[1, 2,56], [3, 4]], [[9, 8,79], [7, 6]])
array([list([1, 2, 56]), list([3, 4])], dtype=object)
>>> np.where([[False, False,True,True], [False, True]], [[1, 2,56,69], [3, 4]], [[9, 8,90,100], [7, 6]])
array([list([1, 2, 56, 69]), list([3, 4])], dtype=object)

In the first case, each term is a (2,2) array (or rather list that can be made into such an array). 在第一种情况下,每个术语是(2,2)数组(或者更确切地说,列表可以制成这样的数组)。 For each True in the condition, it returns the corresponding term in x , the [[1 -][3,4]] , and for each False , the term from y [[- 8][- -]] 对于条件中的每个True ,它返回x的相应项, [[1 -][3,4]] ,并且对于每个False ,来自y [[- 8][- -]]

In the second case, the lists are ragged 在第二种情况下,列表是不规则的

In [1]: [[True, False,True], [False, True]]
Out[1]: [[True, False, True], [False, True]]
In [2]: np.array([[True, False,True], [False, True]])
Out[2]: array([list([True, False, True]), list([False, True])], dtype=object)

the array is (2,), with 2 lists. 数组是(2,),有2个列表。 And when cast as boolean, a 2 element array, with both True. 当转换为boolean时,一个2元素数组,同时为True。 Only an empty list would produce False. 只有空列表才会产生False。

In [3]: _.astype(bool)
Out[3]: array([ True,  True])

The where then returns just the x values. 然后where返回x值。

This second case is understandable, but pathological. 这第二种情况是可以理解的,但是病态的。

more details 更多细节

Let's demonstrate where in more detail, with a simpler case. 我们来演示where更详细,用简单的情况。 Same condition array: 相同的条件数组:

In [57]: condition = np.array([[True, False], [True, True]])
In [58]: condition
Out[58]: 
array([[ True, False],
       [ True,  True]])

The single argument version, which is the equivalent to condition.nonzero() : 单个参数版本,相当于condition.nonzero()

In [59]: np.where(condition)
Out[59]: (array([0, 1, 1]), array([0, 0, 1]))

Some find it easier to visualize the transpose of that tuple - the 3 pairs of coordinates where condition is True: 有些人发现更容易想象该元组的transpose - condition为True的3对坐标:

In [60]: np.argwhere(condition)
Out[60]: 
array([[0, 0],
       [1, 0],
       [1, 1]])

Now the simplest version with 3 arguments, with scalar values. 现在是带有3个参数的最简单版本,带有标量值。

In [61]: np.where(condition, True, False)   # same as condition
Out[61]: 
array([[ True, False],
       [ True,  True]])
In [62]: np.where(condition, 100, 200)
Out[62]: 
array([[100, 200],
       [100, 100]])

A good way of visualizing this action is with two masked assignments. 可视化此操作的一种好方法是使用两个蒙版分配。

In [63]: res = np.zeros(condition.shape, int)
In [64]: res[condition] = 100
In [65]: res[~condition] = 200
In [66]: res
Out[66]: 
array([[100, 200],
       [100, 100]])

Another way to do this is to initial an array with the y value(s), and where the nonzero where to fill in the x value. 另一种方法是使用y值(s)初始化一个数组,并且非零填充x值的位置。

In [69]: res = np.full(condition.shape, 200)
In [70]: res
Out[70]: 
array([[200, 200],
       [200, 200]])
In [71]: res[np.where(condition)] = 100
In [72]: res
Out[72]: 
array([[100, 200],
       [100, 100]])

If x and y are arrays, not scalars, this masked assignment will require refinements, but hopefully for a start this will help. 如果xy是数组,而不是标量,那么这个被屏蔽的赋值将需要改进,但希望一开始这将有所帮助。

np.where(condition,x,y) It checks the condition and if its True returns x else it returns y np.where(condition,x,y)它检查条件,如果它的True返回x,则返回y

np.where([[True, False], [True, True]], [[1, 2], [3, 4]], [[9, 8], [7, 6]])

Here you condition is [[True, False], [True, True]] x = [[1 , 2] , [3 , 4]] y = [[9 , 8] , [7 , 6]] 这里你的条件是[[True, False], [True, True]] x = [[1 , 2] , [3 , 4]] y = [[9 , 8] , [7 , 6]]

First condition is true so it return 1 instead of 9 第一个条件为真,因此它返回1而不是9

Second condition is false so it returns 8 instead of 2 第二个条件是false,因此返回8而不是2

After reading about broadcasting as @hpaulj suggested I think I know how the function works. 在阅读了关于广播的内容后 ,@ hpaulj建议我想我知道这个功能是如何工作的。 It will try to broadcast the 3 arrays,then if the broadcast was successful it will use the True and False values to pick elements either from x or y. 它将尝试广播3个数组,然后如果广播成功,它将使用TrueFalse值从x或y中选择元素。 In the example 在示例中

>>>np.where([[True, False,True], [False, True]], [[1, 2,56], [3, 4]], [[9, 8,79], [7, 6]])

We have 我们有

cnd=np.array([[True, False,True], [False, True]])
x=np.array([[1, 2,56], [3, 4]])
y=np.array([[9, 8,79], [7, 6]])

Now 现在

>>>x.shape
Out[7]: (2,)
>>>y.shape
Out[8]: (2,)
>>>cnd.shape
Out[9]: (2,)

So all three are just arrays with 2 elements(of type list) even the condition(cnd).So both [True, False,True] and [False, True] will be evaluated as True .And both the elements will be selected from x. 所以这三个都只是具有2个元素(类型列表)的数组,甚至是条件(cnd)。因此, [True, False,True][False, True]都将被评估为True 。两个元素都将从中选择X。

>>>np.where([[True, False,True], [False, True]], [[1, 2,56], [3, 4]], [[9, 8,79], [7, 6]])
Out[10]: array([list([1, 2, 56]), list([3, 4])], dtype=object)

I also tried it with a more complex example(a 2x2x2 broadcast) and it still explains it. 我也尝试了一个更复杂的例子( 2x2x2广播),它仍然解释了它。

np.where([[[True,False],[True,True]], [[False,False],[True,False]]],
          [[[12,45],[10,50]], [[100,10],[17,81]]],
          [[[90,93],[85,13]], [[12,345], [190,56,34]]])

Where 哪里

cnd=np.array([[[True,False],[True,True]], [[False,False],[True,False]]])
x=np.array([[[12,45],[10,50]], [[100,10],[17,81]]])
y=np.array( [[[90,93],[85,13]], [[12,345], [190,56,34]]])

Here cnd and x have the shape (2,2,2) and y has the shape (2,2) . 这里cndx具有形状(2,2,2)并且y具有形状(2,2)

>>>cnd.shape
Out[14]: (2, 2, 2)
>>>x.shape
Out[15]: (2, 2, 2)
>>>y.shape
Out[16]: (2, 2)

Now as @hpaulj commented y will be broadcasted to (2,2,2). 现在,作为@hpaulj评论y将被广播到(2,2,2)。 And it'll probably look like this 它可能看起来像这样

>>>cnd
Out[6]: 
array([[[ True, False],
        [ True,  True]],
       [[False, False],
        [ True, False]]]) 
>>>x
Out[7]: 
array([[[ 12,  45],
        [ 10,  50]],
       [[100,  10],
        [ 17,  81]]])
>>>np.broadcast_to(y,(2,2,2))
Out[8]: 
array([[[list([90, 93]), list([85, 13])],
        [list([12, 345]), list([190, 56, 34])]],
       [[list([90, 93]), list([85, 13])],
        [list([12, 345]), list([190, 56, 34])]]], dtype=object)

And the result can be easily predicted to be 结果很容易预测

>>>np.where([[[True,False],[True,True]], [[False,False],[True,False]]], [[[12,45],[10,50]], [[100,10],[17,81]]],[[[90,93],[85,13]], [[12,345], [190,56,34]]])
Out[9]: 
array([[[12, list([85, 13])],
        [10, 50]],
       [[list([90, 93]), list([85, 13])],
        [17, list([190, 56, 34])]]], dtype=object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM