在 NumPy 中返回除法的逐元素餘數


要返回除法的逐元素餘數,請在 Python NumPy 中使用 **numpy.remainder()** 方法。這裡,第一個引數是被除數陣列。第二個引數是除數陣列。

計算相對於 floor_divide 函式的餘數。它等同於 Python 模運算子“x1 % x2”,並且與除數 x2 具有相同的符號。與 np.remainder 等效的 MATLAB 函式是 mod。

out 是儲存結果的位置。如果提供,它必須具有輸入廣播到的形狀。如果未提供或為 None,則返回一個新分配的陣列。元組(僅作為關鍵字引數可能)必須具有與輸出數量相等的長度。

步驟

首先,匯入所需的庫 -

import numpy as np

建立陣列 -

arr = np.array([3, 9, 14, 22, 76, 81, 91])

顯示陣列 -

print("Array...
", arr)

獲取陣列的型別 -

print("
Our Array type...
", arr.dtype)

獲取陣列的維度 -

print("
Our Array Dimension...
",arr.ndim)

獲取陣列的形狀 -

print("
Our Array Shape...
",arr.shape)

要返回除法的逐元素餘數,請在 Python NumPy 中使用 numpy.remainder() 方法。這裡,第一個引數是被除數陣列。第二個引數是除數陣列 -

print("
Result (remainder)...
",np.remainder(arr, 7))

示例

import numpy as np

# Create an array
arr = np.array([3, 9, 14, 22, 76, 81, 91])

# Display the array
print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimension...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # To return the element-wise remainder of division, use the numpy.remainder() method in Python Numpy # Here, the 1st parameter is the Dividend array # The 2nd parameter is the Divisor array print("
Result (remainder)...
",np.remainder(arr, 7))

輸出

Array...
[ 3 9 14 22 76 81 91]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(7,)

Result (remainder)...
[3 2 0 1 6 4 0]

更新於: 2022年2月8日

315 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告