In Python’s NumPy library, the
mod() method returns the remainder of a division.
numpy.mod() method returns the remainder after dividing two input arrays. This is done element-wise (element by element).
Note: In Python, a list of lists can be used to create a two-dimensional (2D) array.
numpy.mod(x1, x2, dtype=None, out=None)
x1: This is an array that represents the data input. This is the dividend.
x2: This is an array that represents the data input. This is the divisor.
dtype: This is an optional parameter. It represents the array’s return type.
out: This is an optional parameter. It denotes the alternate output array where the result will be stored.
Note: If the shapes of
x2differ, they must be able to be broadcasted to a common shape for output representation.
numpy.mod() method returns an array that contains the remainder from the
Note: In Python, the
modfunction is equivalent to the modulus operator,
x1 % x2.
The following code shows how to use the
numpy.mod() method for two-dimensional (2D) arrays:
# import numpy import numpy as np # create a two 2D arrays x1 = np.array([[2,6,5],[3,4,8]]) x2 = np.array([[1,7,2],[10,9,4]]) # divide the 2D arrays # and store the remainder in result result = np.mod(x1, x2) print(result)
np.mod()method. This returns an array containing the remainder of dividing array
x2. The result is stored in a new array called
View all Courses