[PYTHON] where in numpy

Even if I looked it up on the web, I couldn't find the contents easily, so I made a note (it's really short ...)

what I want to know

>> import numpy as np
>> np.where(A>5, 1, -1)

Is there a code? I thought

Try to move

First preparation

>> import numpy as np
###First of all, make an array
>> A = np.arange(10,0,-1)
###Content confirmation
>> A
array([10,  9,  8,  7,  6,  5,  4,  3,  2,  1])

Verification!

###Let's check from the inside
>> A>5
[ True  True  True  True  True False False False False False]
### ->If the condition is met, True is returned, and if not, False is returned.
###Try to bite where
>> np.where(A>5)
(array([0, 1, 2, 3, 4]),)
### ->You have extracted the elements that meet the conditions.
###Well, the content of the theme
>> np.where(A>5, 1, -1)
array([ 1,  1,  1,  1,  1, -1, -1, -1, -1, -1])
### ->If the condition is met, the value of the second argument"1", If not satisfied, the value of the third argument"-1"Seems to return
###Check the contents of ↑ with other values just in case
>> np.where(A>5,2,0)
array([2, 2, 2, 2, 2, 0, 0, 0, 0, 0])
### ->It looks like it fits.
```

 Does it feel like a SQL `where` clause? (is that different?!)

 Reference
 --Official document:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html
 ↑ Based on this article, does it help you understand a little? !!


Recommended Posts

where in numpy
Axis specification in NumPy
Install numpy in Visual Studio 2019
Imitated Python's Numpy in C #
Matrix multiplication in python numpy
Accelerate Zhang-Suen algorithm in Numpy
Put python, numpy, opencv3 in ubuntu14
Self-organizing map in Python NumPy version
[Numpy] Call savetxt () in append mode
Invert numpy Boolean array in tilde
Fixed an issue where numpy.memmap wouldn't fit in numpy array memory
Numpy [Basic]
numpy part 1
NumPy basics
Numpy Memorandum_Matrix
numpy tips
Jupyter, numpy, matplotlib notes used in reports
About numpy
NumPy axis
Use Numpy
numpy part 2
How to install pip, numpy in Autodesk MAYA
[Python] Swapping rows and columns in Numpy data
Install Python 3.5.1 + numpy + scipy + α in Windows environment
Implement PRML algorithm in Python (almost Numpy only)