class MaskedArray(ndarray):
Known subclasses: numpy.ma.core.MaskedConstant
, numpy.ma.core.mvoid
, numpy.ma.tests.test_subclassing.MSubArray
, numpy.ma.tests.test_subclassing.SubMaskedArray
, numpy.matrixlib.tests.test_masked_matrix.MMatrix
An array class with possibly masked values.
Masked values of True exclude the corresponding element from any computation.
Construction:
x = MaskedArray(data, mask=nomask, dtype=None, copy=False, subok=True, ndmin=0, fill_value=None, keep_mask=True, hard_mask=None, shrink=True, order=None)
data
. True indicates a masked (i.e. invalid) data.dtype
is None, the type of the data argument (data.dtype)
is used. If dtype
is not None and different from data.dtype,
a copy is performed.MaskedArray
if possible (True) or a
plain MaskedArray
. Default is True.mask
with the mask of the input data, if any
(True), or to use only mask
for the output (False). Default is True.The mask can be initialized with an array of boolean values with the same shape as data.
>>> data = np.arange(6).reshape((2, 3)) >>> np.ma.MaskedArray(data, mask=[[False, True, False], ... [False, False, True]]) masked_array( data=[[0, --, 2], [3, 4, --]], mask=[[False, True, False], [False, False, True]], fill_value=999999)
Alternatively, the mask can be initialized to homogeneous boolean array with the same shape as data by passing in a scalar boolean value:
>>> np.ma.MaskedArray(data, mask=False) masked_array( data=[[0, 1, 2], [3, 4, 5]], mask=[[False, False, False], [False, False, False]], fill_value=999999)
>>> np.ma.MaskedArray(data, mask=True) masked_array( data=[[--, --, --], [--, --, --]], mask=[[ True, True, True], [ True, True, True]], fill_value=999999, dtype=int64)
Note
The recommended practice for initializing mask with a scalar
boolean value is to use True/False rather than
np.True_/np.False_. The reason is nomask
is represented internally as np.False_.
>>> np.False_ is np.ma.nomask True
Constant | T |
Undocumented |
Method | __add__ |
Add self to other, and return a new masked array. |
Method | __array_finalize__ |
Finalizes the masked array. |
Method | __array_wrap__ |
Special hook for ufuncs. |
Method | __deepcopy__ |
Undocumented |
Method | __div__ |
Divide other into self, and return a new masked array. |
Method | __eq__ |
Check whether other equals self elementwise. |
Method | __float__ |
Convert to float. |
Method | __floordiv__ |
Divide other into self, and return a new masked array. |
Method | __getitem__ |
x.__getitem__(y) <==> x[y] |
Method | __getstate__ |
Return the internal state of the masked array, for pickling purposes. |
Method | __iadd__ |
Add other to self in-place. |
Method | __idiv__ |
Divide self by other in-place. |
Method | __ifloordiv__ |
Floor divide self by other in-place. |
Method | __imul__ |
Multiply self by other in-place. |
Method | __int__ |
Convert to int. |
Method | __ipow__ |
Raise self to the power other, in place. |
Method | __isub__ |
Subtract other from self in-place. |
Method | __itruediv__ |
True divide self by other in-place. |
Method | __mul__ |
Multiply self by other, and return a new masked array. |
Method | __ne__ |
Check whether other does not equal self elementwise. |
Method | __new__ |
Create a new masked array from scratch. |
Method | __pow__ |
Raise self to the power other, masking the potential NaNs/Infs |
Method | __radd__ |
Add other to self, and return a new masked array. |
Method | __reduce__ |
Return a 3-tuple for pickling a MaskedArray. |
Method | __repr__ |
Literal string representation. |
Method | __rfloordiv__ |
Divide self into other, and return a new masked array. |
Method | __rmul__ |
Multiply other by self, and return a new masked array. |
Method | __rpow__ |
Raise other to the power self, masking the potential NaNs/Infs |
Method | __rsub__ |
Subtract self from other, and return a new masked array. |
Method | __rtruediv__ |
Divide self into other, and return a new masked array. |
Method | __setitem__ |
x.__setitem__(i, y) <==> x[i]=y |
Method | __setmask__ |
Set the mask. |
Method | __setstate__ |
Restore the internal state of the masked array, for pickling purposes. state is typically the output of the __getstate__ output, and is a 5-tuple: |
Method | __str__ |
Undocumented |
Method | __sub__ |
Subtract other from self, and return a new masked array. |
Method | __truediv__ |
Divide other into self, and return a new masked array. |
Method | _comparison |
Compare self with other using operator.eq or operator.ne. |
Method | _delegate_binop |
Undocumented |
Method | _get_data |
Returns the underlying data, as a view of the masked array. |
Method | _insert_masked_print |
Replace masked values with masked_print_option, casting all innermost dtypes to object. |
Method | _update_from |
Copies some attributes of obj to self. |
Method | all |
Returns True if all elements evaluate to True. |
Method | anom |
Compute the anomalies (deviations from the arithmetic mean) along the given axis. |
Method | any |
Returns True if any of the elements of a evaluate to True. |
Method | argmax |
Returns array of indices of the maximum values along the given axis. Masked values are treated as if they had the value fill_value. |
Method | argmin |
Return array of indices to the minimum values along the given axis. |
Method | argpartition |
Undocumented |
Method | argsort |
Return an ndarray of indices that sort the array along the specified axis. Masked values are filled beforehand to fill_value . |
Method | compress |
Return a where condition is True. |
Method | compressed |
Return all the non-masked data as a 1-D array. |
Method | count |
Count the non-masked elements of the array along the given axis. |
Method | cumprod |
Return the cumulative product of the array elements over the given axis. |
Method | cumsum |
Return the cumulative sum of the array elements over the given axis. |
Method | dot |
a.dot(b, out=None) |
Method | dtype.setter |
Undocumented |
Method | fill_value.setter |
Undocumented |
Method | filled |
Return a copy of self, with masked values filled with a given value. However, if there are no masked values to fill, self will be returned instead as an ndarray. |
Method | flat.setter |
Undocumented |
Method | harden_mask |
Force the mask to hard. |
Method | ids |
Return the addresses of the data and mask areas. |
Method | iscontiguous |
Return a boolean indicating whether the data is contiguous. |
Method | mask.setter |
Undocumented |
Method | max |
Return the maximum along a given axis. |
Method | mean |
Returns the average of the array elements along given axis. |
Method | min |
Return the minimum along a given axis. |
Method | mini |
Return the array minimum along the specified axis. |
Method | nonzero |
Return the indices of unmasked elements that are not zero. |
Method | partition |
Undocumented |
Method | prod |
Return the product of the array elements over the given axis. |
Method | ptp |
Return (maximum - minimum) along the given dimension (i.e. peak-to-peak value). |
Method | put |
Set storage-indexed locations to corresponding values. |
Method | ravel |
Returns a 1D version of self, as a view. |
Method | recordmask.setter |
Undocumented |
Method | reshape |
Give a new shape to the array without changing its data. |
Method | resize |
|
Method | round |
Return each element rounded to the given number of decimals. |
Method | shape.setter |
Undocumented |
Method | shrink_mask |
Reduce a mask to nomask when possible. |
Method | soften_mask |
Force the mask to soft. |
Method | sort |
Sort the array, in-place |
Method | std |
Returns the standard deviation of the array elements along given axis. |
Method | sum |
Return the sum of the array elements over the given axis. |
Method | take |
|
Method | tobytes |
Return the array data as a string containing the raw bytes in the array. |
Method | tofile |
Save a masked array to a file in binary format. |
Method | toflex |
Transforms a masked array into a flexible-type array. |
Method | tolist |
Return the data portion of the masked array as a hierarchical Python list. |
Method | tostring |
A compatibility alias for tobytes , with exactly the same behavior. |
Method | trace |
(this docstring should be overwritten) |
Method | unshare_mask |
Copy the mask and set the sharedmask flag to False. |
Method | var |
Returns the variance of the array elements along given axis. |
Method | view |
Return a view of the MaskedArray data. |
Class Variable | __array_priority__ |
Undocumented |
Class Variable | _data |
Undocumented |
Class Variable | _defaulthardmask |
Undocumented |
Class Variable | _print_width |
Undocumented |
Class Variable | _print_width_1d |
Undocumented |
Class Variable | copy |
Undocumented |
Class Variable | data |
Undocumented |
Class Variable | diagonal |
Undocumented |
Class Variable | flatten |
Undocumented |
Class Variable | repeat |
Undocumented |
Class Variable | squeeze |
Undocumented |
Class Variable | swapaxes |
Undocumented |
Class Variable | transpose |
Undocumented |
Instance Variable | _fill_value |
Undocumented |
Instance Variable | _hardmask |
Undocumented |
Instance Variable | _mask |
Undocumented |
Instance Variable | _sharedmask |
Undocumented |
Instance Variable | 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. |
Property | baseclass |
Class of the underlying data (read-only). |
Property | dtype |
Undocumented |
Property | flat |
Return a flat iterator, or set a flattened version of self to value. |
Property | hardmask |
Hardness of the mask |
Property | imag |
The imaginary part of the masked array. |
Property | mask |
Current mask. |
Property | real |
The real part of the masked array. |
Property | 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: |
Property | shape |
Undocumented |
Property | sharedmask |
Share status of the mask (read-only). |
numpy.ma.core.MaskedConstant
Special hook for ufuncs.
Wraps the numpy array and sets the mask according to context.
Check whether other equals self elementwise.
When either of the elements is masked, the result is masked as well, but the underlying boolean data are still set, with self and other considered equal if both are masked, and unequal otherwise.
For structured arrays, all fields are combined, with masked values ignored. The result is masked if all fields were masked, with self and other considered equal only if both were fully masked.
numpy.ma.core.mvoid
x.__getitem__(y) <==> x[y]
Return the item described by i, as a masked array.
Check whether other does not equal self elementwise.
When either of the elements is masked, the result is masked as well, but the underlying boolean data are still set, with self and other considered equal if both are masked, and unequal otherwise.
For structured arrays, all fields are combined, with masked values ignored. The result is masked if all fields were masked, with self and other considered equal only if both were fully masked.
numpy.ma.core.MaskedConstant
, numpy.ma.core.mvoid
, numpy.ma.tests.test_subclassing.MSubArray
, numpy.ma.tests.test_subclassing.SubMaskedArray
, numpy.matrixlib.tests.test_masked_matrix.MMatrix
Create a new masked array from scratch.
A masked array can also be created by taking a .view(MaskedArray).
numpy.ma.core.MaskedConstant
numpy.ma.core.mvoid
x.__setitem__(i, y) <==> x[i]=y
Set item described by index. If value is masked, masks those locations.
Restore the internal state of the masked array, for pickling purposes. state is typically the output of the __getstate__ output, and is a 5-tuple:
Compare self with other using operator.eq or operator.ne.
When either of the elements is masked, the result is masked as well, but the underlying boolean data are still set, with self and other considered equal if both are masked, and unequal otherwise.
For structured arrays, all fields are combined, with masked values ignored. The result is masked if all fields were masked, with self and other considered equal only if both were fully masked.
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.
Returns True if all elements evaluate to True.
The output array is masked where all the values along the given axis
are masked: if the output would have been a scalar and that all the
values are masked, then the output is masked
.
Refer to numpy.all
for full documentation.
numpy.ndarray.all : corresponding function for ndarrays numpy.all : equivalent function
>>> np.ma.array([1,2,3]).all() True >>> a = np.ma.array([1,2,3], mask=True) >>> (a.all() is np.ma.masked) True
Compute the anomalies (deviations from the arithmetic mean) along the given axis.
Returns an array of anomalies, with the same shape as the input and where the arithmetic mean is computed along the given axis.
mean : Compute the mean of the array.
>>> a = np.ma.array([1,2,3]) >>> a.anom() masked_array(data=[-1., 0., 1.], mask=False, fill_value=1e+20)
Returns True if any of the elements of a
evaluate to True.
Masked values are considered as False during computation.
Refer to numpy.any
for full documentation.
numpy.ndarray.any : corresponding function for ndarrays numpy.any : equivalent function
Returns array of indices of the maximum values along the given axis. Masked values are treated as if they had the value fill_value.
index_array : {integer_array}
>>> a = np.arange(6).reshape(2,3) >>> a.argmax() 5 >>> a.argmax(0) array([1, 1, 1]) >>> a.argmax(1) array([2, 2])
Return array of indices to the minimum values along the given axis.
>>> x = np.ma.array(np.arange(4), mask=[1,1,0,0]) >>> x.shape = (2,2) >>> x masked_array( data=[[--, --], [2, 3]], mask=[[ True, True], [False, False]], fill_value=999999) >>> x.argmin(axis=0, fill_value=-1) array([0, 0]) >>> x.argmin(axis=0, fill_value=9) array([1, 1])
Return an ndarray of indices that sort the array along the
specified axis. Masked values are filled beforehand to
fill_value
.
Axis along which to sort. If None, the default, the flattened array is used.
a
is an array with fields defined, this argument specifies
which fields to compare first, second, etc. Not all fields need be
specified.ma.MaskedArray.sort : Describes sorting algorithms used. lexsort : Indirect stable sort with multiple keys. numpy.ndarray.sort : Inplace sort.
See sort
for notes on the different sorting algorithms.
>>> a = np.ma.array([3,2,1], mask=[False, False, True]) >>> a masked_array(data=[3, 2, --], mask=[False, False, True], fill_value=999999) >>> a.argsort() array([1, 0, 2])
Return a
where condition is True.
If condition is a ~ma.MaskedArray
, missing values are considered
as False.
~ma.MaskedArray
object.Please note the difference with compressed
!
The output of compress
has a mask, the output of
compressed
does not.
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.compress([1, 0, 1]) masked_array(data=[1, 3], mask=[False, False], fill_value=999999)
>>> x.compress([1, 0, 1], axis=1) masked_array( data=[[1, 3], [--, --], [7, 9]], mask=[[False, False], [ True, True], [False, False]], fill_value=999999)
Return all the non-masked data as a 1-D array.
ndarray
holding the non-masked data is returned.The result is not a MaskedArray!
>>> x = np.ma.array(np.arange(5), mask=[0]*2 + [1]*3) >>> x.compressed() array([0, 1]) >>> type(x.compressed()) <class 'numpy.ndarray'>
Count the non-masked elements of the array along the given axis.
Axis or axes along which the count is performed.
The default, None, performs the count over all
the dimensions of the input array. axis
may be negative, in
which case it counts from the last to the first axis.
If this is a tuple of ints, the count is performed on multiple axes, instead of a single axis or all the axes as before.
axis
is None, a
scalar is returned.ma.count_masked : Count masked elements in array or along a given axis.
>>> import numpy.ma as ma >>> a = ma.arange(6).reshape((2, 3)) >>> a[1, :] = ma.masked >>> a masked_array( data=[[0, 1, 2], [--, --, --]], mask=[[False, False, False], [ True, True, True]], fill_value=999999) >>> a.count() 3
When the axis
keyword is specified an array of appropriate size is
returned.
>>> a.count(axis=0) array([1, 1, 1]) >>> a.count(axis=1) array([3, 0])
Return the cumulative product of the array elements over the given axis.
Masked values are set to 1 internally during the computation. However, their position is saved, and the result will be masked at the same locations.
Refer to numpy.cumprod
for full documentation.
The mask is lost if out
is not a valid MaskedArray !
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.cumprod : corresponding function for ndarrays numpy.cumprod : equivalent function
Return the cumulative sum of the array elements over the given axis.
Masked values are set to 0 internally during the computation. However, their position is saved, and the result will be masked at the same locations.
Refer to numpy.cumsum
for full documentation.
The mask is lost if out
is not a valid ma.MaskedArray
!
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.cumsum : corresponding function for ndarrays numpy.cumsum : equivalent function
>>> marr = np.ma.array(np.arange(10), mask=[0,0,0,1,1,1,0,0,0,0]) >>> marr.cumsum() masked_array(data=[0, 1, 3, --, --, --, 9, 16, 24, 33], mask=[False, False, False, True, True, True, False, False, False, False], fill_value=999999)
a.dot(b, out=None)
Masked dot product of two arrays. Note that out
and strict
are
located in different positions than in ma.dot
. In order to
maintain compatibility with the functional version, it is
recommended that the optional arguments be treated as keyword only.
At some point that may be mandatory.
ma.dot(a,b)
. This is a
performance feature. Therefore, if these conditions are not
met, an exception is raised, instead of attempting to be
flexible.Whether masked data are propagated (True) or set to 0 (False) for the computation. Default is False. Propagating the mask means that if a masked value appears in a row or column, the whole row or column is considered masked.
numpy.ma.dot : equivalent function
numpy.ma.core.mvoid
Return a copy of self, with masked values filled with a given value. However, if there are no masked values to fill, self will be returned instead as an ndarray.
fill_value
attribute of the array is used instead.The result is not a MaskedArray!
>>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999) >>> x.filled() array([ 1, 2, -999, 4, -999]) >>> x.filled(fill_value=1000) array([ 1, 2, 1000, 4, 1000]) >>> type(x.filled()) <class 'numpy.ndarray'>
Subclassing is preserved. This means that if, e.g., the data part of
the masked array is a recarray, filled
returns a recarray:
>>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray) >>> m = np.ma.array(x, mask=[(True, False), (False, True)]) >>> m.filled() rec.array([(999999, 2), ( -3, 999999)], dtype=[('f0', '<i8'), ('f1', '<i8')])
Force the mask to hard.
Whether the mask of a masked array is hard or soft is determined by
its ~ma.MaskedArray.hardmask
property. harden_mask
sets
~ma.MaskedArray.hardmask
to True.
ma.MaskedArray.hardmask
Return the addresses of the data and mask areas.
None
>>> x = np.ma.array([1, 2, 3], mask=[0, 1, 1]) >>> x.ids() (166670640, 166659832) # may vary
If the array has no mask, the address of nomask
is returned. This address
is typically not close to the data in memory:
>>> x = np.ma.array([1, 2, 3]) >>> x.ids() (166691080, 3083169284) # may vary
Return a boolean indicating whether the data is contiguous.
None
>>> x = np.ma.array([1, 2, 3]) >>> x.iscontiguous() True
iscontiguous
returns one of the flags of the masked array:
>>> x.flags C_CONTIGUOUS : True F_CONTIGUOUS : True OWNDATA : False WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False
Return the maximum along a given axis.
Returns the average of the array elements along given axis.
Masked entries are ignored, and result elements which are not finite will be masked.
Refer to numpy.mean
for full documentation.
numpy.ndarray.mean : corresponding function for ndarrays numpy.mean : Equivalent function numpy.ma.average : Weighted average.
>>> a = np.ma.array([1,2,3], mask=[False, False, True]) >>> a masked_array(data=[1, 2, --], mask=[False, False, True], fill_value=999999) >>> a.mean() 1.5
Return the minimum along a given axis.
minimum_fill_value
.Return the array minimum along the specified axis.
Deprecated since version 1.13.0: This function is identical to both:
- self.min(keepdims=True, axis=axis).squeeze(axis=axis)
- np.ma.minimum.reduce(self, axis=axis)
Typically though, self.min(axis=axis) is sufficient.
axis
is None, the result is a scalar. Otherwise, if axis
is
given and the array is at least 2-D, the result is a masked array with
dimension one smaller than the array on which mini
is called.>>> x = np.ma.array(np.arange(6), mask=[0 ,1, 0, 0, 0 ,1]).reshape(3, 2) >>> x masked_array( data=[[0, --], [2, 3], [4, --]], mask=[[False, True], [False, False], [False, True]], fill_value=999999) >>> x.mini() masked_array(data=0, mask=False, fill_value=999999) >>> x.mini(axis=0) masked_array(data=[0, 3], mask=[False, False], fill_value=999999) >>> x.mini(axis=1) masked_array(data=[0, 2, 4], mask=[False, False, False], fill_value=999999)
There is a small difference between mini
and min
:
>>> x[:,1].mini(axis=0) masked_array(data=3, mask=False, fill_value=999999) >>> x[:,1].min(axis=0) 3
Return the indices of unmasked elements that are not zero.
Returns a tuple of arrays, one for each dimension, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values can be obtained with:
a[a.nonzero()]
To group the indices by element, rather than dimension, use instead:
np.transpose(a.nonzero())
The result of this is always a 2d array, with a row for each non-zero element.
None
>>> import numpy.ma as ma >>> x = ma.array(np.eye(3)) >>> x masked_array( data=[[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], mask=False, fill_value=1e+20) >>> x.nonzero() (array([0, 1, 2]), array([0, 1, 2]))
Masked elements are ignored.
>>> x[1, 1] = ma.masked >>> x masked_array( data=[[1.0, 0.0, 0.0], [0.0, --, 0.0], [0.0, 0.0, 1.0]], mask=[[False, False, False], [False, True, False], [False, False, False]], fill_value=1e+20) >>> x.nonzero() (array([0, 2]), array([0, 2]))
Indices can also be grouped by element.
>>> np.transpose(x.nonzero()) array([[0, 0], [2, 2]])
A common use for nonzero is to find the indices of an array, where
a condition is True. Given an array a
, the condition a
> 3 is a
boolean array and since False is interpreted as 0, ma.nonzero(a > 3)
yields the indices of the a
where the condition is true.
>>> a = ma.array([[1,2,3],[4,5,6],[7,8,9]]) >>> a > 3 masked_array( data=[[False, False, False], [ True, True, True], [ True, True, True]], mask=False, fill_value=True) >>> ma.nonzero(a > 3) (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
The nonzero method of the condition array can also be called.
>>> (a > 3).nonzero() (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
Return the product of the array elements over the given axis.
Masked elements are set to 1 internally for computation.
Refer to numpy.prod
for full documentation.
Arithmetic is modular when using integer types, and no error is raised on overflow.
numpy.ndarray.prod : corresponding function for ndarrays numpy.prod : equivalent function
Return (maximum - minimum) along the given dimension (i.e. peak-to-peak value).
Warning
ptp
preserves the data type of the array. This means the
return value for an input of signed integers with n bits
(e.g. np.int8
, np.int16
, etc) is also a signed integer
with n bits. In that case, peak-to-peak values greater than
2**(n-1)-1 will be returned as negative values. An example
with a work-around is shown below.
>>> x = np.ma.MaskedArray([[4, 9, 2, 10], ... [6, 9, 7, 12]])
>>> x.ptp(axis=1) masked_array(data=[8, 6], mask=False, fill_value=999999)
>>> x.ptp(axis=0) masked_array(data=[2, 0, 5, 2], mask=False, fill_value=999999)
>>> x.ptp() 10
This example shows that a negative value can be returned when the input is an array of signed integers.
>>> y = np.ma.MaskedArray([[1, 127], ... [0, 127], ... [-1, 127], ... [-2, 127]], dtype=np.int8) >>> y.ptp(axis=1) masked_array(data=[ 126, 127, -128, -127], mask=False, fill_value=999999, dtype=int8)
A work-around is to use the view()
method to view the result as
unsigned integers with the same bit width:
>>> y.ptp(axis=1).view(np.uint8) masked_array(data=[126, 127, 128, 129], mask=False, fill_value=999999, dtype=uint8)
Set storage-indexed locations to corresponding values.
Sets self._data.flat[n] = values[n] for each n in indices.
If values
is shorter than indices
then it will repeat.
If values
has some masked values, the initial mask is updated
in consequence, else the corresponding values are unmasked.
values
can be a scalar or length 1 array.
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.put([0,4,8],[10,20,30]) >>> x masked_array( data=[[10, --, 3], [--, 20, --], [7, --, 30]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999)
>>> x.put(4,999) >>> x masked_array( data=[[10, --, 3], [--, 999, --], [7, --, 30]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999)
Returns a 1D version of self, as a view.
a
are read using this index order. 'C' means to
index the elements in C-like order, with the last axis index
changing fastest, back to the first axis index changing slowest.
'F' means to index the elements in Fortran-like index order, with
the first index changing fastest, and the last index changing
slowest. Note that the 'C' and 'F' options take no account of the
memory layout of the underlying array, and only refer to the order
of axis indexing. 'A' means to read the elements in Fortran-like
index order if m
is Fortran contiguous in memory, C-like order
otherwise. 'K' means to read the elements in the order they occur
in memory, except for reversing the data when strides are negative.
By default, 'C' index order is used.>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.ravel() masked_array(data=[1, --, 3, --, 5, --, 7, --, 9], mask=[False, True, False, True, False, True, False, True, False], fill_value=999999)
Give a new shape to the array without changing its data.
Returns a masked array containing the same data, but with a new shape. The result is a view on the original array; if this is not possible, a ValueError is raised.
reshape : Equivalent function in the masked array module. numpy.ndarray.reshape : Equivalent method on ndarray object. numpy.reshape : Equivalent function in the NumPy module.
The reshaping operation cannot guarantee that a copy will not be made, to modify the shape in place, use a.shape = s
>>> x = np.ma.array([[1,2],[3,4]], mask=[1,0,0,1]) >>> x masked_array( data=[[--, 2], [3, --]], mask=[[ True, False], [False, True]], fill_value=999999) >>> x = x.reshape((4,1)) >>> x masked_array( data=[[--], [2], [3], [--]], mask=[[ True], [False], [False], [ True]], fill_value=999999)
Warning
This method does nothing, except raise a ValueError exception. A
masked array does not own its data and therefore cannot safely be
resized in place. Use the numpy.ma.resize
function instead.
This method is difficult to implement safely and may be deprecated in future releases of NumPy.
Return each element rounded to the given number of decimals.
Refer to numpy.around
for full documentation.
numpy.ndarray.round : corresponding function for ndarrays numpy.around : equivalent function
Reduce a mask to nomask when possible.
None
None
>>> x = np.ma.array([[1,2 ], [3, 4]], mask=[0]*4) >>> x.mask array([[False, False], [False, False]]) >>> x.shrink_mask() masked_array( data=[[1, 2], [3, 4]], mask=False, fill_value=999999) >>> x.mask False
Force the mask to soft.
Whether the mask of a masked array is hard or soft is determined by
its ~ma.MaskedArray.hardmask
property. soften_mask
sets
~ma.MaskedArray.hardmask
to False.
ma.MaskedArray.hardmask
Sort the array, in-place
a
is a structured array, this argument specifies which fields
to compare first, second, and so on. This list does not need to
include all of the fields.a
.numpy.ndarray.sort : Method to sort an array in-place. argsort : Indirect sort. lexsort : Indirect stable sort on multiple keys. searchsorted : Find elements in a sorted array.
See sort for notes on the different sorting algorithms.
>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # Default >>> a.sort() >>> a masked_array(data=[1, 3, 5, --, --], mask=[False, False, False, True, True], fill_value=999999)
>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # Put missing values in the front >>> a.sort(endwith=False) >>> a masked_array(data=[--, --, 1, 3, 5], mask=[ True, True, False, False, False], fill_value=999999)
>>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0]) >>> # fill_value takes over endwith >>> a.sort(endwith=False, fill_value=3) >>> a masked_array(data=[1, --, --, 3, 5], mask=[False, True, True, False, False], fill_value=999999)
Returns the standard deviation of the array elements along given axis.
Masked entries are ignored.
Refer to numpy.std
for full documentation.
numpy.ndarray.std : corresponding function for ndarrays numpy.std : Equivalent function
Return the sum of the array elements over the given axis.
Masked elements are set to 0 internally.
Refer to numpy.sum
for full documentation.
numpy.ndarray.sum : corresponding function for ndarrays numpy.sum : equivalent function
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.sum() 25 >>> x.sum(axis=1) masked_array(data=[4, 5, 16], mask=[False, False, False], fill_value=999999) >>> x.sum(axis=0) masked_array(data=[8, 5, 12], mask=[False, False, False], fill_value=999999) >>> print(type(x.sum(axis=0, dtype=np.int64)[0])) <class 'numpy.int64'>
Return the array data as a string containing the raw bytes in the array.
The array is filled with a fill value before the string conversion.
MaskedArray.fill_value
is used.Order of the data item in the copy. Default is 'C'.
numpy.ndarray.tobytes tolist, tofile
As for ndarray.tobytes
, information about the shape, dtype, etc.,
but also about fill_value
, will be lost.
>>> x = np.ma.array(np.array([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]]) >>> x.tobytes() b'\x01\x00\x00\x00\x00\x00\x00\x00?B\x0f\x00\x00\x00\x00\x00?B\x0f\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'
Save a masked array to a file in binary format.
Warning
This function is not implemented yet.
tofile
is called.Transforms a masked array into a flexible-type array.
The flexible type array that is returned will have two fields:
None
ndarray
with two fields: the first element
containing a value, the second element containing the corresponding
mask boolean. The returned record shape matches self.shape.A side-effect of transforming a masked array into a flexible ndarray
is
that meta information (fill_value, ...) will be lost.
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4) >>> x masked_array( data=[[1, --, 3], [--, 5, --], [7, --, 9]], mask=[[False, True, False], [ True, False, True], [False, True, False]], fill_value=999999) >>> x.toflex() array([[(1, False), (2, True), (3, False)], [(4, True), (5, False), (6, True)], [(7, False), (8, True), (9, False)]], dtype=[('_data', '<i8'), ('_mask', '?')])
numpy.ma.core.mvoid
Return the data portion of the masked array as a hierarchical Python list.
Data items are converted to the nearest compatible Python type.
Masked values are converted to fill_value
. If fill_value
is None,
the corresponding entries in the output list will be None.
>>> x = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[0] + [1,0]*4) >>> x.tolist() [[1, None, 3], [None, 5, None], [7, None, 9]] >>> x.tolist(-999) [[1, -999, 3], [-999, 5, -999], [7, -999, 9]]
A compatibility alias for tobytes
, with exactly the same behavior.
Despite its name, it returns bytes
not str
s.
Copy the mask and set the sharedmask flag to False.
Whether the mask is shared between masked arrays can be seen from
the sharedmask
property. unshare_mask
ensures the mask is not shared.
A copy of the mask is only made if it was shared.
sharedmask
Returns the variance of the array elements along given axis.
Masked entries are ignored, and result elements which are not finite will be masked.
Refer to numpy.var
for full documentation.
numpy.ndarray.var : corresponding function for ndarrays numpy.var : Equivalent function
Return a view of the MaskedArray data.
a
. As with ndarray.view, dtype can also be specified as
an ndarray sub-class, which then specifies the type of the
returned object (this is equivalent to setting the type
parameter).dtype
, or
in its absence the original array, as discussed in the notes below.numpy.ndarray.view : Equivalent method on ndarray object.
a.view() is used two different ways:
a.view(some_dtype) or a.view(dtype=some_dtype) constructs a view of the array's memory with a different data-type. This can cause a reinterpretation of the bytes of memory.
a.view(ndarray_subclass) or a.view(type=ndarray_subclass) just
returns an instance of ndarray_subclass
that looks at the same array
(same shape, dtype, etc.) This does not cause a reinterpretation of the
memory.
If fill_value
is not specified, but dtype
is specified (and is not
an ndarray sub-class), the fill_value
of the MaskedArray will be
reset. If neither fill_value
nor dtype
are specified (or if
dtype
is an ndarray sub-class), then the fill value is preserved.
Finally, if fill_value
is specified, but dtype
is not, the fill
value is set to the specified value.
For a.view(some_dtype), if some_dtype has a different number of bytes per entry than the previous dtype (for example, converting a regular array to a structured array), then the behavior of the view cannot be predicted just from the superficial appearance of a (shown by print(a)). It also depends on exactly how a is stored in memory. Therefore if a is C-ordered versus fortran-ordered, versus defined as a slice or transpose, etc., the view may give different results.
The filling value of the masked array is a scalar. When setting, None will set to a default based on the data type.
>>> 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
The imaginary part of the masked array.
This property is a view on the imaginary part of this MaskedArray
.
real
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.imag masked_array(data=[1.0, --, 1.6], mask=[False, True, False], fill_value=1e+20)
The real part of the masked array.
This property is a view on the real part of this MaskedArray
.
imag
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False]) >>> x.real masked_array(data=[1.0, --, 3.45], mask=[False, True, False], fill_value=1e+20)
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])