Trying to teach myself some python and I am super confused from the docs what the where function does. Can somebody explain the example from the documentation below step by
I think it becomes pretty clear when you add linebreaks to arrange the inputs to look like matrices:
np.where( # First argument
[[True, False],
[True, True]],
# Second argument
[[1, 2],
[3, 4]],
# Third argument
[[9, 8],
[7, 6]])
You can see the first argument as a mask that determines from which of the two following inputs elements should be taken.
The result
array([[1, 8],
[3, 4]])
contains elements from the second argument wherever the mask is True and elements from the third argument where it is False.