numpy.fmod() Numpy Function

python numpy.fmod() function Numpy parameters with code examples in jupyter notebook explained

Let’s practically understand the numpy.fmod function of Python numpy. In order to make things interesting, we’ll be using Python example codes to demonstrate the working of its parameters.

Python Numpy fmod() Function

This function’s role is to calculate the remainder element-wise of division among two input arrays. The array that it returns contains items that are the result of the division of items of the first array and the second array’s corresponding items. It means the resulting array has the remainders.

python numpy.fmod() function Numpy parameters with code examples in jupyter notebook explained

python numpy fmod function code examples

python numpy fmod() function code examples parameters explained

 

Syntax

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

Parameters of fmod Function

1. Required Parameters

The parameters (ar1, ar2) show the arrays whose items will be used in the operation to get the remainders. The second array items should not be 0.

2. Optional Parameters

  • The ‘out‘ is used to store the output. The shape of this array must be the same as the input arrays.
  • The decision to perform the operation on specific items of input arrays is controlled by ‘where‘.
  • The type casting is controller using the parameter ‘casting‘. The ‘same_kind’ is its default value.
  • The output’s array memory layout is controlled by ‘order‘. ‘K’ is its default value.
  • The data type of the result array is controlled by ‘dtype‘. Default is using the types of input arrays.
  • If the ‘subok‘ parameter is True, then the resulting array will be the same subclass as the input arrays.

fmod() Function Practical Python Code Examples

Example 1: fmod() on 1D Arrays

import numpy as np

aaa = np.array([6,4,98,523])
bbb = np.array([5,2,8,42])

resultA = np.fmod(aaa, bbb)
print(resultA)

Output

[ 1  0  2 19]

In this example, we created simple 1D arrays using numpy. Then we passed these 2 arrays to the fmod() function and stored the returning value of this function inside a variable. The result shows that the remainder of each corresponding item of the arrays(aaa, bbb) is added to the newly created array.

Example 2: Making use of the ‘out’ Parameter

ar1DFirst = np.array([40, 80, 90,6])
ar1DSecond = np.array([6, 9, 4,3])
outAr = np.zeros(4, dtype=int)

np.fmod(ar1DFirst, ar1DSecond, out=outAr)
print(outAr)

Output

[4 8 2 0]

In this simple example, we created an array of zeros using the zeros function of Numpy and explicitly gave it a datatype of integer. Then we used the ‘out’ parameter of the fmod() function and passed it the array of zeros. Now the result will be stored in that array. Just make sure that the shape of this array is the same as the input arrays.

Example 3: Parameter ‘where’

a = np.array([14, 92, 54, 24])
b = np.array([5,7,6,2])

conditionArray = np.array([True, False, True, False])

resultingArray = np.fmod(a, b, where=conditionArray)
print(resultingArray)

Output

[4 7 0 2]
In this example code, we created an array that consists of Boolean values. Then we pass it to the argument ‘where’. In the output section, we can see that when the value was False, then no remainder operation was performed, instead the item of the second array(b) was returned as it is. In this example, items at positions 2 and 4 returned the values of the second array because the condition was False.

Example 4: ‘casting’ parameter of fmod Function

xAr1 = np.array([6.6, 3.4, 45.3])
xAr2 = np.array([4, 2, 10])

rAr = np.fmod(xAr1, xAr2, casting='unsafe')
print(rAr)

Output

[2.6  1.4  5.3]

In this code, we have arrays of type float and integer. We pass ‘unsafe’ to the parameter ‘casting’. It does the casting in which the resulting output will be in a floating point even when the input data is of a type integer. The default ‘same_kind’ makes the output of the same data type as the arrays that are passed to the fmod function as parameters.

Example 5: ‘dtype’ parameter of fmod

xx1 = np.array([17, 34, 95])
xx2 = np.array([5,2,8])

outputArrr = np.fmod(xx1, xx2, dtype=np.float64)
print(outputArrr)

Output

[2. 0. 7.]

We passed float to the ‘dtype’ parameter which means the output items of the array will be in float format. We can see that the input arrays items are in integer format but still, the output array items are in float format. The reason is that we passed float to the parameter ‘dtype’.

Example 6: Memory layout using the ‘order’ parameter

x1 = np.array([[4, 34, 26], [7, 4, 55]], order='C') # Row-major layout
x2 = np.array([7, 5, 10], order='F') # Column-major layout

# 'C' order for output array
res_c_order = np.fmod(x1, x2, order='C')

# 'F' order for output array
res_f_order = np.fmod(x1, x2, order='F')

print("Original first arr (C-order):\n", x1)
print("Original second arr (F-order):\n", x2)
print("Result with 'C' order for output array:\n", res_c_order)
print("Result with 'F' order for output array:\n", res_f_order)

Output

Original first arr (C-order):
 [[ 4 34 26]
 [ 7  4 55]]
Original second arr (F-order):
 [ 7  5 10]
Result with 'C' order for output array:
 [[4 4 6]
 [0 4 5]]
Result with 'F' order for output array:
 [[4 4 6]
 [0 4 5]]

In this example, we specified a 2D array and a 1D array and gave them their specific orders (C and F). Then we applied the fmod function on both of them and made use of the ‘order’ parameter to give orders to the resulting output. In the output, we can see the ‘C'(C style) order and the ‘F'(Fortran style) order of the resulting arrays.

Example 7: Using parameter ‘subok’

class MyArray(np.ndarray):
   pass

x1 = np.array([10, 20, 30]).view(MyArray)
x2 = np.array([3, 7, 5]).view(MyArray)

result_subok_false = np.fmod(x1, x2, subok=False)
result_subok_true = np.fmod(x1, x2, subok=True)

print(type(result_custom_subok_false))
print(type(result_custom_subok_true))

Output

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

In this code, we created a custom subclass of ‘ndarray’ named ‘MyArray’. Then we created two instance arrays of this custom class. After that, we pass it to the fmod() function and make the ‘subok’ parameter True and False. The output shows that when it was true then the resulting output was of the same subclass as the input arrays.

Conclusion

Hope you have a super understanding of this Python Numpy fmod function. We’ve discussed multiple Python code examples in which the parameters of the fmod function were implemented in order to understand the working of this function in depth.

Thank you so much for reading this Python post.

Leave a Comment

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