NumPy divmod – 返回元素商和余数

您好,欢迎来到Numpy divmod教程 在本教程中,我们将学习 NumPy divmod() 方法,并看到许多相关示例。那么让我们开始吧!

另请阅读:NumPy mod – Numpy 中模数运算符的完整指南


什么是 NumPy divmod?

The divmod() method in NumPy returns the element-wise quotient and the remainder of the division of two given arrays. The // in Python returns the floor of the quotient and the % operator returns the remainder of the division, similar to the mod() function.

We will see examples demonstrating the use of this function in the upcoming sections of this tutorial.


Syntax of NumPy divmod

numpy.divmod(x1, x2, out=None)
Parameter Description Required/Optional
x1 (array_like) Dividend array. Required
x2 (array_like) Divisor array. Required
out An alternative output array in which to place the result. It must have the same shape as the expected output. Optional

Returns:
Returns the element-wise quotient and remainder of the division. If both x1 and x2 are scalars, then the result is also a scalar value.

Some common observations about the divmod() function:

  • If the first argument, i.e., x1 is 0, then the function returns (0, 0).
  • If the second argument, i.e., x2 is 0, then the function returns a ‘Zero Division Error’.
  • If x1 is a float value, the function returns (q, x1%x2) where q is the whole part of the quotient.

Examples of numpy.divmod()

Let’s now get right into using the numpy.divmod method so we can understand the outputs.

Using numpy.divmod() when both the elements are scalar

import numpy as np
 
dividend = 23
divisor = 6
ans = np.divmod(dividend, divisor)
 
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)

Output:

Dividend = 23
Divisor = 6
Result = (3, 5)

Here both the elements are scalar. 6*3=18 and 6*4=24, so 23 is not completely divisible by 6. When 23 is divided by 6, the quotient is 3 and remainder is 23-18=5, which is returned as (3, 5).


Using numpy.divmod() when one element is a scalar and the other an array

import numpy as np
 
dividend = [30, 19, 8]
divisor = 6
ans = np.divmod(dividend, divisor)
 
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)

Output:

Dividend = [30, 19, 8]
Divisor = 6
Result = (array([5, 3, 1], dtype=int32), array([0, 1, 2], dtype=int32))

In this case, all the elements in the dividend array are one-by-one divided by the divisor and the quotient and remainder for each of these divisions are stored in the respective result array.
The first array in the output is the quotient array and the second one is the remainder array.

The output is computed as:

Quotient array:

30//6 = 5
19//6 = 3
8//6 = 1

Remainder array:

30%6 = 0
19%6 = 1
8%6 = 2

当两个元素都是一维数组时使用 numpy.divmod()

import numpy as np
 
dividend = [72, 60, 30]
divisor = [3, 15, 24]
ans = np.divmod(dividend, divisor)
 
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =", ans)

输出:

Dividend = [72, 60, 30]
Divisor = [3, 15, 24]
Result = (array([24,  4,  1], dtype=int32), array([0, 0, 6], dtype=int32))

这里,两个数组中相同位置的元素进行除法运算,并计算商和余数。即,被除数[0]除以除数[0],依此类推。这只不过是按元素划分。

商数组:

dividend[0] // divisor[0] = 72//3 = 24
dividend[1] // divisor[1] = 60//15 = 4
dividend[2] // divisor[2] = 30//24 = 1

余数数组:

dividend[0] % divisor[0] = 72%3 = 0
dividend[1] % divisor[1] = 60 = 0
dividend[2] % divisor[2] = 30$ = 6

当两个元素都是二维数组时使用 numpy.divmod()

import numpy as np
 
dividend = [[18, 35], [10, 7]]
divisor = [[5, 7], [10, 4]]
ans = np.divmod(dividend, divisor)
 
print("Dividend =", dividend, "\nDivisor =", divisor)
print("Result =\n", ans)

输出:

Dividend = [[18, 35], [10, 7]]
Divisor = [[5, 7], [10, 4]]
Result =
 (array([[3, 5],
       [1, 1]], dtype=int32), array([[3, 0],
       [0, 3]], dtype=int32))

与上面一维数组的示例相同,这里也进行逐元素除法,商和余数计算如下:

商数组:

dividend[0][0] // divisor[0][0] = 18//5 = 3
dividend[0][1] // divisor[0][1] = 35//7 = 5
dividend[1][0] // divisor[1][0] = 10//10 = 1
dividend[1][1] // divisor[1][1] = 7//4 = 1

结果是 [[3, 5], [1, 1]]。

余数数组:

dividend[0][0] // divisor[0][0] = 18%5 = 3
dividend[0][1] // divisor[0][1] = 35%7 = 0
dividend[1][0] // divisor[1][0] = 10 = 0
dividend[1][1] // divisor[1][1] = 7%4 = 3

结果是 [[3, 0], [0, 3]]。


概括

就这样!在本教程中,我们学习了 Numpy divmod 方法,并使用该方法练习了不同类型的示例。如果您想了解有关 NumPy 的更多信息,请随时阅读我们的 NumPy 教程


参考