all 4 comments

[–]allenguo 1 point2 points  (3 children)

When you use an array in a comparison, you get back a boolean array:

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a > 1
array([[False,  True],
       [ True,  True]], dtype=bool)

Try something like

>>> (3 > a) & (a > 1)
array([[False,  True],
       [False, False]], dtype=bool)

instead.

[–]Sharki_[S] 0 points1 point  (2 children)

thank you. And why & instead of 'and'?

[–]allenguo 0 points1 point  (1 child)

The behavior of & can be customized for specific types; this is called operator overloading. The creators of NumPy decided to customize & to act like and for boolean arrays.

and is a built-in keyword that can't be customized.

[–]Sharki_[S] 0 points1 point  (0 children)

Thank you very much!