w3resource

Perform Masked multiplication on a Masked array in NumPy


13. Masked Operation: Multiplication

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.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to perform element-wise multiplication of two masked arrays while preserving the union of their masks.
  • Write a Numpy program to multiply a masked array by a scalar and then compare the result with standard array multiplication.
  • Write a Numpy program to conduct a masked multiplication on a 2D array and then fill the resulting masked values with zeros.
  • Write a Numpy program to apply masked multiplication on two arrays and then sum the resulting unmasked products.

Go to:


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

Python-Numpy Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.