Numpy mod() Function of Python

python Numpy mod() Function code python examples in jupyter notebook

Let’s discover the role and usage of the Python Numpy mod() function practically with code examples. Also, let’s explore all the parameters of this function in detail.

Python Numpy mod() Function

This function returns an array of remainders that are generated by element-wise operation between two input arrays. In this operation, each item of the first array is divided with the corresponding item of the second array.

Images of Code Examples

python Numpy mod() Function code python examples in jupyter notebook

python Numpy.mod() Function code python examples parameters explained

parameters of numpy mod function examples codes python

Mod Function Syntax

numpy.mod(ar1, ar2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])

Explanation of Its Parameters

Positional Parameters(required)

  • This first array(ar1) items will be divided.
  • The role of the divisor will be performed by the second array(ar2).

Keywords Parameters(optional)

  • We can specify where we want to store the result by using the ‘out‘ parameter.
  • Deciding the execution of the operation on specific items is handled by the ‘where‘ parameter.
  • Handling the type casting is performed by the ‘casting‘ parameter.
  • For the output array, the specification of memory layout is set by the ‘order‘ parameter.
  • Specifying the datatype of resulting arrays is done by the parameter ‘dtype‘.
  • We can set whether the resulting array should be of the same subclass or not as the input arrays provided as arguments to the mod() function using the ‘subok‘ parameter.

7 Examples explaining the working of mod() Function

Example 1: Modulus of two arrays (required arguments of mod Function)

import numpy as np

mont_revenue = np.array([16000, 25000, 19000, 29000])
mont_expenses = np.array([6000, 10000, 9000, 12000])

#remainders returned using mod
mont_profit = np.mod(mont_revenue, mont_expenses)

print("Monthly Revenue:", mont_revenue)
print("Monthly Expenses:", mont_expenses)
print("Monthly Profit:", mont_profit)

Output

Monthly Revenue: [16000 25000 19000 29000]
Monthly Expenses: [ 6000 10000  9000 12000]
Monthly Profit: [4000 5000 1000 5000]

In this code, we passed two numpy arrays to the mod function and it returned an array that consists of the remainders created as a result of the mod operation on each corresponding items of the two arrays.

Example 2: Storing the resulting array inside a specific array (‘out’ parameter)

arrayFirst= np.array([45,32,67,4])
arraySecond= np.array([5,32,6,8])
outArr = np.zeros_like(arrayFirst)
np.mod(arrayFirst, arraySecond, out=outArr)
print(outArr)

Output

[0 0 1 4]

This example shows that we can easily store the result inside our own specified array. We created an array that will have the same shape as the first array. Then we passed it to the ‘out’ parameter which will store the result inside this specific array.

Example 3: Decide which items the operation should be performed (‘where’ parameter)

xArray1 = np.array([56,23,567,8,34,634])
xArray2 = np.array([43,23,31,22,4,34])
condAr = np.array([True, False, True, False, True,False])

resultingArr = np.mod(xArray1, xArray2, where=condAr)
print(resultingArr)

Output

[13 23  9 22  2 34]

We created an array that contains Boolean datatype items. By passing it to the ‘where’ parameter, we specified which items should be used in the operation. In this code, the items at positions 2, 4, and 6 are not used in operations so the items of the second array(xArray2) were added to the resulting array as it is.

Example 4: Handle Type Casting with Parameter (casting)

aaa1 = np.array([40, 25, 60, 35, 70])
bbb2 = np.array([7, 9, 6, 20, 9])

resultArry = np.mod(aaa1, bbb2, casting='unsafe')
print(resultArry)

Output

[ 5.  7.  0. 15.  7.]

We passed ‘unsafe’ to the casting parameter which will give the output array a more suitable datatype. We have input arrays with items of datatype integer but the output items are in floating point.

Example 5: Set Memory Layout for output array(‘order’ parameter)

a1 = np.array([14, 10, 290, 325, 130])
a2 = np.array([6, 2, 3, 14, 120])

resultF = np.mod(a1, a2, order='F')
print(resultF)

Output

[ 2  0  2  3 10]

In this code, we set the order to ‘F‘ which means column-major order. We can also set it to ‘C‘ which specifies the row-major order.

Example 6: Custom Datatype of Output Array (‘dtype’ Parameter)

firstAr = np.array([10, 155, 20, 25, 30])
secondAr = np.array([3, 8, 4, 100, 15])

re = np.mod(firstAr, secondAr, dtype=np.float64) 
print(re)

Output

[ 1.  3.  0. 25.  0.]

We used two arrays that have item types of integers but in the output, we see floating-point elements. The reason is that we passed float to the ‘dtype’ parameter which will set the output array items to float even if the items of input arrays are integers.

Example 7: Deciding output array to be of a subclass as input arrays or not (parameter ‘subok’)

class MyArray(np.ndarray):
   pass

x1 = np.array([10, 25, 39, 249, 30]).view(MyArray)
x2 = np.array([2, 999, 40, 100, 5]).view(MyArray)

resultA = np.mod(x1, x2, subok=True)
resultB = np.mod(x1, x2, subok=False)
print(type(resultA))
print(type(resultB))

Output

<class '__main__.MyArray'>
<class 'numpy.ndarray'>

We created a subclass of numpy ‘np.ndarray‘. Then we created two arrays that are instances of that subclass. We also passed True and False to the parameter ‘subok’ of the mod function and stored the two results separately. We can see that when the ‘subok’ is True then the resulting array was of the same subclass as the input arrays. While when the ‘subok’ was set to False, then the output array was not of the same subclass as the input arrays.

In our previous article, we discussed the Numpy fmod function, so do check that out as well.

Conclusion

Hope these practical examples and demonstrations of the Python Numpy mod() function have helped you understand this function properly. To make you deeply understand the usage of this function, we discussed its parameters practically with examples.

Thank you.

Leave a Comment

Your email address will not be published. Required fields are marked *