使用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