What is the numpy.mod() function in Python?

Overview

The mod() function in NumPy returns the remainder from the division of two arrays, element-wise.

Syntax

numpy.fmod(x1, x2, /, out=None, *, where=True)
Syntax for the mod() function

Parameter value

The mod() function takes the following parameter values:

  • x1: This represents the input array of elements that are the dividends. This is a required parameter.
  • x2: This represents an array of elements that are the divisors. This is also a required parameter.
  • out: This represents the location where the result is stored. This is an optional parameter.
  • where: This is the condition over which the input is being broadcast. At a given location where this condition is True, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value. This is also an optional parameter.
  • **kwargs: This represents other keyword arguments. This is also an optional parameter.
  • Example

    import numpy as np
    # creating input arrays
    x1 = np.array([1, 2, 3])
    x2 = np.array([1, 2, 2])
    # implementing the mod() function
    myarray = np.mod(x1, x2)
    print(x1)
    print(x2)
    print("The element-wise remainders values of the division are: ", myarray)

    Explanation

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

    Free Resources