# Python numpy.ndarray.view() Examples

The following are code examples for showing how to use numpy.ndarray.view(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
```def reduce(self, target, axis=None):
"Reduce target along the given axis."
target = narray(target, copy=False, subok=True)
if axis is not None:
kargs = {'axis': axis}
else:
kargs = {}
target = target.ravel()
m = m.ravel()
t = self.ufunc.reduce(target, **kargs)
else:
target = target.filled(
self.fill_value_func(target)).view(type(target))
t = self.ufunc.reduce(target, **kargs)
m = umath.logical_and.reduce(m, **kargs)
elif m:
return t ```
Example 2
```def reshape(a, new_shape, order='C'):
"""
Returns an array containing the same data with a new shape.

Refer to `MaskedArray.reshape` for full documentation.

--------

"""
# We can't use 'frommethod', it whine about some parameters. Dmmit.
try:
return a.reshape(new_shape, order=order)
except AttributeError:
_tmp = narray(a, copy=False).reshape(new_shape, order=order)
Example 3
```def inner(a, b):
"""
Returns the inner product of a and b for arrays of floating point types.

Like the generic NumPy equivalent the product sum is over the last dimension
of a and b.

Notes
-----
The first argument is not conjugated.

"""
fa = filled(a, 0)
fb = filled(b, 0)
if len(fa.shape) == 0:
fa.shape = (1,)
if len(fb.shape) == 0:
fb.shape = (1,)
Example 4
```def outer(self, a, b):
"""
Return the function applied to the outer product of a and b.

"""
(da, db) = (getdata(a), getdata(b))
d = self.f.outer(da, db)
else:
m = umath.logical_or.outer(ma, mb)
if (not m.ndim) and m:
np.copyto(d, da, where=m)
if not d.shape:
return d
Example 5
```def recordmask(self):
"""
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)])
array([False, False,  True, False, False])
"""

Example 6
```def _get_data(self):
"""
Returns the underlying data, as a view of the masked array.

If the underlying data is a subclass of :class:`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 :attr:`baseclass`
attribute.
"""
return ndarray.view(self, self._baseclass) ```
Example 7
```def imag(self):
"""
The imaginary part of the masked array.

This property is a view on the imaginary part of this `MaskedArray`.

--------
real

Examples
--------
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False])
>>> x.imag
fill_value=1e+20)

"""
result = self._data.imag.view(type(self))
return result

# kept for compatibility ```
Example 8
```def round(self, decimals=0, out=None):
"""
Return each element rounded to the given number of decimals.

Refer to `numpy.around` for full documentation.

--------
ndarray.around : corresponding function for ndarrays
numpy.around : equivalent function
"""
result = self._data.round(decimals=decimals, out=out).view(type(self))
if result.ndim > 0:
result._update_from(self)
# No explicit output: we're done
if out is None:
return result
return out ```
Example 9
```def __new__(cls):
if not cls.__has_singleton():
# We define the masked singleton as a float for higher precedence.
# Note that it can be tricky sometimes w/ type comparison
data = np.array(0.)

# prevent any modifications
data.flags.writeable = False

# that might confuse it - this way, the construction is entirely
# within our control

return cls.__singleton ```
Example 10
```def diag(v, k=0):
"""
Extract a diagonal or construct a diagonal array.

This function is the equivalent of `numpy.diag` that takes masked
values into account, see `numpy.diag` for details.

--------
numpy.diag : Equivalent function for ndarrays.

"""
return output ```
Example 11
```def reshape(a, new_shape, order='C'):
"""
Returns an array containing the same data with a new shape.

Refer to `MaskedArray.reshape` for full documentation.

--------

"""
# We can't use 'frommethod', it whine about some parameters. Dmmit.
try:
return a.reshape(new_shape, order=order)
except AttributeError:
_tmp = narray(a, copy=False).reshape(new_shape, order=order)
Example 12
```def reduce(self, target, axis=0, dtype=None):
"""
Reduce `target` along the given `axis`.

"""
t = filled(target, self.filly)
if t.shape == ():
t = t.reshape(1)
m.shape = (1,)

tr = self.f.reduce(t, axis)
else:
tr = self.f.reduce(t, axis, dtype=dtype or t.dtype)
mr = umath.logical_and.reduce(m, axis)

if not tr.shape:
if mr:
else:
return tr
Example 13
```def outer(self, a, b):
"""
Return the function applied to the outer product of a and b.

"""
(da, db) = (getdata(a), getdata(b))
d = self.f.outer(da, db)
else:
m = umath.logical_or.outer(ma, mb)
if (not m.ndim) and m:
np.copyto(d, da, where=m)
if not d.shape:
return d
Example 14
```def __call__(self, a, b, *args, **kwargs):
"Execute the call behavior."
# Get the data
(da, db) = (getdata(a), getdata(b))
# Get the result
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(da, db, *args, **kwargs)
# Get the mask as a combination of the source masks and invalid
m = ~umath.isfinite(result)
# Apply the domain
domain = ufunc_domain.get(self.f, None)
if domain is not None:
m |= domain(da, db)
# Take care of the scalar case first
if (not m.ndim):
if m:
else:
return result
# When the mask is True, put back da if possible
# any errors, just abort; impossible to guarantee masked values
try:
np.copyto(result, 0, casting='unsafe', where=m)
# avoid using "*" since this may be overlaid
# only add back if it can be cast safely
except:
pass

# Transforms to a (subclass of) MaskedArray
Example 15
```def __getitem__(self, indx):
result = self.dataiter.__getitem__(indx).view(type(self.ma))
# set shape to match that of data; this is needed for matrices
return result

# This won't work if ravel makes a copy ```
Example 16
```def astype(self, newtype):
"""
Returns a copy of the MaskedArray cast to given newtype.

Returns
-------
A copy of self cast to input newtype.
The returned record shape matches self.shape.

Examples
--------
>>> x = np.ma.array([[1,2,3.1],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> print(x)
[[1.0 -- 3.1]
[-- 5.0 --]
[7.0 -- 9.0]]
>>> print(x.astype(int32))
[[1 -- 3]
[-- 5 --]
[7 -- 9]]

"""
newtype = np.dtype(newtype)
output = self._data.astype(newtype).view(type(self))
output._update_from(self)
names = output.dtype.names
if names is None:
else:
else:
# Don't check _fill_value if it's None, that'll speed things up
if self._fill_value is not None:
output._fill_value = _check_fill_value(self._fill_value, newtype)
return output ```
Example 17
```def __setattr__(self, attr, value):
# Try to reset the shape of the mask (if we don't have a void)
# This raises a ValueError if the dtype change won't work
try:
except (AttributeError, TypeError):
pass ```
Example 18
```def _get_recordmask(self):
"""
Return the mask of the records.

"""
Example 19
```def _get_data(self):
"""Return the current data, as a view of the original
underlying data.

"""
return ndarray.view(self, self._baseclass) ```
Example 20
```def __ne__(self, other):
"""
Check whether other doesn't equal self elementwise

"""
check = self.filled(0).__ne__(other)
try:
check = check.view(type(self))
except AttributeError:
# In case check is a boolean (or a numpy.bool)
return check
else:
odata = filled(other, 0)
check = self.filled(0).__ne__(odata).view(type(self))
else:
axis = 1
else:
axis = None
try:
except ValueError:
return check ```
Example 21
```def get_imag(self):
"""
Return the imaginary part of the masked array.

The returned array is a view on the imaginary part of the `MaskedArray`
whose `get_imag` method is called.

Parameters
----------
None

Returns
-------
The imaginary part of the masked array.

--------
get_real, real, imag

Examples
--------
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False])
>>> x.get_imag()
fill_value = 1e+20)

"""
result = self._data.imag.view(type(self))
return result ```
Example 22
```def get_real(self):
"""
Return the real part of the masked array.

The returned array is a view on the real part of the `MaskedArray`
whose `get_real` method is called.

Parameters
----------
None

Returns
-------
The real part of the masked array.

--------
get_imag, real, imag

Examples
--------
>>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False])
>>> x.get_real()
fill_value = 1e+20)

"""
result = self._data.real.view(type(self))
return result ```
Example 23
```def any(self, axis=None, out=None, keepdims=np._NoValue):
"""
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.

--------
ndarray.any : corresponding function for ndarrays
numpy.any : equivalent function

"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

if out is None:
d = self.filled(False).any(axis=axis, **kwargs).view(type(self))
if d.ndim:
return d
self.filled(False).any(axis=axis, out=out, **kwargs)
return out ```
Example 24
```def cumsum(self, axis=None, dtype=None, out=None):
"""
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.

Notes
-----
The mask is lost if `out` is not a valid :class:`MaskedArray` !

Arithmetic is modular when using integer types, and no error is
raised on overflow.

--------
ndarray.cumsum : corresponding function for ndarrays
numpy.cumsum : equivalent function

Examples
--------
>>> print(marr.cumsum())
[0 1 3 -- -- -- 9 16 24 33]

"""
result = self.filled(0).cumsum(axis=axis, dtype=dtype, out=out)
if out is not None:
return out
result = result.view(type(self))
return result ```
Example 25
```def prod(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
"""
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.

Notes
-----
Arithmetic is modular when using integer types, and no error is raised
on overflow.

--------
ndarray.prod : corresponding function for ndarrays
numpy.prod : equivalent function
"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

# No explicit output
if out is None:
result = self.filled(1).prod(axis, dtype=dtype, **kwargs)
rndim = getattr(result, 'ndim', 0)
if rndim:
result = result.view(type(self))
return result
# Explicit output
result = self.filled(1).prod(axis, dtype=dtype, out=out, **kwargs)
return out ```
Example 26
```def cumprod(self, axis=None, dtype=None, out=None):
"""
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.

Notes
-----
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.

--------
ndarray.cumprod : corresponding function for ndarrays
numpy.cumprod : equivalent function
"""
result = self.filled(1).cumprod(axis=axis, dtype=dtype, out=out)
if out is not None:
return out
result = result.view(type(self))
return result ```
Example 27
```def argmin(self, axis=None, fill_value=None, out=None):
"""
Return array of indices to the minimum values along the given axis.

Parameters
----------
axis : {None, integer}
If None, the index is into the flattened array, otherwise along
the specified axis
fill_value : {var}, optional
Value used to fill in the masked values.  If None, the output of
out : {None, array}, optional
Array into which the result can be placed. Its type is preserved
and it must be of the right shape to hold the output.

Returns
-------
ndarray or scalar
If multi-dimension input, returns a new ndarray of indices to the
minimum values along the given axis.  Otherwise, returns a scalar
of index to the minimum values along the given axis.

Examples
--------
>>> x.shape = (2,2)
>>> print(x)
[[-- --]
[2 3]]
>>> print(x.argmin(axis=0, fill_value=-1))
[0 0]
>>> print(x.argmin(axis=0, fill_value=9))
[1 1]

"""
if fill_value is None:
fill_value = minimum_fill_value(self)
d = self.filled(fill_value).view(ndarray)
return d.argmin(axis, out=out) ```
Example 28
```def argmax(self, axis=None, fill_value=None, out=None):
"""
Returns array of indices of the maximum values along the given axis.

Parameters
----------
axis : {None, integer}
If None, the index is into the flattened array, otherwise along
the specified axis
fill_value : {var}, optional
Value used to fill in the masked values.  If None, the output of
out : {None, array}, optional
Array into which the result can be placed. Its type is preserved
and it must be of the right shape to hold the output.

Returns
-------
index_array : {integer_array}

Examples
--------
>>> a = np.arange(6).reshape(2,3)
>>> a.argmax()
5
>>> a.argmax(0)
array([1, 1, 1])
>>> a.argmax(1)
array([2, 2])

"""
if fill_value is None:
fill_value = maximum_fill_value(self._data)
d = self.filled(fill_value).view(ndarray)
return d.argmax(axis, out=out) ```
Example 29
```def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
cls = type(self)
# Make sure the indices are not masked
indices = indices.filled(0)
# Get the data, promoting scalars to 0d arrays with [...] so that
# .view works correctly
if out is None:
out = _data.take(indices, axis=axis, mode=mode)[...].view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
else:
# demote 0d arrays back to scalars, for consistency with ndarray.take
return out[()]

# Array methods ```
Example 30
```def __new__(self, data, mask=nomask, dtype=None, fill_value=None,
_data = np.array(data, copy=copy, subok=subok, dtype=dtype)
_data = _data.view(self)
else:
try:
except TypeError:
# Transform the mask to a void
if fill_value is not None:
_data.fill_value = fill_value
return _data ```
Example 31
```def __new__(self):
return self._data.view(self) ```
Example 32
```def outer(self, a, b):
"Return the function applied to the outer product of a and b."
else:
m = logical_or.outer(ma, mb)
result = self.ufunc.outer(filled(a), filled(b))
return result ```
Example 33
```def putmask(a, mask, values):  # , mode='raise'):
"""
Changes elements of an array based on conditional and input values.

--------

Notes
-----
Using a masked array as `values` will **not** transform a `ndarray` into

"""
# We can't use 'frommethod', the order of arguments is different
else:
return ```
Example 34
```def transpose(a, axes=None):
"""
Permute the dimensions of an array.

This function is exactly equivalent to `numpy.transpose`.

--------
numpy.transpose : Equivalent function in top-level NumPy module.

Examples
--------
>>> import numpy.ma as ma
>>> x = ma.arange(4).reshape((2,2))
>>>> x
[[0 1]
[2 --]],
[[False False]
[False  True]],
fill_value = 999999)
>>> ma.transpose(x)
[[0 2]
[1 --]],
[[False False]
[False  True]],
fill_value = 999999)

"""
# We can't use 'frommethod', as 'transpose' doesn't take keywords
try:
return a.transpose(axes)
except AttributeError:
Example 35
```def reduce(self, target, axis=0, dtype=None):
"""
Reduce `target` along the given `axis`.

"""
t = filled(target, self.filly)
if t.shape == ():
t = t.reshape(1)
m.shape = (1,)

tr = self.f.reduce(t, axis)
else:
tr = self.f.reduce(t, axis, dtype=dtype or t.dtype)
mr = umath.logical_and.reduce(m, axis)

if not tr.shape:
if mr:
else:
return tr
Example 36
```def __getitem__(self, indx):
result = self.dataiter.__getitem__(indx).view(type(self.ma))
# set shape to match that of data; this is needed for matrices
return result

# This won't work if ravel makes a copy ```
Example 37
```def dtype(self, dtype):
# Try to reset the shape of the mask (if we don't have a void).
# This raises a ValueError if the dtype change won't work.
try:
except (AttributeError, TypeError):
pass ```
Example 38
```def mask(self):

# We could try to force a reshape, but that wouldn't work in some
# cases.
# Return a view so that the dtype and shape cannot be changed in place
# This still preserves nomask by identity
Example 39
```def all(self, axis=None, out=None, keepdims=np._NoValue):
"""
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

Refer to `numpy.all` for full documentation.

--------
ndarray.all : corresponding function for ndarrays
numpy.all : equivalent function

Examples
--------
>>> np.ma.array([1,2,3]).all()
True
True

"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

if out is None:
d = self.filled(True).all(axis=axis, **kwargs).view(type(self))
if d.ndim:
return d
self.filled(True).all(axis=axis, out=out, **kwargs)
return out ```
Example 40
```def any(self, axis=None, out=None, keepdims=np._NoValue):
"""
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.

--------
ndarray.any : corresponding function for ndarrays
numpy.any : equivalent function

"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

if out is None:
d = self.filled(False).any(axis=axis, **kwargs).view(type(self))
if d.ndim:
return d
self.filled(False).any(axis=axis, out=out, **kwargs)
return out ```
Example 41
```def cumsum(self, axis=None, dtype=None, out=None):
"""
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.

Notes
-----
The mask is lost if `out` is not a valid :class:`MaskedArray` !

Arithmetic is modular when using integer types, and no error is
raised on overflow.

--------
ndarray.cumsum : corresponding function for ndarrays
numpy.cumsum : equivalent function

Examples
--------
>>> 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)

"""
result = self.filled(0).cumsum(axis=axis, dtype=dtype, out=out)
if out is not None:
return out
result = result.view(type(self))
return result ```
Example 42
```def cumprod(self, axis=None, dtype=None, out=None):
"""
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.

Notes
-----
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.

--------
ndarray.cumprod : corresponding function for ndarrays
numpy.cumprod : equivalent function
"""
result = self.filled(1).cumprod(axis=axis, dtype=dtype, out=out)
if out is not None:
return out
result = result.view(type(self))
return result ```
Example 43
```def argmax(self, axis=None, fill_value=None, out=None):
"""
Returns array of indices of the maximum values along the given axis.

Parameters
----------
axis : {None, integer}
If None, the index is into the flattened array, otherwise along
the specified axis
fill_value : {var}, optional
Value used to fill in the masked values.  If None, the output of
out : {None, array}, optional
Array into which the result can be placed. Its type is preserved
and it must be of the right shape to hold the output.

Returns
-------
index_array : {integer_array}

Examples
--------
>>> a = np.arange(6).reshape(2,3)
>>> a.argmax()
5
>>> a.argmax(0)
array([1, 1, 1])
>>> a.argmax(1)
array([2, 2])

"""
if fill_value is None:
fill_value = maximum_fill_value(self._data)
d = self.filled(fill_value).view(ndarray)
return d.argmax(axis, out=out) ```
Example 44
```def take(self, indices, axis=None, out=None, mode='raise'):
"""
"""
cls = type(self)
# Make sure the indices are not masked
indices = indices.filled(0)
# Get the data, promoting scalars to 0d arrays with [...] so that
# .view works correctly
if out is None:
out = _data.take(indices, axis=axis, mode=mode)[...].view(cls)
else:
np.take(_data, indices, axis=axis, mode=mode, out=out)
else:
# demote 0d arrays back to scalars, for consistency with ndarray.take
return out[()]

# Array methods ```
Example 45
```def __has_singleton(cls):
# second case ensures `cls.__singleton` is not just a view on the
# superclass singleton
return cls.__singleton is not None and type(cls.__singleton) is cls ```
Example 46
```def __array_finalize__(self, obj):
if not self.__has_singleton():
# this handles the `.view` in __new__, which we want to copy across
# properties normally
elif self is self.__singleton:
# not clear how this can happen, play it safe
pass
else:
# everywhere else, we want to downcast to MaskedArray, to prevent a
Example 47
```def __array_prepare__(self, obj, context=None):
Example 48
```def __array_wrap__(self, obj, context=None):
Example 49
```def outer(self, a, b):
"Return the function applied to the outer product of a and b."
else:
m = logical_or.outer(ma, mb)
result = self.f.outer(filled(a), filled(b))
return result ```
Example 50
```def putmask(a, mask, values):  # , mode='raise'):
"""
Changes elements of an array based on conditional and input values.

--------

Notes
-----
Using a masked array as `values` will **not** transform a `ndarray` into

"""
# We can't use 'frommethod', the order of arguments is different
else:
return ```
Example 51
```def inner(a, b):
"""
Returns the inner product of a and b for arrays of floating point types.

Like the generic NumPy equivalent the product sum is over the last dimension
of a and b. The first argument is not conjugated.

"""
fa = filled(a, 0)
fb = filled(b, 0)
if fa.ndim == 0:
fa.shape = (1,)
if fb.ndim == 0:
fb.shape = (1,)
Example 52
```def __call__(self, *args, **params):
# Find the common parameters to the call and the definition
_extras = self._extras
common_params = set(params).intersection(_extras)
# Drop the common parameters from the call
for p in common_params:
_extras[p] = params.pop(p)
# Get the result
if "fill_value" in common_params:
result.fill_value = _extras.get("fill_value", None)
return result ```
Example 53
```def getdata(a, subok=True):
"""
Return the data of a masked array as an ndarray.

Return the data of `a` (if any) as an ndarray if `a` is a ``MaskedArray``,
else return `a` as a ndarray or subclass (depending on `subok`) if not.

Parameters
----------
a : array_like
Input ``MaskedArray``, alternatively a ndarray or a subclass thereof.
subok : bool
Whether to force the output to be a `pure` ndarray (False) or to
return a subclass of ndarray if appropriate (True, default).

--------

Examples
--------
>>> import numpy.ma as ma
>>> a
[[1 --]
[3 4]],
[[False  True]
[False False]],
fill_value=999999)
>>> ma.getdata(a)
array([[1, 2],
[3, 4]])

Equivalently use the ``MaskedArray`` `data` attribute.

>>> a.data
array([[1, 2],
[3, 4]])

"""
try:
data = a._data
except AttributeError:
data = np.array(a, copy=False, subok=subok)
if not subok:
return data.view(ndarray)
return data ```
Example 54
```def __call__(self, a, *args, **kwargs):
"""
Execute the call behavior.

"""
d = getdata(a)
# Deal with domain
if self.domain is not None:
# Case 1.1. : Domained function
# nans at masked positions cause RuntimeWarnings, even though
# they are masked. To avoid this we suppress warnings.
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)
m = ~umath.isfinite(result)
m |= self.domain(d)
else:
# Case 1.2. : Function without a domain
# Get the result and the mask
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)

if not result.ndim:
# Case 2.1. : The result is scalarscalar
if m:
return result

# Case 2.2. The result is an array
# We need to fill the invalid data back w/ the input Now,
# that's plain silly: in C, we would just skip the element and
# keep the original, but we do have to do it that way in Python

# In case result has a lower dtype than the inputs (as in
# equal)
try:
np.copyto(result, d, where=m)
except TypeError:
pass
# Transform to
Example 55
```def __call__(self, a, b, *args, **kwargs):
"""
Execute the call behavior.

"""
# Get the data, as ndarray
(da, db) = (getdata(a), getdata(b))
# Get the result
with np.errstate():
np.seterr(divide='ignore', invalid='ignore')
result = self.f(da, db, *args, **kwargs)
# Get the mask for the result
else:
else:
m = umath.logical_or(ma, mb)

# Case 1. : scalar
if not result.ndim:
if m:
return result

# Case 2. : array
# Revert result to da where masked
if m is not nomask and m.any():
# any errors, just abort; impossible to guarantee masked values
try:
np.copyto(result, da, casting='unsafe', where=m)
except:
pass

# Transforms to a (subclass of) MaskedArray
Example 56
```def masked_invalid(a, copy=True):
"""
Mask an array where invalid values occur (NaNs or infs).

This function is a shortcut to ``masked_where``, with
`condition` = ~(np.isfinite(a)). Any pre-existing mask is conserved.
Only applies to arrays with a dtype where NaNs or infs make sense
(i.e. floating point types), but accepts any array_like object.

--------

Examples
--------
>>> import numpy.ma as ma
>>> a = np.arange(5, dtype=np.float)
>>> a[2] = np.NaN
>>> a[3] = np.PINF
>>> a
array([  0.,   1.,  NaN,  Inf,   4.])
masked_array(data = [0.0 1.0 -- -- 4.0],
mask = [False False  True  True False],
fill_value=1e+20)

"""
a = np.array(a, copy=copy, subok=True)
condition = ~(np.isfinite(getdata(a)))
cls = type(a)
else:
condition = ~(np.isfinite(a))
result = a.view(cls)
return result

###############################################################################
#                            Printing options                                 #
############################################################################### ```
Example 57
```def flatten_structured_array(a):
"""
Flatten a structured array.

The data type of the output is chosen such that it can represent all of the
(nested) fields.

Parameters
----------
a : structured array

Returns
-------
output : masked array or ndarray
A flattened masked array if the input is a masked array, otherwise a
standard ndarray.

Examples
--------
>>> ndtype = [('a', int), ('b', float)]
>>> a = np.array([(1, 1), (2, 2)], dtype=ndtype)
>>> flatten_structured_array(a)
array([[1., 1.],
[2., 2.]])

"""

def flatten_sequence(iterable):
"""
Flattens a compound of nested iterables.

"""
for elm in iter(iterable):
if hasattr(elm, '__iter__'):
for f in flatten_sequence(elm):
yield f
else:
yield elm

a = np.asanyarray(a)
inishape = a.shape
a = a.ravel()
out = np.array([tuple(flatten_sequence(d.item())) for d in a._data])
else:
out = np.array([tuple(flatten_sequence(d.item())) for d in a])
if len(inishape) > 1:
newshape = list(out.shape)
newshape[0] = inishape
out.shape = tuple(flatten_sequence(newshape))
return out ```
Example 58
```def _arraymethod(funcname, onmask=True):
"""
Return a class method wrapper around a basic array method.

Creates a class method which returns a masked array, where the new
``_data`` array is the output of the corresponding basic method called
on the original ``_data``.

If `onmask` is True, the new mask is the output of the method called
on the initial mask. Otherwise, the new mask is just a reference

Parameters
----------
funcname : str
Name of the function to apply on data.
Whether the mask must be processed also (True) or left
alone (False). Default is True. Make available as `_onmask`
attribute.

Returns
-------
method : instancemethod
Class method wrapper of the specified basic array method.

"""
def wrapped_method(self, *args, **params):
result = getattr(self._data, funcname)(*args, **params)
result = result.view(type(self))
result._update_from(self)
if result.ndim:
else:
return result
methdoc = getattr(ndarray, funcname, None) or getattr(np, funcname, None)
if methdoc is not None:
wrapped_method.__doc__ = methdoc.__doc__
wrapped_method.__name__ = funcname
return wrapped_method ```
Example 59
```def __str__(self):
"""
String representation.

"""
return str(f)
res = self._data
else:
if m.shape == () and m.itemsize==len(m.dtype):
if m.dtype.names:
m = m.view((bool, len(m.dtype)))
if m.any():
return str(tuple((f if _m else _d) for _d, _m in
zip(self._data.tolist(), m)))
else:
return str(self._data)
elif m:
return str(f)
else:
return str(self._data)
# convert to object array to make filled work
names = self.dtype.names
if names is None:
data = self._data
# For big arrays, to avoid a costly conversion to the
# object dtype, extract the corners before the conversion.
print_width = (self._print_width if self.ndim > 1
else self._print_width_1d)
for axis in range(self.ndim):
if data.shape[axis] > print_width:
ind = print_width // 2
arr = np.split(data, (ind, -ind), axis=axis)
data = np.concatenate((arr[0], arr[2]), axis=axis)
arr = np.split(mask, (ind, -ind), axis=axis)
res = data.astype("O")
else:
rdtype = _recursive_make_descr(self.dtype, "O")
res = self._data.astype(rdtype)
_recursive_printoption(res, m, f)
else:
res = self.filled(self.fill_value)
return str(res) ```
Example 60
```def ravel(self, order='C'):
"""
Returns a 1D version of self, as a view.

Parameters
----------
order : {'C', 'F', 'A', 'K'}, optional
The elements of `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.

Returns
-------
Output view is of shape ``(self.size,)`` (or
``(np.ma.product(self.shape),)``).

Examples
--------
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> print(x)
[[1 -- 3]
[-- 5 --]
[7 -- 9]]
>>> print(x.ravel())
[1 -- 3 -- 5 -- 7 -- 9]

"""
r = ndarray.ravel(self._data, order=order).view(type(self))
r._update_from(self)
else:
return r ```
Example 61
```def reshape(self, *s, **kwargs):
"""
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.

Parameters
----------
shape : int or tuple of ints
The new shape should be compatible with the original shape. If an
integer is supplied, then the result will be a 1-D array of that
length.
order : {'C', 'F'}, optional
Determines whether the array data should be viewed as in C
(row-major) or FORTRAN (column-major) order.

Returns
-------
reshaped_array : array
A new view on the array.

--------
reshape : Equivalent function in the masked array module.
numpy.ndarray.reshape : Equivalent method on ndarray object.
numpy.reshape : Equivalent function in the NumPy module.

Notes
-----
The reshaping operation cannot guarantee that a copy will not be made,
to modify the shape in place, use ``a.shape = s``

Examples
--------
>>> print(x)
[[-- 2]
[3 --]]
>>> x = x.reshape((4,1))
>>> print(x)
[[--]
[2]
[3]
[--]]

"""
kwargs.update(order=kwargs.get('order', 'C'))
result = self._data.reshape(*s, **kwargs).view(type(self))
result._update_from(self)
return result ```
Example 62
```def sum(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
"""
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.

--------
ndarray.sum : corresponding function for ndarrays
numpy.sum : equivalent function

Examples
--------
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> print(x)
[[1 -- 3]
[-- 5 --]
[7 -- 9]]
>>> print(x.sum())
25
>>> print(x.sum(axis=1))
[4 5 16]
>>> print(x.sum(axis=0))
[8 5 12]
>>> print(type(x.sum(axis=0, dtype=np.int64)[0]))
<type 'numpy.int64'>

"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

# No explicit output
if out is None:
result = self.filled(0).sum(axis, dtype=dtype, **kwargs)
rndim = getattr(result, 'ndim', 0)
if rndim:
result = result.view(type(self))
return result
# Explicit output
result = self.filled(0).sum(axis, dtype=dtype, out=out, **kwargs)
return out ```
Example 63
```def argsort(self, axis=None, kind='quicksort', order=None, fill_value=None):
"""
Return an ndarray of indices that sort the array along the
specified axis.  Masked values are filled beforehand to
`fill_value`.

Parameters
----------
axis : int, optional
Axis along which to sort.  The default is -1 (last axis).
If None, the flattened array is used.
fill_value : var, optional
Value used to fill the array before sorting.
The default is the `fill_value` attribute of the input array.
kind : {'quicksort', 'mergesort', 'heapsort'}, optional
Sorting algorithm.
order : list, optional
When `a` is an array with fields defined, this argument specifies
which fields to compare first, second, etc.  Not all fields need be
specified.

Returns
-------
index_array : ndarray, int
Array of indices that sort `a` along the specified axis.
In other words, ``a[index_array]`` yields a sorted `a`.

--------
sort : Describes sorting algorithms used.
lexsort : Indirect stable sort with multiple keys.
ndarray.sort : Inplace sort.

Notes
-----
See `sort` for notes on the different sorting algorithms.

Examples
--------
>>> a = np.ma.array([3,2,1], mask=[False, False, True])
>>> a
fill_value = 999999)
>>> a.argsort()
array([1, 0, 2])

"""
if fill_value is None:
fill_value = default_fill_value(self)
d = self.filled(fill_value).view(ndarray)
return d.argsort(axis=axis, kind=kind, order=order) ```
Example 64
```def power(a, b, third=None):
"""
Returns element-wise base array raised to power from second array.

This is the masked array version of `numpy.power`. For details see
`numpy.power`.

--------
numpy.power

Notes
-----
The *out* argument to `numpy.power` is not supported, `third` has to be
None.

"""
if third is not None:
# Get the rawdata
fa = getdata(a)
fb = getdata(b)
# Get the type of the result (so that we preserve subclasses)
basetype = type(a)
else:
# Get the result and view it as a (subclass of) MaskedArray
with np.errstate(divide='ignore', invalid='ignore'):
result = np.where(m, fa, umath.power(fa, fb)).view(basetype)
result._update_from(a)
# Find where we're in trouble w/ NaNs and Infs
invalid = np.logical_not(np.isfinite(result.view(ndarray)))
if not (result.ndim):
# Fix the invalid parts
if invalid.any():
if not result.ndim:
result._data[invalid] = result.fill_value
return result ```
Example 65
```def concatenate(arrays, axis=0):
"""
Concatenate a sequence of arrays along the given axis.

Parameters
----------
arrays : sequence of array_like
The arrays must have the same shape, except in the dimension
corresponding to `axis` (the first, by default).
axis : int, optional
The axis along which the arrays will be joined. Default is 0.

Returns
-------
The concatenated array with any masked entries preserved.

--------
numpy.concatenate : Equivalent function in the top-level NumPy module.

Examples
--------
>>> import numpy.ma as ma
>>> a = ma.arange(3)
>>> b = ma.arange(2, 5)
>>> a
fill_value = 999999)
>>> b
fill_value = 999999)
>>> ma.concatenate([a, b])
masked_array(data = [0 -- 2 2 3 4],
mask = [False  True False False False False],
fill_value = 999999)

"""
d = np.concatenate([getdata(a) for a in arrays], axis)
data = d.view(rcls)
# Check whether one of the arrays has a non-empty mask.
for x in arrays:
break
else:
return data
# OK, so we have to concatenate the masks
dm = np.concatenate([getmaskarray(a) for a in arrays], axis)
# If we decide to keep a '_shrinkmask' option, we want to check that
# all of them are True, and then check for dm.any()
if not dm.dtype.fields and not dm.any():
else:
return data ```
Example 66
```def expand_dims(x, axis):
"""
Expand the shape of an array.

Expands the shape of the array by including a new axis before the one
specified by the `axis` parameter. This function behaves the same as

--------
numpy.expand_dims : Equivalent function in top-level NumPy module.

Examples
--------
>>> import numpy.ma as ma
>>> x = ma.array([1, 2, 4])
>>> x
fill_value = 999999)
>>> np.expand_dims(x, axis=0)
array([[1, 2, 4]])
>>> ma.expand_dims(x, axis=0)
[[1 -- 4]],
[[False  True False]],
fill_value = 999999)

The same result can be achieved using slicing syntax with `np.newaxis`.

>>> x[np.newaxis, :]
[[1 -- 4]],
[[False  True False]],
fill_value = 999999)

"""
result = n_expand_dims(x, axis)
new_shape = result.shape
result = x.view()
result.shape = new_shape
return result ```
Example 67
```def getdata(a, subok=True):
"""
Return the data of a masked array as an ndarray.

Return the data of `a` (if any) as an ndarray if `a` is a ``MaskedArray``,
else return `a` as a ndarray or subclass (depending on `subok`) if not.

Parameters
----------
a : array_like
Input ``MaskedArray``, alternatively a ndarray or a subclass thereof.
subok : bool
Whether to force the output to be a `pure` ndarray (False) or to
return a subclass of ndarray if appropriate (True, default).

--------

Examples
--------
>>> import numpy.ma as ma
>>> a
data=[[1, --],
[3, 4]],
[False, False]],
fill_value=2)
>>> ma.getdata(a)
array([[1, 2],
[3, 4]])

Equivalently use the ``MaskedArray`` `data` attribute.

>>> a.data
array([[1, 2],
[3, 4]])

"""
try:
data = a._data
except AttributeError:
data = np.array(a, copy=False, subok=subok)
if not subok:
return data.view(ndarray)
return data ```
Example 68
```def __call__(self, a, *args, **kwargs):
"""
Execute the call behavior.

"""
d = getdata(a)
# Deal with domain
if self.domain is not None:
# Case 1.1. : Domained function
# nans at masked positions cause RuntimeWarnings, even though
# they are masked. To avoid this we suppress warnings.
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)
m = ~umath.isfinite(result)
m |= self.domain(d)
else:
# Case 1.2. : Function without a domain
# Get the result and the mask
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)

if not result.ndim:
# Case 2.1. : The result is scalarscalar
if m:
return result

# Case 2.2. The result is an array
# We need to fill the invalid data back w/ the input Now,
# that's plain silly: in C, we would just skip the element and
# keep the original, but we do have to do it that way in Python

# In case result has a lower dtype than the inputs (as in
# equal)
try:
np.copyto(result, d, where=m)
except TypeError:
pass
# Transform to
Example 69
```def __call__(self, a, b, *args, **kwargs):
"""
Execute the call behavior.

"""
# Get the data, as ndarray
(da, db) = (getdata(a), getdata(b))
# Get the result
with np.errstate():
np.seterr(divide='ignore', invalid='ignore')
result = self.f(da, db, *args, **kwargs)
# Get the mask for the result
else:
else:
m = umath.logical_or(ma, mb)

# Case 1. : scalar
if not result.ndim:
if m:
return result

# Case 2. : array
# Revert result to da where masked
if m is not nomask and m.any():
# any errors, just abort; impossible to guarantee masked values
try:
np.copyto(result, da, casting='unsafe', where=m)
except Exception:
pass

# Transforms to a (subclass of) MaskedArray
Example 70
```def __call__(self, a, b, *args, **kwargs):
"Execute the call behavior."
# Get the data
(da, db) = (getdata(a), getdata(b))
# Get the result
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(da, db, *args, **kwargs)
# Get the mask as a combination of the source masks and invalid
m = ~umath.isfinite(result)
# Apply the domain
domain = ufunc_domain.get(self.f, None)
if domain is not None:
m |= domain(da, db)
# Take care of the scalar case first
if (not m.ndim):
if m:
else:
return result
# When the mask is True, put back da if possible
# any errors, just abort; impossible to guarantee masked values
try:
np.copyto(result, 0, casting='unsafe', where=m)
# avoid using "*" since this may be overlaid
# only add back if it can be cast safely
except Exception:
pass

# Transforms to a (subclass of) MaskedArray

# Unary ufuncs ```
Example 71
```def masked_invalid(a, copy=True):
"""
Mask an array where invalid values occur (NaNs or infs).

This function is a shortcut to ``masked_where``, with
`condition` = ~(np.isfinite(a)). Any pre-existing mask is conserved.
Only applies to arrays with a dtype where NaNs or infs make sense
(i.e. floating point types), but accepts any array_like object.

--------

Examples
--------
>>> import numpy.ma as ma
>>> a = np.arange(5, dtype=float)
>>> a[2] = np.NaN
>>> a[3] = np.PINF
>>> a
array([ 0.,  1., nan, inf,  4.])
fill_value=1e+20)

"""
a = np.array(a, copy=copy, subok=True)
condition = ~(np.isfinite(getdata(a)))
cls = type(a)
else:
condition = ~(np.isfinite(a))
result = a.view(cls)
return result

###############################################################################
#                            Printing options                                 #
############################################################################### ```
Example 72
```def flatten_structured_array(a):
"""
Flatten a structured array.

The data type of the output is chosen such that it can represent all of the
(nested) fields.

Parameters
----------
a : structured array

Returns
-------
output : masked array or ndarray
A flattened masked array if the input is a masked array, otherwise a
standard ndarray.

Examples
--------
>>> ndtype = [('a', int), ('b', float)]
>>> a = np.array([(1, 1), (2, 2)], dtype=ndtype)
>>> np.ma.flatten_structured_array(a)
array([[1., 1.],
[2., 2.]])

"""

def flatten_sequence(iterable):
"""
Flattens a compound of nested iterables.

"""
for elm in iter(iterable):
if hasattr(elm, '__iter__'):
for f in flatten_sequence(elm):
yield f
else:
yield elm

a = np.asanyarray(a)
inishape = a.shape
a = a.ravel()
out = np.array([tuple(flatten_sequence(d.item())) for d in a._data])
else:
out = np.array([tuple(flatten_sequence(d.item())) for d in a])
if len(inishape) > 1:
newshape = list(out.shape)
newshape[0] = inishape
out.shape = tuple(flatten_sequence(newshape))
return out ```
Example 73
```def _arraymethod(funcname, onmask=True):
"""
Return a class method wrapper around a basic array method.

Creates a class method which returns a masked array, where the new
``_data`` array is the output of the corresponding basic method called
on the original ``_data``.

If `onmask` is True, the new mask is the output of the method called
on the initial mask. Otherwise, the new mask is just a reference

Parameters
----------
funcname : str
Name of the function to apply on data.
Whether the mask must be processed also (True) or left
alone (False). Default is True. Make available as `_onmask`
attribute.

Returns
-------
method : instancemethod
Class method wrapper of the specified basic array method.

"""
def wrapped_method(self, *args, **params):
result = getattr(self._data, funcname)(*args, **params)
result = result.view(type(self))
result._update_from(self)
# __setmask__ makes a copy, which we don't want
return result
methdoc = getattr(ndarray, funcname, None) or getattr(np, funcname, None)
if methdoc is not None:
wrapped_method.__doc__ = methdoc.__doc__
wrapped_method.__name__ = funcname
return wrapped_method ```
Example 74
```def ravel(self, order='C'):
"""
Returns a 1D version of self, as a view.

Parameters
----------
order : {'C', 'F', 'A', 'K'}, optional
The elements of `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.

Returns
-------
Output view is of shape ``(self.size,)`` (or
``(np.ma.product(self.shape),)``).

Examples
--------
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> x
data=[[1, --, 3],
[--, 5, --],
[7, --, 9]],
[ 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)

"""
r = ndarray.ravel(self._data, order=order).view(type(self))
r._update_from(self)
else:
return r ```
Example 75
```def sum(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
"""
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.

--------
ndarray.sum : corresponding function for ndarrays
numpy.sum : equivalent function

Examples
--------
>>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
>>> x
data=[[1, --, 3],
[--, 5, --],
[7, --, 9]],
[ True, False,  True],
[False,  True, False]],
fill_value=999999)
>>> x.sum()
25
>>> x.sum(axis=1)
fill_value=999999)
>>> x.sum(axis=0)
fill_value=999999)
>>> print(type(x.sum(axis=0, dtype=np.int64)[0]))
<class 'numpy.int64'>

"""
kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}

# No explicit output
if out is None:
result = self.filled(0).sum(axis, dtype=dtype, **kwargs)
rndim = getattr(result, 'ndim', 0)
if rndim:
result = result.view(type(self))
return result
# Explicit output
result = self.filled(0).sum(axis, dtype=dtype, out=out, **kwargs)
return out ```
Example 76
```def argmin(self, axis=None, fill_value=None, out=None):
"""
Return array of indices to the minimum values along the given axis.

Parameters
----------
axis : {None, integer}
If None, the index is into the flattened array, otherwise along
the specified axis
fill_value : {var}, optional
Value used to fill in the masked values.  If None, the output of
out : {None, array}, optional
Array into which the result can be placed. Its type is preserved
and it must be of the right shape to hold the output.

Returns
-------
ndarray or scalar
If multi-dimension input, returns a new ndarray of indices to the
minimum values along the given axis.  Otherwise, returns a scalar
of index to the minimum values along the given axis.

Examples
--------
>>> x.shape = (2,2)
>>> x
data=[[--, --],
[2, 3]],
[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])

"""
if fill_value is None:
fill_value = minimum_fill_value(self)
d = self.filled(fill_value).view(ndarray)
return d.argmin(axis, out=out) ```
Example 77
```def power(a, b, third=None):
"""
Returns element-wise base array raised to power from second array.

This is the masked array version of `numpy.power`. For details see
`numpy.power`.

--------
numpy.power

Notes
-----
The *out* argument to `numpy.power` is not supported, `third` has to be
None.

"""
if third is not None:
# Get the rawdata
fa = getdata(a)
fb = getdata(b)
# Get the type of the result (so that we preserve subclasses)
basetype = type(a)
else:
# Get the result and view it as a (subclass of) MaskedArray
with np.errstate(divide='ignore', invalid='ignore'):
result = np.where(m, fa, umath.power(fa, fb)).view(basetype)
result._update_from(a)
# Find where we're in trouble w/ NaNs and Infs
invalid = np.logical_not(np.isfinite(result.view(ndarray)))
if not (result.ndim):
# Fix the invalid parts
if invalid.any():
if not result.ndim:
result._data[invalid] = result.fill_value
return result ```
Example 78
```def concatenate(arrays, axis=0):
"""
Concatenate a sequence of arrays along the given axis.

Parameters
----------
arrays : sequence of array_like
The arrays must have the same shape, except in the dimension
corresponding to `axis` (the first, by default).
axis : int, optional
The axis along which the arrays will be joined. Default is 0.

Returns
-------
The concatenated array with any masked entries preserved.

--------
numpy.concatenate : Equivalent function in the top-level NumPy module.

Examples
--------
>>> import numpy.ma as ma
>>> a = ma.arange(3)
>>> b = ma.arange(2, 5)
>>> a
fill_value=999999)
>>> b
fill_value=999999)
>>> ma.concatenate([a, b])
masked_array(data=[0, --, 2, 2, 3, 4],
mask=[False,  True, False, False, False, False],
fill_value=999999)

"""
d = np.concatenate([getdata(a) for a in arrays], axis)
data = d.view(rcls)
# Check whether one of the arrays has a non-empty mask.
for x in arrays:
break
else:
return data
# OK, so we have to concatenate the masks
dm = np.concatenate([getmaskarray(a) for a in arrays], axis)
dm = dm.reshape(d.shape)

# If we decide to keep a '_shrinkmask' option, we want to check that
# all of them are True, and then check for dm.any()
return data ```
Example 79
```def getdata(a, subok=True):
"""
Return the data of a masked array as an ndarray.

Return the data of `a` (if any) as an ndarray if `a` is a ``MaskedArray``,
else return `a` as a ndarray or subclass (depending on `subok`) if not.

Parameters
----------
a : array_like
Input ``MaskedArray``, alternatively a ndarray or a subclass thereof.
subok : bool
Whether to force the output to be a `pure` ndarray (False) or to
return a subclass of ndarray if appropriate (True, default).

--------

Examples
--------
>>> import numpy.ma as ma
>>> a
[[1 --]
[3 4]],
[[False  True]
[False False]],
fill_value=999999)
>>> ma.getdata(a)
array([[1, 2],
[3, 4]])

Equivalently use the ``MaskedArray`` `data` attribute.

>>> a.data
array([[1, 2],
[3, 4]])

"""
try:
data = a._data
except AttributeError:
data = np.array(a, copy=False, subok=subok)
if not subok:
return data.view(ndarray)
return data ```
Example 80
```def __call__(self, a, *args, **kwargs):
"""
Execute the call behavior.

"""
d = getdata(a)
# Deal with domain
if self.domain is not None:
# Case 1.1. : Domained function
# nans at masked positions cause RuntimeWarnings, even though
# they are masked. To avoid this we suppress warnings.
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)
m = ~umath.isfinite(result)
m |= self.domain(d)
else:
# Case 1.2. : Function without a domain
# Get the result and the mask
with np.errstate(divide='ignore', invalid='ignore'):
result = self.f(d, *args, **kwargs)

if not result.ndim:
# Case 2.1. : The result is scalarscalar
if m:
return result

# Case 2.2. The result is an array
# We need to fill the invalid data back w/ the input Now,
# that's plain silly: in C, we would just skip the element and
# keep the original, but we do have to do it that way in Python

# In case result has a lower dtype than the inputs (as in
# equal)
try:
np.copyto(result, d, where=m)
except TypeError:
pass
# Transform to