~
: bitwise not, for python integer x, it’s essentially calculates -x - 1np.logical_not
: logical NOT-
: minusimport numpy as np
~
doesn't work as expected for Python Boolean values, since they're aint under the hood¶~True
-2
~False
-1
1 - True
0
1 - False
1
a1 = np.array([True, False])
a1.dtype
dtype('bool')
a1
array([ True, False])
~a1
array([False, True])
np.logical_not(a1)
array([False, True])
-a1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-8-39f3e99f3cf6> in <module> ----> 1 -a1 TypeError: The numpy boolean negative, the `-` operator, is not supported, use the `~` operator or the logical_not function instead.
a2 = np.array([0, 1, 2, True, False])
a2.dtype
dtype('int64')
a2
array([0, 1, 2, 1, 0])
~a2
array([-1, -2, -3, -2, -1])
np.logical_not(a2)
array([ True, False, False, False, True])
-a2
array([ 0, -1, -2, -1, 0])