Return the element-wise remainder of division with modulo operation in Numpy

NumpyServer Side ProgrammingProgramming

To return the element-wise remainder of division with mod operation, use the numpy.mod() method in Python Numpy. Here, the 1st parameter is the Dividend array. The 2nd parameter is the Divisor array.

Computes the remainder complementary to the floor_divide function. It is equivalent to the Python modulus operator "x1 % x2" and has the same sign as the divisor x2. The MATLAB function equivalent to np.remainder is mod.

Steps

At first, import the required library −

import numpy as np

Create an array. This is the dividend array −

arr = np.array([3, 9, 14, 22, 76, 81, 91])

Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nOur Array type...\n", arr.dtype)

Get the dimensions of the Array −

print("\nOur Array Dimension...\n",arr.ndim)

Get the shape of the Array −

print("\nOur Array Shape...\n",arr.shape)

Divisor array −

divisor = np.array([5, 7, 3, 2, 2, 7, 7])

To return the element-wise remainder of division with mod operation, use the numpy.mod() method. Here, the 1st parameter is the Dividend array. The 2nd parameter is the Divisor array:

print("\nResult...\n",np.mod(arr, divisor))

Example

import numpy as np

# Create an array
# This is the dividend array
arr = np.array([3, 9, 14, 22, 76, 81, 91])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# Divisor array
divisor = np.array([5, 7, 3, 2, 2, 7, 7])

# To return the element-wise remainder of division with mod operation, use the numpy.mod() method in Python Numpy
# Here, the 1st parameter is the Dividend array
# The 2nd parameter is the Divisor array
print("\nResult...\n",np.mod(arr, divisor))

Output

Array...
[ 3 9 14 22 76 81 91]

Our Array type...
int64

Our Array Dimension...
1

Our Array Shape...
(7,)

Result...
[3 2 2 0 0 4 0]
raja
Updated on 08-Feb-2022 09:45:49

Advertisements