Creating a masked array
Masked arrays can be used to ignore missing or invalid data items. A
MaskedArray
function from the numpy.ma
module is a subclass of ndarray
, with a mask. In this recipe, we will use the Lena Soderberg image as data source, and pretend that some of this data is corrupt. At the end, we will plot the original image, log values of the original image, the masked array, and log values thereof.
How to do it...
Let's create the masked array.
Create the masked array.
In order to create a masked array, we need to specify a mask. Let's create a random mask. This mask has values, which are either zero or one:
random_mask = numpy.random.randint(0, 2, size=lena.shape)
Create a masked array.
Using the mask in the previous step, create a masked array:
masked_array = numpy.ma.array(lena, mask=random_mask)
The following is the complete code for this masked array tutorial:
import numpy import scipy import matplotlib.pyplot lena = scipy.misc.lena() random_mask = numpy.random.randint(0, 2, size...