The numpy.median() method computes the median along an array's specified axis.
Example
median() Syntax
The syntax of the numpy.median() method is:
numpy.median(array, axis = None, out = None, overwrite_input = False, keepdims = <no value>)
median() Arguments
The numpy.median() method takes following arguments:
array- array containing numbers whose median we need to compute (can bearray_like)axis(optional) - axis or axes along which the medians are computed (intortuple of int)out(optional) - output array in which to place the result (ndarray)override_input(optional) -boolvalue that determines if intermediate calculations can modify an arraykeepdims(optional) - specifies whether to preserve the shape of the original array (bool)
Notes: The default values of numpy.median() have the following implications:
axis = None- the median of the entire array is taken.- By default,
keepdimswill not be passed.
median() Return Value
The numpy.median() method returns the median of the array.
Example 1: Find the median of a ndArray
Output
median of the entire array: 3.5 median across axis 0: [[2. 3.] [4. 5.]] median across axis 0 and 1 [3. 4.]
Example 2: Using Optional keepdims Argument
If keepdims is set to True, the resultant median array is of the same number of dimensions as the original array.
Output
Dimensions in original array: 2 Without keepdims: [2.5 3.5 4.5] with dimensions 1 With keepdims: [[2.5 3.5 4.5]] with dimensions 2
Example 3: Using Optional out Argument
The out parameter allows to specify an output array where the result will be stored.
Output
median: [2.5 3.5 4.5]