Using masked arrays to represent invalid data
A common issue arising when working with data or doing computations is the presence of invalid values. Such values can arise either from data that is missing or as a result of operations that resulted in inconsistent data. We may also want to mask array elements that we know would raise errors in further computations.
Masked arrays in NumPy are supported by the masked_array
class, which is defined in the numpy.ma
module. To work with masked arrays, we need first to import this module, which can be done with the following code:
import numpy.ma as ma
How to do it...
In the following sections, we will get into the details of creating a masked array from the following:
- An explicit mask
- A condition
Creating a masked array from an explicit mask
This recipe applies when we know in advance the position of an array that should be masked. The following code illustrates this situation:
x = np.array([1.0, 3.2, -2.1, 4.3, 5.4]) xm = ma.masked_array(x, [0, 0, 1, 0...