使用Numpy计算布尔数组的按位非
要计算布尔数组的按位非,请在Python Numpy中使用**numpy.bitwise_not()**方法。计算输入数组中整数的底层二进制表示的按位非。此ufunc实现C/Python运算符˜。
where参数是在输入上广播的条件。在条件为True的位置,out数组将设置为ufunc结果。在其他位置,out数组将保留其原始值。请注意,如果通过默认的out=None创建未初始化的out数组,则其中条件为False的位置将保持未初始化状态。
步骤
首先,导入所需的库:
import numpy as np
创建一个二维数组:
arr = np.array([[True, False], [False,True ], [False, False]])
显示我们的数组:
print("Array...
",arr)获取数据类型:
print("
Array datatype...
",arr.dtype)
获取数组的维度:
print("
Array Dimensions...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
获取数组的元素个数:
print("
Elements in the Array...
",arr.size)要计算布尔数组的按位非,请在Python Numpy中使用numpy.bitwise_not()方法:
print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))
示例
import numpy as np
# Create a 2d array
arr = np.array([[True, False], [False,True ], [False, False]])
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)
# To compute the bit-wise NOT of a boolean array, use the numpy.bitwise_not() method in Python Numpy
print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))输出
Array... [[ True False] [False True] [False False]] Array datatype... bool Array Dimensions... 2 Our Array Shape... (3, 2) Elements in the Array... 6 Result (bit-wise NOT)... [[False True] [ True False] [ True True]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP