Masked arrays can be used to ignore missing or invalid data items. A MaskedArray
class from the numpy.ma
module is a subclass of ndarray
, with a mask. We will use the Lena Söderberg image as the data source and pretend that some of this data is corrupt. Finally, we will plot the original image, log values of the original image, the masked array, and log values thereof.
Let's create the masked array:
To create a masked array, we need to specify a mask. Create a random mask with values that are either
0
or1
:random_mask = np.random.randint(0, 2, size=lena.shape)
Using the mask from the previous step, create a masked array:
masked_array = np.ma.array(lena, mask=random_mask)
The following is the complete code for this masked array tutorial:
from __future__ import print_function import numpy as np from scipy.misc import lena import matplotlib.pyplot as plt lena = lena() random_mask = np.random.randint(0, 2, size=lena.shape) plt.subplot(221) plt.title("Original...