3

Say we have two arrays

a = np.array([1,2,3,4]).reshape(2,2)
b = np.array([True, False, False, True]).reshape(2,2)

gives

a = [[1, 2],
     [3, 4]] 
b = [[True, False],
     [False, True]]

We can do a[b] to get only the values of b that are true giving us [1, 4]

I tried to do a[not b] to get those that are false but got an error. I know I can do a[b == False] but want to do it in a Pythonic way.

Any solution?

Jeremie
  • 45
  • 2

1 Answers1

1

You can use either np.logical_not or the ~ operator:

>>> a[np.logical_not(b)]
>>> a[~b]
a_guest
  • 34,165
  • 12
  • 64
  • 118