Constants of the numpy.ma
module#
In addition to the MaskedArray
class, the numpy.ma
module
defines several constants.
- numpy.ma.masked#
The
masked
constant is a special case ofMaskedArray
, with a float datatype and a null shape. It is used to test whether a specific entry of a masked array is masked, or to mask one or several entries of a masked array:>>> x = ma.array([1, 2, 3], mask=[0, 1, 0]) >>> x[1] is ma.masked True >>> x[-1] = ma.masked >>> x masked_array(data=[1, --, --], mask=[False, True, True], fill_value=999999)
- numpy.ma.nomask#
Value indicating that a masked array has no invalid entry.
nomask
is used internally to speed up computations when the mask is not needed. It is represented internally asnp.False_
.
- numpy.ma.masked_print_option#
String used in lieu of missing data when a masked array is printed. By default, this string is
'--'
.Use
set_display()
to change the default string. Example usage:numpy.ma.masked_print_option.set_display('X')
replaces missing data with'X'
.
The MaskedArray
class#
A subclass of ndarray
designed to manipulate numerical arrays with missing data.
An instance of MaskedArray
can be thought as the combination of several elements:
The
data
, as a regularnumpy.ndarray
of any shape or datatype (the data).A boolean
mask
with the same shape as the data, where aTrue
value indicates that the corresponding element of the data is invalid. The special valuenomask
is also acceptable for arrays without named fields, and indicates that no data is invalid.A
fill_value
, a value that may be used to replace the invalid entries in order to return a standardnumpy.ndarray
.
Attributes and properties of masked arrays#
See also
- ma.MaskedArray.data#
Returns the underlying data, as a view of the masked array.
If the underlying data is a subclass of
numpy.ndarray
, it is returned as such.>>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]]) >>> x.data matrix([[1, 2], [3, 4]])
The type of the data can be accessed through the
baseclass
attribute.
- ma.MaskedArray.mask#
Current mask.
- ma.MaskedArray.recordmask#
Get or set the mask of the array if it has no named fields. For structured arrays, returns a ndarray of booleans where entries are
True
if all the fields are masked,False
otherwise:>>> x = np.ma.array([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)], ... mask=[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)], ... dtype=[('a', int), ('b', int)]) >>> x.recordmask array([False, False, True, False, False])
- ma.MaskedArray.fill_value#
The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.
Examples
>>> for dt in [np.int32, np.int64, np.float64, np.complex128]: ... np.ma.array([0, 1], dtype=dt).get_fill_value() ... 999999 999999 1e+20 (1e+20+0j)
>>> x = np.ma.array([0, 1.], fill_value=-np.inf) >>> x.fill_value -inf >>> x.fill_value = np.pi >>> x.fill_value 3.1415926535897931 # may vary
Reset to default:
>>> x.fill_value = None >>> x.fill_value 1e+20
- ma.MaskedArray.baseclass#
Class of the underlying data (read-only).
Share status of the mask (read-only).
- ma.MaskedArray.hardmask#
Specifies whether values can be unmasked through assignments.
By default, assigning definite values to masked array entries will unmask them. When
hardmask
isTrue
, the mask will not change through assignments.Examples
>>> x = np.arange(10) >>> m = np.ma.masked_array(x, x>5) >>> assert not m.hardmask
Since m has a soft mask, assigning an element value unmasks that element:
>>> m[8] = 42 >>> m masked_array(data=[0, 1, 2, 3, 4, 5, --, --, 42, --], mask=[False, False, False, False, False, False, True, True, False, True], fill_value=999999)
After hardening, the mask is not affected by assignments:
>>> hardened = np.ma.harden_mask(m) >>> assert m.hardmask and hardened is m >>> m[:] = 23 >>> m masked_array(data=[23, 23, 23, 23, 23, 23, --, --, 23, --], mask=[False, False, False, False, False, False, True, True, False, True], fill_value=999999)
As MaskedArray
is a subclass of ndarray
, a masked array also inherits all the attributes and properties of a ndarray
instance.
Base object if memory is from some other object. |
|
An object to simplify the interaction of the array with the ctypes module. |
|
Data-type of the array's elements. |
|
Information about the memory layout of the array. |
|
Length of one array element in bytes. |
|
Total bytes consumed by the elements of the array. |
|
Number of array dimensions. |
|
Tuple of array dimensions. |
|
Number of elements in the array. |
|
Tuple of bytes to step in each dimension when traversing an array. |
|
The imaginary part of the masked array. |
|
The real part of the masked array. |
|
Return a flat iterator, or set a flattened version of self to value. |
|
MaskedArray
methods#
See also
Conversion#
Convert to float. |
|
Convert to int. |
|
|
Return a view of the MaskedArray data. |
|
Copy of the array, cast to a specified type. |
|
Swap the bytes of the array elements |
Return all the non-masked data as a 1-D array. |
|
|
Return a copy of self, with masked values filled with a given value. |
|
Save a masked array to a file in binary format. |
Transforms a masked array into a flexible-type array. |
|
|
Return the data portion of the masked array as a hierarchical Python list. |
Transforms a masked array into a flexible-type array. |
|
|
A compatibility alias for tobytes, with exactly the same behavior. |
|
Return the array data as a string containing the raw bytes in the array. |
Shape manipulation#
For reshape, resize, and transpose, the single tuple argument may be
replaced with n
integers which will be interpreted as an n-tuple.
|
Return a copy of the array collapsed into one dimension. |
|
Returns a 1D version of self, as a view. |
|
Give a new shape to the array without changing its data. |
|
|
|
Remove axes of length one from a. |
|
Return a view of the array with axis1 and axis2 interchanged. |
|
Returns a view of the array with axes transposed. |
View of the transposed array. |
Item selection and manipulation#
For array methods that take an axis
keyword, it defaults to None.
If axis is None, then the array is treated as a 1-D array.
Any other value for axis
represents the dimension along which
the operation should proceed.
|
Returns array of indices of the maximum values along the given axis. |
|
Return array of indices to the minimum values along the given axis. |
|
Return an ndarray of indices that sort the array along the specified axis. |
|
Use an index array to construct a new array from a set of choices. |
|
Return a where condition is |
|
Return specified diagonals. |
|
Fill the array with a scalar value. |
|
Copy an element of an array to a standard Python scalar and return it. |
Return the indices of unmasked elements that are not zero. |
|
|
Set storage-indexed locations to corresponding values. |
|
Repeat elements of an array. |
|
Find indices where elements of v should be inserted in a to maintain order. |
|
Sort the array, in-place |
|
Pickling and copy#
|
Return a copy of the array. |
|
Dump a pickle of the array to the specified file. |
Returns the pickle of the array as a string. |
Calculations#
|
Returns True if all elements evaluate to True. |
|
Compute the anomalies (deviations from the arithmetic mean) along the given axis. |
|
Returns True if any of the elements of a evaluate to True. |
|
Return an array whose values are limited to |
Complex-conjugate all elements. |
|
Return the complex conjugate, element-wise. |
|
|
Return the cumulative product of the array elements over the given axis. |
|
Return the cumulative sum of the array elements over the given axis. |
|
Return the maximum along a given axis. |
|
Returns the average of the array elements along given axis. |
|
Return the minimum along a given axis. |
|
Return the product of the array elements over the given axis. |
|
Return the product of the array elements over the given axis. |
|
Return (maximum - minimum) along the given dimension (i.e. |
|
Return each element rounded to the given number of decimals. |
|
Returns the standard deviation of the array elements along given axis. |
|
Return the sum of the array elements over the given axis. |
|
Return the sum along diagonals of the array. |
|
Compute the variance along the specified axis. |
Arithmetic and comparison operations#
Comparison operators:#
|
Return self<value. |
|
Return self<=value. |
|
Return self>value. |
|
Return self>=value. |
|
Check whether other equals self elementwise. |
|
Check whether other does not equal self elementwise. |
Truth value of an array (bool()
):#
True if self else False |
Arithmetic:#
|
|
|
Add self to other, and return a new masked array. |
|
Add other to self, and return a new masked array. |
|
Subtract other from self, and return a new masked array. |
|
Subtract self from other, and return a new masked array. |
|
Multiply self by other, and return a new masked array. |
|
Multiply other by self, and return a new masked array. |
|
Divide other into self, and return a new masked array. |
|
Divide other into self, and return a new masked array. |
|
Divide self into other, and return a new masked array. |
|
Divide other into self, and return a new masked array. |
|
Divide self into other, and return a new masked array. |
|
Return self%value. |
|
Return value%self. |
|
Return divmod(self, value). |
|
Return divmod(value, self). |
|
Raise self to the power other, masking the potential NaNs/Infs |
|
Raise other to the power self, masking the potential NaNs/Infs |
|
Return self<<value. |
|
Return value<<self. |
|
Return self>>value. |
|
Return value>>self. |
|
Return self&value. |
|
Return value&self. |
|
Return self|value. |
|
Return value|self. |
|
Return self^value. |
|
Return value^self. |
Arithmetic, in-place:#
|
Add other to self in-place. |
|
Subtract other from self in-place. |
|
Multiply self by other in-place. |
|
Divide self by other in-place. |
|
True divide self by other in-place. |
|
Floor divide self by other in-place. |
|
Return self%=value. |
|
Raise self to the power other, in place. |
|
Return self<<=value. |
|
Return self>>=value. |
|
Return self&=value. |
|
Return self|=value. |
|
Return self^=value. |
Representation#
Literal string representation. |
|
Return str(self). |
|
Return the addresses of the data and mask areas. |
|
Return a boolean indicating whether the data is contiguous. |
Special methods#
For standard library functions:
Used if |
|
|
Used if |
Return the internal state of the masked array, for pickling purposes. |
|
Return a 3-tuple for pickling a MaskedArray. |
|
|
Restore the internal state of the masked array, for pickling purposes. |
Basic customization:
|
Create a new masked array from scratch. |
|
Returns either a new reference to self if dtype is not given or a new array of provided data type if dtype is different from the current dtype of the array. |
|
Special hook for ufuncs. |
Container customization: (see Indexing)
Return len(self). |
|
|
x.__getitem__(y) <==> x[y] |
|
x.__setitem__(i, y) <==> x[i]=y |
|
Delete self[key]. |
|
Return key in self. |
Specific methods#
Handling the mask#
The following methods can be used to access information about the mask or to manipulate the mask.
|
Set the mask. |
Force the mask to hard, preventing unmasking by assignment. |
|
Force the mask to soft (default), allowing unmasking by assignment. |
|
Copy the mask and set the sharedmask flag to |
|
Reduce a mask to nomask when possible. |
Handling the fill_value#
The filling value of the masked array is a scalar. |
|
|
Counting the missing elements#
|
Count the non-masked elements of the array along the given axis. |