What is the numpy.remainder() function in NumPy?
Overview
The remainder() function in Python is used to obtain the remainder of a division of two input arrays, x1 and x2, element-wise.
Syntax
numpy.remainder(x1, x2, /, out=None, *, where=True)
Syntax for the remainder() function
Parameter values
The remainder() function takes the following parameter values:
x1: This represents the input array of elements, which are the dividends. This is a required parameter value.
x2: This represents an array of array, which are the divisors. This is a required parameter value.out: This represents the location where the result is stored. This is an optional parameter value.where: This is the condition over which the input is broadcast. This is an optional parameter value. At a given location where this condition isTrue, the resulting array is set to theufuncresult. Otherwise, the resulting array retains its original value.
-
kwargs: This represents the other keyword arguments. This is an optional parameter value.
Return value
The remainder() function returns the remainder, element-wise, of the division of two input arrays.
Example
import numpy as np# creating input arraysx1 = np.array([6, 2, 3])x2 = np.array([5, 2, 1])# implementing the remainder functionmyarray = np.remainder(x1, x2)print(x1)print(x2)print("The remainders of the division element-wise: ", myarray)
Explanation
- Line 1: We import the
numpymodule. - Line 4: We create the input arrays,
x1andx2, using thearray()function. - Line 7: We implement the
numpy.remainder()function on the input arrays. We assign the result to a variable calledmyarray. - Lines 9–10: We print the input arrays
x1andx2to the console. - Line 11: We print the variable
myarrayto the console.