To create three possible outcomes based on a condition using np.where()
in Python with numpy, you can follow these steps:
main.py19 chars2 lines
main.py27 chars2 lines
np.where()
to assign different values based on the condition. The first argument is the condition, the second argument is the value to assign if the condition is True, and the third argument is the value to assign if the condition is False:main.py60 chars2 lines
In this case, if the condition is True (number is greater than 5), it will assign value_if_true
, and if the condition is False, it will assign value_if_false
.
Here's an example that creates three possible outcomes:
main.py287 chars14 lines
Output:
main.py105 chars2 lines
In this example, it checks if each element of your_array
is greater than 5, and assigns the corresponding outcome based on the condition.
By using np.where()
, you can easily create multiple outcomes based on a condition in Python with numpy.
gistlibby LogSnag