在 NumPy 中計算陣列的按元素非運算的真值
要計算陣列的按元素非運算的真值,請在 Python NumPy 中使用 **numpy.logical_not()** 方法。返回值為 True 或 False。
返回值是布林結果,其形狀與 x 相同,表示對 x 的元素進行非運算的結果。如果 x 是標量,則這是一個標量。out 是儲存結果的位置。如果提供,則其形狀必須是輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅可能作為關鍵字引數)的長度必須等於輸出的數量。
NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援各種硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合良好。
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立一個二維 NumPy 陣列。我們插入了元素 -
arr = np.array([[True, False, True], [True, True, False]])
顯示陣列 -
print("Array...
", arr)獲取陣列的型別 -
print("
Our Array type...
", arr.dtype)獲取陣列的維度 -
print("
Our Array Dimension...
",arr.ndim)
獲取陣列的形狀 -
print("
Our Array Shape...
",arr.shape)要計算陣列的按元素非運算的真值,請在 Python NumPy 中使用 numpy.logical_not() 方法。返回值為 True 或 False -
print("
Result (NOT)...
",np.logical_not(arr))示例
import numpy as np
# Creating a 2D numpy array using the array() method
# We have inserted elements
arr = np.array([[True, False, True], [True, True, False]])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimension of the array
print("
Our Array Dimension...
",arr.ndim)
# Get the shape of the array
print("
Our Array Shape...
",arr.shape)
# To compute the truth value of NOT an array element-wise, use the numpy.logical_not() method in Python Numpy
# Return value is either True or False
print("
Result (NOT)...
",np.logical_not(arr))輸出
Array... [[ True False True] [ True True False]] Our Array type... bool Our Array Dimension... 2 Our Array Shape... (2, 3) Result (NOT)... [[False True False] [False False True]]
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP