bundles / numpy 2.4.3 / numpy / median
_ArrayFunctionDispatcher
numpy:median
Signature
def median ( a , axis = None , out = None , overwrite_input = False , keepdims = False ) Summary
Compute the median along the specified axis.
Extended Summary
Returns the median of the array elements.
Parameters
a: array_likeInput array or object that can be converted to an array.
axis: {int, sequence of int, None}, optionalAxis or axes along which the medians are computed. The default, axis=None, will compute the median along a flattened version of the array. If a sequence of axes, the array is first flattened along the given axes, then the median is computed along the resulting flattened axis.
out: ndarray, optionalAlternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary.
overwrite_input: bool, optionalIf True, then allow use of memory of input array
afor 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. Ifoverwrite_inputisTrueandais not already anndarray, an error will be raised.keepdims: bool, optionalIf 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 original
arr.
Returns
median: ndarrayA new array holding the result. If the input contains integers or floats smaller than
float64, then the output data-type isnp.float64. Otherwise, the data-type of the output is the same as that of the input. Ifoutis specified, that array is returned instead.
Notes
Given a vector V of length N, the median of V is the middle value of a sorted copy of V, V_sorted - i e., V_sorted[(N-1)/2], when N is odd, and the average of the two middle values of V_sorted when N is even.
Examples
import numpy as np a = np.array([[10, 7, 4], [3, 2, 1]]) a np.median(a) np.median(a, axis=0)✓
np.median(a, axis=1)
✗np.median(a, axis=(0, 1)) m = np.median(a, axis=0) out = np.zeros_like(m)✓
np.median(a, axis=0, out=m) m✗
b = a.copy()
✓np.median(b, axis=1, overwrite_input=True)
✗assert not np.all(a==b) b = a.copy() np.median(b, axis=None, overwrite_input=True) assert not np.all(a==b)✓
See also
- mean
- percentile
Aliases
-
numpy.median