Masking numpy arrays#

Masking is the term used for selecting entries in arrays, e.g. depending on its content. In order to do this, we need to use NumPy arrays.

First, we define a NumPy array. In our case, it contains numbers:

import numpy

measurements = numpy.asarray([1, 17, 25, 3, 5, 26, 12])
measurements

Next, we create a mask, e.g. a “filter” that flags all measurements which are above a given threshold:

mask = measurements > 10
mask

We can apply that mask to our data to retrieve a new array that only contains the desired values.

measurements[mask]

Note that the shape of the masked array is not the same as the original array. If you want that, you need to specify what to do with the missing values. Let’s see an example.

First, we will create a copy of the original array:

filtered_measurements = measurements.copy()

Then, we apply the mask and set every value that does not correspond to the mask to -1:

filtered_measurements[~mask] = -1

Now:

measurements.shape, filtered_measurements.shape
measurements
filtered_measurements

Exercise 1#

Create a new mask for all measurements below 20.

Apply the mask to retrieve a new array with numbers below 20.

Compute the average of all numbers below 20.

Extra example#

Masks can also be N-dimensional:

from scipy.datasets import face
face()
image = face()
image.shape
import matplotlib.pyplot as plt
plt.imshow(image)
image_mask = image > 200  # Change the threshold to the value you prefer!
image_mask.shape
image[image_mask]
image[image_mask].shape

To apply the mask with the same shape as the original image, we’ll first create a copy.

new_image = image.copy()
new_image[~image_mask] = 0
plt.imshow(new_image)

Exercise 2#

Manipulate the grayscale version of the face using masks.

Hint: Here’s the grayscale version of the face image used in the example above:

gray_image = face(gray=True)
plt.gray()
plt.imshow(gray_image)
gray_face = gray_image.copy()
gray_face
gray_face.max(), gray_face.min()
gray_face.shape
image_mask = gray_face < ___  # Fill your threshold value here!
gray_face[~image_mask] = ___  # Choose a value to insert here
plt.imshow(gray_face)

Further reading#

For more complex masking operations, you can use the numpy.ma module: https://numpy.org/numpy-tutorials/content/tutorial-ma.html