Creating a NumPy masked array
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) ...
Get Python Data Analysis - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.