在 NumPy 中返回一個數組小於等於另一個數組的元素級真值


要返回一個數組小於等於另一個數組的元素級真值,請在 Python NumPy 中使用 **numpy.less_equal()** 方法。返回值為 True 或 False。返回輸出陣列,x1 和 x2 的元素級比較。通常為 bool 型別,除非傳遞 dtype=object。如果 x1 和 x2 都是標量,則為標量。

該條件在輸入上進行廣播。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。請注意,如果透過預設的 out=None 建立了一個未初始化的 out 陣列,則其中條件為 False 的位置將保持未初始化狀態。

NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等等。它支援各種硬體和計算平臺,並且與分散式、GPU 和稀疏陣列庫配合良好。

步驟

首先,匯入所需的庫 -

import numpy as np

使用 array() 方法建立兩個 2D NumPy 陣列。我們插入了 int 型別的元素 -

arr1 = np.array([[49, 76, 61], [82, 69, 29]])
arr2 = np.array([[40, 60, 61], [81, 55, 32]])

顯示陣列 -

print("Array 1...
", arr1) print("
Array 2...
", arr2)

獲取陣列的型別 -

print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)

獲取陣列的維度 -

print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)

獲取陣列的形狀 -

print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)

要返回一個數組小於等於另一個數組的元素級真值,請使用 numpy.less_equal() 方法。返回值為 True 或 False -

print("
Result...
",np.less_equal(arr1, arr2))

示例

import numpy as np

# Creating two 2D numpy array using the array() method
# We have inserted elements of int type
arr1 = np.array([[49, 76, 61], [82, 69, 29]])
arr2 = np.array([[40, 60, 61], [81, 55, 32]])

# Display the arrays
print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To return the truth value of an array less than equal to another element-wise, use the numpy.less_equal() method in Python Numpy # Return value is either True or False print("
Result...
",np.less_equal(arr1, arr2))

輸出

Array 1...
[[49 76 61]
[82 69 29]]

Array 2...
[[40 60 61]
[81 55 32]]

Our Array 1 type...
int64

Our Array 2 type...
int64

Our Array 1 Dimensions...
2

Our Array 2 Dimensions...
2

Our Array 1 Shape...
(2, 3)

Our Array 2 Shape...
(2, 3)

Result...
[[False False True]
[False False True]]

更新於: 2022年2月7日

119 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告