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 is True, the resulting array is set to the ufunc result. 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 arrays
x1 = np.array([6, 2, 3])
x2 = np.array([5, 2, 1])
# implementing the remainder function
myarray = np.remainder(x1, x2)
print(x1)
print(x2)
print("The remainders of the division element-wise: ", myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create the input arrays, x1 and x2, using the array() function.
  • Line 7: We implement the numpy.remainder() function on the input arrays. We assign the result to a variable called myarray.
  • Lines 9–10: We print the input arrays x1 and x2to the console.
  • Line 11: We print the variable myarray to the console.

Free Resources