NumPy mod – A Complete Guide to the Modulus Operator in Numpy

NumPy Mod Cover Image

Hello and welcome to this tutorial on Numpy mod. In this tutorial, we will be learning about the NumPy mod() method and also seeing a lot of examples regarding the same. So let us begin!


What is NumPy mod?

The mod() method in NumPy returns the element-wise remainder of the division of two given arrays. The % operator in Python also returns the remainder of division, similar to the mod() function.

We will see the examples demonstrating the use of this function in the upcoming sections of this tutorial.


Syntax of NumPy mod

numpy.mod(x1, x2, out=None)
ParameterDescriptionRequired/Optional
x1 (array_like)Dividend array.Required
x2 (array_like)Divisor array.Required
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional

Returns: Returns the element-wise remainder of the division. If both x1 and x2 are scalars, then the result is also a scalar value.


Examples

Let’s now get right into using the numpy.mod method so we can understand the outputs.

When both the elements are scalar

import numpy as np 

dividend = 15
divisor = 7
ans = np.mod(dividend, divisor)

print("Dividend =", dividend, "\nDivisor =", divisor)
print(dividend, "%", divisor, "=", ans)

Output:

Dividend = 15 
Divisor = 7
15 % 7 = 1

A simple case where both the elements are scalar. 7*2=14 and 7*3=21, so 15 is not completely divisible by 7 and the remainder comes out to be 1 here.


Modulus of a scalar and an array using numpy.mod()

import numpy as np 

dividend = [13, 8, 16]
divisor = 7
ans = np.mod(dividend, divisor)

print("Dividend =", dividend, "\nDivisor =", divisor)
print(dividend, "%", divisor, "=", ans)

Output:

Dividend = [13, 8, 16] 
Divisor = 7
[13, 8, 16] % 7 = [6 1 2]

In this case, all the elements in the dividend array are one-by-one divided by the divisor and the remainder for each of these divisions is stored in the result array.
The output is computed as:
13%7 = 6
8%7 = 1
16%7 = 2

We can also reverse the elements as follows:

import numpy as np 

dividend = 7
divisor = [7, 5, 3]
ans = np.mod(dividend, divisor)

print("Dividend =", dividend, "\nDivisor =", divisor)
print(dividend, "%", divisor, "=", ans)

Output:

Dividend = 7 
Divisor = [7, 5, 3]
7 % [7, 5, 3] = [0 2 1]

Here, each element in the divisor array divides the dividend i.e. 7 and the remainder of it is stored in the output array. Hence, the output is computed as:
7%7 = 0
7%5 = 2
7%3 = 1


Modulus when both the elements are 1-dimensional arrays

import numpy as np 

dividend = [30, 58, 35]
divisor = [5, 9, 4]
ans = np.mod(dividend, divisor)

print("Dividend =", dividend, "\nDivisor =", divisor)
print(dividend, "%", divisor, "=", ans)

Output:

Dividend = [30, 58, 35] 
Divisor = [5, 9, 4]
[30, 58, 35] % [5, 9, 4] = [0 4 3]

Here, the elements at the same places in both arrays undergo the division operation and the remainder is calculated. That is, dividend[0] is divided by divisor[0] and so on. This is nothing but element-wise division.
The output is computed as:

dividend[0] % divisor[0] = 30%5 = 0
dividend[1] % divisor[1] = 58%9 = 4
dividend[2] % divisor[2] = 35%4 = 3

When both the elements are 2-dimensional arrays

import numpy as np 

dividend = [[16, 15], [24, 23]]
divisor = [[4, 7], [10, 9]]
ans = np.mod(dividend, divisor)

print("Dividend =", dividend, "\nDivisor =", divisor)
print(dividend, "%", divisor, "=\n", ans)

Output:

Dividend = [[16, 15], [24, 23]] 
Divisor = [[4, 7], [10, 9]]
[[16, 15], [24, 23]] % [[4, 7], [10, 9]] =
 [[0 1]
 [4 5]]

Same as the above example of 1-dimensional arrays, here as well the element-wise division takes place and the remainder is calculated as:
Row 1:

dividend[0][0] % divisor[0][0] = 16%4 = 0
dividend[0][1] % divisor[0][1] = 15%7 = 1

Row 2:

dividend[1][0] % divisor[1][0] = 24%10 = 4
dividend[1][1] % divisor[1][1] = 23%9 = 5

Conclusion

That’s all! In this tutorial, we learned about the Numpy mod method and practiced different types of examples using the same. If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference