# Specific use of NP. Where()  and NP. Where() 

Time：2021-10-24

This paper mainly introduces the specific use of NP. Where()  and NP. Where() , as well as the specific usage of NP. Where(). There is no more nonsense, as follows:

?
 1 2 3 4 5 6 7 8 9 `import` `numpy as np` ` ` `a ``=` `np.arange(``12``).reshape(``3``,``4``)` `print``(``'a:'``, a)` `print``(``'np.where(a > 5):'``, np.where(a > ``5``))` `print``(``'a[np.where(a > 5)]:'``, a[np.where(a > ``5``)])` `print``(``'np.where(a > 5):'``, np.where(a > ``5``)[``0``])` `print``(``'np.where(a > 5):'``, np.where(a > ``5``)[``1``])` `print``(a[np.where(a > ``5``)[``0``], np.where(a > ``5``)[``1``]])`
?
 1 2 3 4 5 6 7 8 `a: [[ ``0` `1` `2` `3``]` ` ``[ ``4` `5` `6` `7``]` ` ``[ ``8` `9` `10` `11``]]` `np.where(a > ``5``): (array([``1``, ``1``, ``2``, ``2``, ``2``, ``2``]), array([``2``, ``3``, ``0``, ``1``, ``2``, ``3``]))` `a[np.where(a > ``5``)]: [ ``6` `7` `8` `9` `10` `11``]` `np.where(a > ``5``)[``0``]: [``1` `1` `2` `2` `2` `2``]` `np.where(a > ``5``)[``1``]: [``2` `3` `0` `1` `2` `3``]` `[ ``6` `7` `8` `9` `10` `11``]`

NP. Where()  indicates row index and NP. Where()  indicates column index

## Numpy. Where() has two uses:

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

?
 1 2 3 4 5 6 7 8 9 10 11 `>>> aa ``=` `np.arange(``10``)` `>>> np.where(aa,``1``,``-``1``)` `array([``-``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``, ``1``]) ``#0 is false, so the first output is - 1` `>>> np.where(aa > ``5``,``1``,``-``1``)` `array([``-``1``, ``-``1``, ``-``1``, ``-``1``, ``-``1``, ``-``1``, ``1``, ``1``, ``1``, ``1``])`   `>>> np.where([[``True``,``False``], [``True``,``True``]],  ``#Examples on the official website` `  ``[[``1``,``2``], [``3``,``4``]],` `       ``[[``9``,``8``], [``7``,``6``]])` `array([[``1``, ``8``],` `  ``[``3``, ``4``]])`

The conditions of the above example are [[true, false] and [true, false]], respectively corresponding to the four values of the final output result. The first value is selected from [1,9]. Because the condition is true, it is 1. The second value is selected from [2,8]. Because the condition is false, select 8, and so on. For similar problems, take another example:

?
 1 2 3 4 5 6 7 `>>> 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``=``'

### 2. np.where(condition)

If there are only conditions and NO x and y, the coordinates of elements that meet the condition (i.e. non-0) are output (equivalent to numpy. Nonzero). The coordinates here are given in the form of tuples. Usually, the output tuple contains several arrays corresponding to the dimensional coordinates of qualified elements.

?
 1 2 3 4 5 6 7 8 `>>> a ``=` `np.array([``2``,``4``,``6``,``8``,``10``])` `>>> np.where(a > ``5``)  ``#Return index` `(array([``2``, ``3``, ``4``]),)  ` `>>> a[np.where(a > ``5``)]   ``#Equivalent to a [a > 5]` `array([ ``6``, ``8``, ``10``])`   `>>> np.where([[``0``, ``1``], [``1``, ``0``]])` `(array([``0``, ``1``]), array([``1``, ``0``]))`

In the above example, the true values of [[0,1] and [1,0]] are two ones, with their first dimensional coordinates of [0,1] and the second dimensional coordinates of [1,0].

Here is an example of a complex point:

?
 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 27 28 `>>> 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``]))` `#The eligible elements are` `  ``[ ``6``, ``7``, ``8``]],`   `   ``[[ ``9``, ``10``, ``11``],` `    ``[``12``, ``13``, ``14``],` `    ``[``15``, ``16``, ``17``]],`   `   ``[[``18``, ``19``, ``20``],` `    ``[``21``, ``22``, ``23``],` `    ``[``24``, ``25``, ``26``]]]`

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

It should be noted that the input cannot be list directly. It needs to be changed to array or array. For example, range (10) and NP. Range (10), which returns an array, can only achieve the effect by using NP. Where.

This is the end of this article about the specific use of NP. Where ()  and NP. Where () . For more information about NP. Where ()  and NP. Where () , please search for previous articles or continue to browse the relevant articles below. I hope you will support developpaper in the future!

## [hematemesis finishing] Super complete golang interview questions collection + golang Learning Guide + golang knowledge map + growth route

The brain map is constantly updated. Check the address onlineSubsequent articles and contents will be updated toGitHub projectWelcome to pay attention. Directory (Ctrl + F) Basic introduction Novice 50 mistakes that golang novices often make data type I don’t even know that nil slice is different from empty slice? Then the bat interviewer has to […]