Numpy where syntax from docs

后端 未结 3 1606
我寻月下人不归
我寻月下人不归 2020-12-12 04:44

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

3条回答
  •  情歌与酒
    2020-12-12 05:26

    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.

提交回复
热议问题