numpy.where() usage details

numpy.where ( condition [, x , y ])


numpy.where() can be used in two ways:

1. np.where(condition, x, y)

If the condition is satisfied, output x, if not, output y.


If it is a one-dimensional array, it is equivalent to[xv if c else yv for (c,xv,yv) in zip(condition,x,y)]

>>> aa = np.arange(10)
>>> np.where(aa,1,-1)
array([-1,  1,  1,  1,  1,  1,  1,  1,  1,  1])  # 0为False,所以第一个输出-1
>>> np.where(aa > 5,1,-1)
array([-1, -1, -1, -1, -1, -1,  1,  1,  1,  1])

>>> np.where([[True,False], [True,True]],    # 官网上的例子
             [[1,2], [3,4]],
             [[9,8], [7,6]])
array([[1, 8],
       [3, 4]])

The conditions of the above example are [[True,False], [True,False]], respectively, corresponding to the four values ​​of the final output result. The first value is [1,9]selected from, because the condition is True, so 1 is selected. The second value is [2,8]selected from it, because the condition is False, so choose 8, and so on. A similar problem can be seen as an example:

>>> a = 10
>>> np.where([[a > 5,a < 5], [a == 10,a == 7]],
             [["chosen","not chosen"], ["chosen","not chosen"]],
             [["not chosen","chosen"], ["not chosen","chosen"]])

array([['chosen', 'chosen'],
       ['chosen', 'chosen']], dtype='<U10')



2. np.where(condition)

Only the condition (condition), without x and y, outputs the coordinates of the element that meets the condition (ie, non-zero) (equivalent to numpy.nonzero ). The coordinates here are given in the form of tuple. Usually, how many dimensions does the original array have, and the output tuple contains several arrays, which correspond to the coordinates of each dimension of the eligible elements.

>>> a = np.array([2,4,6,8,10])
>>> np.where(a > 5)             # 返回索引
(array([2, 3, 4]),)   
>>> a[np.where(a > 5)]              # 等价于 a[a>5]
array([ 6,  8, 10])

>>> np.where([[0, 1], [1, 0]])
(array([0, 1]), array([1, 0]))

The truth value in the above example condition [[0,1],[1,0]]is two 1s, and the respective first-dimensional coordinates are [0,1]and the second-dimensional coordinates are [1,0].


Here's a more complicated example:

>>> a = np.arange(27).reshape(3,3,3)
>>> a
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])

>>> np.where(a > 5)
(array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
 array([2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2, 0, 0, 0, 1, 1, 1, 2, 2, 2]),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2]))


# 符合条件的元素为
       [ 6,  7,  8]],

      [[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]],

      [[18, 19, 20],
       [21, 22, 23],
       [24, 25, 26]]]

So np.where will output the corresponding coordinates of each element, because the original array has three dimensions, so there are three arrays in the tuple.



/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734969&siteId=291194637