w3resource

Extract Unmasked data from a Masked NumPy array


15. Extract Unmasked Data

Write a NumPy program that creates a masked array and extracts the unmasked data as a regular NumPy array.

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)

# Extract the unmasked data as a regular NumPy array
unmasked_data = masked_array.compressed()

# Print the original array, the masked array, and the unmasked data
print('Original 2D array:\n', array_2d)
print('Masked array (elements > 50 are masked):\n', masked_array)
print('Unmasked data extracted as a regular NumPy array:\n', unmasked_data)

Output:

Original 2D array:
 [[20 68 13 60]
 [83 45 47 49]
 [75 81 19 71]
 [10 66 56 13]]
Masked array (elements > 50 are masked):
 [[20 -- 13 --]
 [-- 45 47 49]
 [-- -- 19 --]
 [10 -- -- 13]]
Unmasked data extracted as a regular NumPy array:
 [20 13 45 47 49 19 10 13]

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.
  • Extract Unmasked Data:
    • Used the compressed method of the masked array to extract the unmasked data as a regular NumPy array.
  • Print the original 2D array, the masked array, and the unmasked data extracted as a regular NumPy array.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to extract all unmasked elements from a masked array into a 1D regular array.
  • Write a Numpy program to retrieve the unmasked subarray from a 2D masked array and then reshape it to its original dimensions.
  • Write a Numpy program to filter a masked array for unmasked values and then compute summary statistics on the extracted data.
  • Write a Numpy program to compare the unmasked data extraction using np.ma.compressed with manual indexing of unmasked elements.

Python-Numpy Code Editor:

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

Previous: Apply custom function to Unmasked elements in a Masked NumPy array.
Next: Perform Masked sum operation in NumPy ignoring Masked elements.

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.