使用〜反转一个numpy布尔数组

时间:2022-06-28 01:52:29

Can I use ~A to invert a numpy array of booleans, instead of the rather awkward functions np.logical_and() and np.invert()? Indeed, ~ seems to work fine, but I can't find it in any nympy reference manual, and - more alarmingly - it certainly does not work with scalars (e.g. bool(~True) returns True !), so I'm a little bit worried ...

我可以用~A来反转一个numpy数组的布尔值,而不是相当笨拙的函数np.logical_and()和np.invert()吗?确实,〜似乎工作得很好,但我在任何nympy参考手册中找不到它,而且 - 更令人担忧的是 - 它肯定不适用于标量(例如bool(~True)返回True!),所以我是一个有点担心......

1 个解决方案

#1


22  

short answer: YES

简答:是的

Ref:

参考:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html

Notice:

注意:

Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~.

计算输入数组中整数的基础二进制表示的逐位NOT。这个ufunc实现了C / Python操作符〜。

and

bitwise_not is an alias for invert:

bitwise_not是invert的别名:

>> np.bitwise_not is np.invert
>> True

#1


22  

short answer: YES

简答:是的

Ref:

参考:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html

http://docs.scipy.org/doc/numpy/reference/generated/numpy.invert.html

Notice:

注意:

Computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator ~.

计算输入数组中整数的基础二进制表示的逐位NOT。这个ufunc实现了C / Python操作符〜。

and

bitwise_not is an alias for invert:

bitwise_not是invert的别名:

>> np.bitwise_not is np.invert
>> True