NumPy remainder() Function:
Remainder function python: In numpy, there is also a function called numpy.remainder() that can be used to do mathematical operations. It returns the element-wise division remainder between two arrays arr1 and arr2, i.e. arr1 percent arr2. When arr2 is 0 and both arr1 and arr2 are (arrays of) integers, it returns 0.
Syntax:
numpy.remainder(l1, l2, out=None)
Parameters:
l1 and l2: (Required)
These are required arguments. These are the arrays that have to be divided, here l1 as dividend and l2 as the divisor.
They must be broadcastable to a common shape if l1.shape!= l2.shape.
out:
This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.
Return Value:
The element-by-element remainder of dividing l1 and l2 is returned(l1%l2).
NumPy remainder() Function in Python
Example
Approach:
- Import NumPy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Create some sample arrays of different shapes to test the remainder() function.
- Pass the first array and some random number to remainder() function of NumPy module and print the result.
- Here it divides each element of the array1 with the given random value say 9 and gives the remainder.
- Pass the first array and second array to remainder() function of NumPy module and print the result.
- Here it divides second array elements for the first array element and gives the remainder
- Similarly, test it with other arrays of different shapes.
- The Exit of the Program.
Below is the implementation:
# Import NumPy module using the import keyword. import numpy as np # Pass some random list as an argument to the array() function to create an array. # Store it in a variable. arry1 = np.array([[32,64],[95,128],[150,180]]) # Create some sample arrays of different shapes to test the remainder() function. arry2 = np.array([[10,20]]) arry3 = np.array([[100],[200],[300]]) arry4 = np.array([[55,65],[75,85],[95,105]]) # Pass the first array and some random number to remainder() function of NumPy module and print the result. # Here it divides each element of the array1 with the given random value say 9 and gives the remainder. print('Getting remainder value by dividing first array with 9: ') print(np.remainder(arry1, 9)) # Pass the first array and second array to remainder() function of NumPy module and print the result. # Here it divides second array elements for the first array element and gives the remainder print('Getting remainder value by dividing first array with second array: ') print(np.remainder(arry1, arry2)) # Similarly, test it with other arrays of different shapes. print('Getting remainder value by dividing first array with third array: ') print(np.remainder(arry1, arry3)) print('Getting remainder value by dividing first array with fourth array: ') print(np.remainder(arry1, arry4))
Output:
Getting remainder value by dividing first array with 9: [[5 1] [5 2] [6 0]] Getting remainder value by dividing first array with second array: [[2 4] [5 8] [0 0]] Getting remainder value by dividing first array with third array: [[ 32 64] [ 95 128] [150 180]] Getting remainder value by dividing first array with fourth array: [[32 64] [20 43] [55 75]]