Data is often messy and contains gaps or characters that we do not deal with often. Masked arrays can be utilized to disregard absent or invalid data points. A masked array from the numpy.ma
subpackage is a subclass of ndarray
with a mask. In this section, we will use the face
photo as the data source and act as if some of this data is corrupt. The following is the full code for the masked array example from the ch-04.ipynb
file in this book's code bundle:
import numpy import scipy import matplotlib.pyplot as plt face = scipy.misc.face() random_mask = numpy.random.randint(0, 2, size=face.shape) plt.subplot(221) plt.title("Original") plt.imshow(face) plt.axis('off') masked_array = numpy.ma.array(face, mask=random_mask) plt.subplot(222) plt.title("Masked") plt.imshow(masked_array) plt.axis('off') plt.subplot(223) plt.title("Log") ...