How to invert the elements of a boolean array in python ?

Published: April 16, 2019

DMCA.com Protection Status

Examples of how to invert the elements of a boolean array in python using the numpy function invert()

>>> import numpy as np
>>> a = np.array((True,True,False,True,False))
>>> b = np.invert(a)
>>> b
array([False, False,  True, False,  True], dtype=bool)

Another example

>>> import numpy as np
>>> a = np.array((1,1,0,1,0), dtype=bool)
>>> b = np.invert(a)
>>> b
array([False, False,  True, False,  True], dtype=bool)

Note: can be used, for example, to mask an array (see How to mask an array using another array in python ?)

References

Links Site
numpy.invert docs.scipy.org
How to mask an array using another array in python ? science-emergence.com