究竟如何numpy.where()选择在这个例子中的元素?

问题描述 投票:3回答:3

从numpy的docs

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

我的权利在假设[[True, False], [True, True]]部分是条件和[[1, 2], [3, 4]][[9, 8], [7, 6]]是根据文档参数分别是x和y。

那么究竟是选择在下面的例子中的元素的功能?

另外,为什么是这些例子中的元素类型的列表?

>>> 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)
python python-3.x numpy
3个回答
2
投票

在第一种情况下,每个项是(2,2)阵列(或者更确切地说,列表可以做成这样的阵列)。对于在条件各True,它返回在x,所述[[1 -][3,4]]对应项,并且对于每个False,从y [[- 8][- -]]术语

在第二种情况下,该名单是衣衫褴褛

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)

阵列是(2,)中,用2所列出。并且当浇铸为布尔型,2元件阵列,与两个真。只有一个空列表会产生错误。

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

在其中,然后返回刚才的x值。

第二种情况是可以理解的,但病理。

更多细节

我们来演示where更详细,用简单的情况。同等条件下阵:

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

单参数版本,也就是相当于condition.nonzero()

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

有些人觉得它更容易可视化的元组的transpose - 3对坐标,其中condition为真:

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

现在有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]])

可视化这一行动的一个好方法是有两个蒙面分配。

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

另一种方式来做到这一点是初始与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]])

如果xy是数组,而不是标量,这个蒙面分配将需要改进,但我希望一开始,这将帮助。


0
投票

np.where(condition,x,y)它检查的条件,如果其他人的真实退货X它返回y

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

在这里,你调理is[[True, False], [True, True]] x = [[1 , 2] , [3 , 4]] y = [[9 , 8] , [7 , 6]]

所以返回1而不是9第一条件为真

第二个条件是虚假的,因此返回8,而不是2


0
投票

阅读broadcasting作为@hpaulj建议后,我想我知道的功能是如何工作的。它会尝试广播3个阵列,那么,如果所述广播是成功的,它将使用TrueFalse值来接无论是从x或y的元素。在这个例子中

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

我们有

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

现在

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

因此,所有三个是只需用2个元素(类型列表的)阵列即使在条件(CND)。所以既[True, False,True][False, True]将被评估为True.And两个元件将从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)

我也有一个更复杂的例子(一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]]])

哪里

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

这里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)

现在,作为@hpaulj评论y将被广播到(2,2,2)。它可能会是这样的

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

其结果可以很容易地预测到

>>>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)
© www.soinside.com 2019 - 2024. All rights reserved.