Divide a given scalar element with masked array elements and return arrays with Quotient and Remainder in NumPy

To divide a given scalar element with masked array elements and return arrays with Quotient and Remainder, use the ma.MaskedArray.__rdivmod__() method in Python Numpy. A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.

Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...<br>",arr.ndim)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)

Get the shape of the Masked Array −

print("\nOur Masked Array Shape...<br>",maskArr.shape)

Get the number of elements of the Masked Array −

print("\nElements in the Masked Array...<br>",maskArr.size)

The scalar −

val = 35
print("\nThe given value...<br>",val)

To divide a given scalar element with masked array elements and return arrays with Quotient and Remainder, use the ma.MaskedArray.__rdivmod__() method −

print("\nResultant Arrays...<br>",maskArr.__rdivmod__(val))

Example

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[85, 68, 35, 84], [67, 33, 109, 53], [29, 88, 105, 37], [56, 45, 70, 85]])
print("Array...<br>", arr)
print("\nArray type...<br>", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...<br>",arr.ndim)

# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0],
[0, 0, 0, 1], [0, 1, 0, 0]])
print("\nOur Masked Array<br>", maskArr)
print("\nOur Masked Array type...<br>", maskArr.dtype)

# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...<br>",maskArr.ndim)

# Get the shape of the Masked Array
print("\nOur Masked Array Shape...<br>",maskArr.shape)

# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...<br>",maskArr.size)

# The scalar
val = 35
print("\nThe given value...<br>",val)

# To divide a given scalar element with masked array elements and return arrays with Quotient and Remainder,
# use the ma.MaskedArray.__rdivmod__() method
print("\nResultant Arrays...<br>",maskArr.__rdivmod__(val))

Output

Array type...
int64

Array Dimensions...
2

Our Masked Array
[[-- -- 35 84]
[67 33 -- 53]
[29 88 105 --]
[56 -- 70 85]]

Our Masked Array type...
int64

Our Masked Array Dimensions...
2

Our Masked Array Shape...
(4, 4)

Elements in the Masked Array...
16

The given value...
35

Resultant Arrays...
(masked_array(
data=[[--, --, 1, 0],
      [0, 1, --, 0],
      [1, 0, 0, --],
      [0, --, 0, 0]],
mask=[[ True, True, False, False],
      [False, False, True, False],
      [False, False, False, True],
      [False, True, False, False]],
fill_value=999999), masked_array(
data=[[--, --, 0, 35],
      [35, 2, --, 35],
      [6, 35, 35, --],
      [35, --, 35, 35]],
mask=[[ True, True, False, False],
      [False, False, True, False],
      [False, False, False, True],
      [False, True, False, False]],
fill_value=999999))

Updated on: 2022-02-04T06:51:47+05:30

202 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements