在 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([[92, 81, 98, 45], [22, 67, 54, 69 ]])
显示我们的数组 -
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([[92, 81, 98, 45], [22, 67, 54, 69 ]])
# 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 an array element-wise, use the numpy.bitwise_not() method in Python Numpy
print("
Result (bit-wise NOT)...
",np.bitwise_not(arr))输出
Array... [[92 81 98 45] [22 67 54 69]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (2, 4) Elements in the Array... 8 Result (bit-wise NOT)... [[-93 -82 -99 -46] [-23 -68 -55 -70]]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP