w3resource

Update Mask in NumPy Masked array to include additional elements

NumPy: Masked Arrays Exercise-18 with Solution

Write a NumPy program that creates a masked array and changes the mask to include additional elements.

Sample Solution:

Python Code:

import numpy as np
import numpy.ma as ma

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

# Define an initial condition to mask elements less than 20
initial_condition = array_2d < 20

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

# Define an additional condition to mask elements greater than 80
additional_condition = array_2d > 80

# Update the mask to include additional elements
masked_array.mask = masked_array.mask | additional_condition

# Print the original array, the initial masked array, and the updated masked array
print('Original 2D array:\n', array_2d)
print('Initial masked array (values < 20 are masked):\n', masked_array)
print('Updated masked array (values < 20 or > 80 are masked):\n', masked_array)

Output:

Original 2D array:
 [[28 91 10 92 58]
 [45 35 89 92  4]
 [26 82 69 70 18]
 [29 85  3 15 84]
 [80  7 68 83 81]]
Initial masked array (values < 20 are masked):
 [[28 -- -- -- 58]
 [45 35 -- -- --]
 [26 -- 69 70 --]
 [29 -- -- -- --]
 [80 -- 68 -- --]]
Updated masked array (values < 20 or > 80 are masked):
 [[28 -- -- -- 58]
 [45 35 -- -- --]
 [26 -- 69 70 --]
 [29 -- -- -- --]
 [80 -- 68 -- --]]

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 (5, 5).
  • Define Initial Condition:
    • Define an initial condition to mask elements in the array that are less than 20.
  • Create Masked Array:
    • Create a masked array from the 2D array using ma.masked_array, applying the initial condition as the mask. Elements less than 20 are masked.
  • Define Additional Condition:
    • Define an additional condition to mask elements in the array that are greater than 80.
  • Update Mask:
    • Updated the mask to include additional elements by combining the initial mask with the additional condition using logical OR (|).
  • Print the original 2D array, the initially masked array, and the updated masked array to verify the operation.

Python-Numpy Code Editor:

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

Previous: Mask values in NumPy array based on Complex condition.
Next: Apply Mathematical function to Unmasked elements in NumPy Masked 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/update-mask-in-numpy-masked-array-to-include-additional-elements.php