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 Lena Soderberg 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 masked.py
file in this book's code bundle:
import numpy import scipy import matplotlib.pyplot as plt lena = scipy.misc.lena() random_mask = numpy.random.randint(0, 2, size=lena.shape) plt.subplot(221) plt.title("Original") plt.imshow(lena) plt.axis('off') masked_array = numpy.ma.array(lena, mask=random_mask) ...
Get Python Data Analysis 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.