w3resource

Perform Masked multiplication on a Masked array in NumPy

NumPy: Masked Arrays Exercise-13 with Solution

Write a NumPy program that creates a masked array and performs a masked operation, such as masked multiplication.

Sample Solution:

Python Code:

import numpy as np
import numpy.ma as ma

# Create a 2D NumPy array of shape (4, 4) with random integers
array_2d = np.random.randint(0, 100, size=(4, 4))

# Define the condition to mask elements greater than 50
condition = array_2d > 50

# Create a masked array from the 2D array using the condition
masked_array = ma.masked_array(array_2d, mask=condition)

# Create another 2D NumPy array of the same shape with random integers
multiplier_array = np.random.randint(1, 10, size=(4, 4))

# Perform masked multiplication
masked_multiplication_result = masked_array * multiplier_array

# Print the original arrays and the masked multiplication result
print('Original 2D array:\n', array_2d)
print('Masked array (elements > 50 are masked):\n', masked_array)
print('Multiplier array:\n', multiplier_array)
print('Masked multiplication result:\n', masked_multiplication_result)

Output:

Original 2D array:
 [[19 60 37 77]
 [46 57 49 41]
 [47 48 27 24]
 [13 35  2 27]]
Masked array (elements > 50 are masked):
 [[19 -- 37 --]
 [46 -- 49 41]
 [47 48 27 24]
 [13 35 2 27]]
Multiplier array:
 [[2 3 6 1]
 [6 5 8 2]
 [7 6 9 9]
 [3 8 2 3]]
Masked multiplication result:
 [[38 -- 222 --]
 [276 -- 392 82]
 [329 288 243 216]
 [39 280 4 81]]

Explanation:

  • Import Libraries:
    • Imported numpy as "np" for array creation and manipulation.
    • Imported numpy.ma as "ma" for creating and working with masked arrays.
  • Create 2D NumPy Array:
    • Create a 2D NumPy array named 'array_2d' with random integers ranging from 0 to 99 and a shape of (4, 4).
  • Define Condition:
    • Define a condition to mask elements in the array that are greater than 50.
  • Create Masked Array:
    • Create a masked array from the 2D array using ma.masked_array, applying the condition as the mask. Elements greater than 50 are masked.
  • Create Multiplier Array:
    • Create another 2D NumPy array named multiplier_array with random integers ranging from 1 to 9 and a shape of (4, 4).
  • Perform Masked Multiplication:
    • Performed masked multiplication using the masked array and the multiplier array. The multiplication operation respects the mask, and masked elements remain masked in the result.
  • Print the original 2D array, the masked array, the multiplier array, and the result of the masked multiplication.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Convert Masked NumPy array to Regular array with NaN.
Next: Apply custom function to Unmasked elements in a Masked NumPy array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/perform-masked-multiplication-on-a-masked-array-in-numpy.php