NumPy: Apply a Mask from one Array to another Array

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. NumPy: Apply a Mask from one Array to another Array
  2. NumPy: Apply a Mask from one 2D Array to another 2D Array
  3. NumPy: Apply a Mask from one Array to another Array using masked_where()

# NumPy: Apply a Mask from one Array to another Array

To apply a mask from one NumPy array to another array:

  1. Use the numpy.ma.masked_where() method to mask the first array where a condition is met.
  2. Use the numpy.ma.getmask() method to get the mask of the masked array.
  3. Use the numpy.ma.masked_where() method to mask the second array.
main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --] new_x_array = np.ma.masked_where(np.ma.getmask(masked_y_array), x) # apply the mask of `masked_y_array` to `x` print(new_x_array) # ๐Ÿ‘‰๏ธ [1 3 5 7 -- --]

numpy apply mask from one array to another array

The code for this article is available on GitHub

The numpy.ma.masked_where method masks an array where a condition is met.

In other words, the method returns the supplied array as a masked array where the condition returns True.

main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --]

Any masked values of the array are also masked in the output.

Once we've filtered out the values in the array that match the condition, we can apply the mask to the second array using numpy.ma.getmask().

The numpy.ma.getmask() method takes a masked array as a parameter and returns the mask of the masked array.

main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --] # ๐Ÿ‘‡๏ธ [False False False False True True] print(np.ma.getmask(masked_y_array))

The last step is to apply the mask to the other array by using the numpy.ma.masked_where() method.

main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --] new_x_array = np.ma.masked_where(np.ma.getmask(masked_y_array), x) # apply the mask of `masked_y_array` to `x` print(new_x_array) # ๐Ÿ‘‰๏ธ [1 3 5 7 -- --]

numpy apply mask from one array to another array

The code for this article is available on GitHub

You can apply the mask to as many arrays as necessary by using numpy.ma.getmask() as shown in the code sample.

If you need to get all non-masked data as a 1-D array, use the numpy.ma.compressed() method.

main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --] print(np.ma.compressed(masked_y_array)) # ๐Ÿ‘‰๏ธ [2 4 6 8] new_x_array = np.ma.masked_where(np.ma.getmask(masked_y_array), x) # apply the mask of `masked_y_array` to `x` print(new_x_array) # ๐Ÿ‘‰๏ธ [1 3 5 7 -- --] print(np.ma.compressed(new_x_array)) # ๐Ÿ‘‰๏ธ [1 3 5 7]

get all non masked data as 1d array

The numpy.ma.compressed() method returns all of the non-masked data in the supplied array as a 1-D array.

# NumPy: Apply a Mask from one 2D Array to another 2D Array

This approach also works if you need to apply a mask from one 2D array to another 2D array.

main.py
import numpy as np x = np.array([[1, 3], [5, 7], [9, 12]]) y = np.array([[2, 4], [6, 8], [10, 14]]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # apply the mask of `masked_y_array` to `x` new_x_array = np.ma.masked_where(np.ma.getmask(masked_y_array), x) print(new_x_array)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
[[2 4] [6 8] [-- --]] [[1 3] [5 7] [-- --]]

apply mask from one 2d array to another 2d array

# NumPy: Apply a Mask from one Array to another Array using masked_where()

You can also use the numpy.ma.masked_where() method twice, with the same condition to apply a mask from one array to another.

main.py
import numpy as np x = np.array([1, 3, 5, 7, 9, 12]) y = np.array([2, 4, 6, 8, 10, 14]) masked_y_array = np.ma.masked_where(y > 8, y) # filter out values in `y` that are greater than 8 print(masked_y_array) # ๐Ÿ‘‰๏ธ [2 4 6 8 -- --] print(np.ma.compressed(masked_y_array)) # ๐Ÿ‘‰๏ธ [2 4 6 8] new_x_array = np.ma.masked_where(y > 8, x) # apply the mask of `masked_y_array` to `x` print(new_x_array) # ๐Ÿ‘‰๏ธ [1 3 5 7 -- --] print(np.ma.compressed(new_x_array)) # ๐Ÿ‘‰๏ธ [1 3 5 7]
The code for this article is available on GitHub

We first masked the y array using the condition y > 8, effectively filtering out the values in y that are greater than 8.

main.py
masked_y_array = np.ma.masked_where(y > 8, y)

We then called the masked_where() method a second time, with the same condition (y > 8).

main.py
new_x_array = np.ma.masked_where(y > 8, x)

However, notice that we passed x as the second argument to the method.

This way, the mask from the y array is applied to the x array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev