numpy.ma.median#
- ma.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)[source]#
Compute the median along the specified axis.
Returns the median of the array elements.
- Parameters:
- aarray_like
Input array or object that can be converted to an array.
- axisint, optional
Axis along which the medians are computed. The default (None) is to compute the median along a flattened version of the array.
- outndarray, optional
Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output but the type will be cast if necessary.
- overwrite_inputbool, optional
If True, then allow use of memory of input array (a) for calculations. The input array will be modified by the call to median. This will save memory when you do not need to preserve the contents of the input array. Treat the input as undefined, but it will probably be fully or partially sorted. Default is False. Note that, if overwrite_input is True, and the input is not already an
ndarray
, an error will be raised.- keepdimsbool, optional
If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
New in version 1.10.0.
- Returns:
See also
Notes
Given a vector
V
withN
non masked values, the median ofV
is the middle value of a sorted copy ofV
(Vs
) - i.e.Vs[(N-1)/2]
, whenN
is odd, or{Vs[N/2 - 1] + Vs[N/2]}/2
whenN
is even.Examples
>>> import numpy as np >>> x = np.ma.array(np.arange(8), mask=[0]*4 + [1]*4) >>> np.ma.median(x) 1.5
>>> x = np.ma.array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4) >>> np.ma.median(x) 2.5 >>> np.ma.median(x, axis=-1, overwrite_input=True) masked_array(data=[2.0, 5.0], mask=[False, False], fill_value=1e+20)