numpy.mod() in Python: Introduction, Syntax & Examples

The numpy.mod() function in Python is a part of the NumPy library used for various numerical and mathematical operations. It is used to compute the element-wise remainder of the division between two arrays.

In this article, we will understand Python numpy.mod() function, its syntax, and demonstrate it with various examples. Let’s get started.

Also Read: numpy.multiply() in Python

Syntax of numpy.mod() Function

Let us get started by understanding the fundamental constituents of the numpy.mod() function with the help of its syntax given below.

Syntax:

numpy.mod(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None)

Parameters:

  • x1: This is the dividend array or the scalar value.
  • x2: This is the divisor array or the scalar value.
  • out (optional): This is an optional parameter that specifies the location where the result should be stored. If not provided, a new array will be created.
  • where (optional): This parameter is used for conditional execution. If provided, only the elements where the condition is True will be included in the result.
  • casting (optional): This parameter controls what kind of data casting may occur. The default is ‘same_kind’, which means that only casting between similar kinds (e.g., float to float) is allowed.
  • order (optional): Specifies the memory layout of the result. The default is ‘K’ which means the order is determined by the input array’s memory layout.
  • dtype (optional): This parameter allows us to set the data type of the output array. If not specified, it is determined by the data types of the input arrays.

Return:

The numpy.mod() function returns an array with the same shape as the input arrays, where each element is the remainder of the corresponding elements in x1 divided by x2.

Examples of numpy.mod() Function

Let us now look at some examples to demonstrate the use of numpy.mod() function in Python.

Modulus of Two Arrays

Let’s start computing the reminders by passing one-dimensional arrays into the numpy.mod() function.

Example:

import numpy as np

ar1 = np.array([180, 90, 160, 290])
ar2 = np.array([6, 25, 12, 12])

result = np.mod(ar1, ar2)

print(result)

In the above code, we first imported the NumPy library as np and then by using it we created two arrays: ar1 and ar2. Then we passed these two arrays in the nump.mod() function which will be responsible for returning the array of remainders and then we saved the outcome into the result variable.

Output:

Modulus of Two Arrays

Here we can see that the numpy.modf() function does the element-wise operation between the two arrays ar1 and ar2 by dividing each item from the array ar1 with the corresponding item of the second array ar2 and returns us the array of remainders.

Storing the Output Array Inside a Specific Array

In this example, let’s try to store our output array inside a specified array using the ‘out’ parameter.

Example:

import numpy as np

ar1= np.array([8,12,87,14])
ar2= np.array([5,90,36,6])

result = np.zeros_like(ar1)
np.mod(ar1, ar2, out=result)

print(result)

Firstly like above we created two arrays: ar1 and ar2 for performing the operation on them. Then we created an array filled with zeros with the same shape as the ar1 using numpy.zeros_like() function and stored the outcome in the result variable. Then we passed the result array to the out parameter in the numpy.mod() function along with both the arrays means after performing the operation on ar1 and ar2 the outcome from the operation will be stored in the result array.

Output:

Storing the Output Array Inside a Specific Array

Deciding on Which Items the Operation Should Be Performed

In this example, we will perform the mod operation by filtering the elements from the arrays by passing the ‘where’ parameter in the numpy.mod() function.

Example:

import numpy as np

ar1 = np.array([6,13,117,8,110,123])
ar2 = np.array([3,2,11,65,54,4])

condAr = np.array([True, False, True, False, True,False])
result = np.mod(ar1, ar2, where=condAr)

print(result)

This time we have created a boolean array that contains boolean data type elements in it for doing the operation. Then we passed this boolean array condAr to the ‘where’ parameter in the numpy.mod() function which decides on which elements from the arrays: ar1 and ar2 the operation should be performed.

That means the first element of the condAr is True that’s why the mod operation between both the arrays ar1 and ar2 will be performed and the outcome will be returned in the result variable. In the same way, the second element of condAr is False so the mod operation will not be performed between the arrays so the elements from the array ar2 will be returned as it is in the result variable.

Output:

Deciding on Which Items the Operation Should Be Performed

Passing the ‘dtype’ Parameter in the numpy.mod() Function

As we know in numpy.mod() function the default datatype of the output array is the same as the datatype of our input arrays. So let’s change the datatype of our output array by passing parameter ‘dtype’ in the numpy.mod() function.

Example:

import numpy as np

ar1 = np.array([4, 15, 20, 12, 11])
ar2 = np.array([3, 2, 4, 5, 7])

result = np.mod(ar1, ar2, dtype=np.float64) 

print(result)

Output:

Passing the 'dtype' Parameter in the numpy.mod() Function

Here we have taken the input arrays: ar1 and ar2 of integer datatype. But we can see that the elements of our output array are of type float because we have passed float to the ‘dtype’ parameter in the numpy.mod() function.

Conclusion

In this tutorial, we have discussed numpy.mod() function provided by Python’s NumPy library, its syntax, and parameters, and also explored a few examples to get an understanding of it. After reading this tutorial, we hope you can easily get the element-wise remainder of the division between two NumPy arrays.

Read More: numpy.reciprocal() in Python – Get Reciprocal of Array Elements

Reference

https://numpy.org/doc/stable/reference/generated/numpy.mod.html

Priyanshu Singh
Priyanshu Singh
Articles: 44