MCPcopy Create free account
hub / github.com/numpy/numpy / median

Function median

numpy/ma/extras.py:659–735  ·  view source on GitHub ↗

Compute the median along the specified axis. Returns the median of the array elements. Parameters ---------- a : array_like Input array or object that can be converted to an array. axis : int, optional Axis along which the medians are computed. The default

(a, axis=None, out=None, overwrite_input=False, keepdims=False)

Source from the content-addressed store, hash-verified

657
658
659def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
660 """
661 Compute the median along the specified axis.
662
663 Returns the median of the array elements.
664
665 Parameters
666 ----------
667 a : array_like
668 Input array or object that can be converted to an array.
669 axis : int, optional
670 Axis along which the medians are computed. The default (None) is
671 to compute the median along a flattened version of the array.
672 out : ndarray, optional
673 Alternative output array in which to place the result. It must
674 have the same shape and buffer length as the expected output
675 but the type will be cast if necessary.
676 overwrite_input : bool, optional
677 If True, then allow use of memory of input array (a) for
678 calculations. The input array will be modified by the call to
679 median. This will save memory when you do not need to preserve
680 the contents of the input array. Treat the input as undefined,
681 but it will probably be fully or partially sorted. Default is
682 False. Note that, if `overwrite_input` is True, and the input
683 is not already an `ndarray`, an error will be raised.
684 keepdims : bool, optional
685 If this is set to True, the axes which are reduced are left
686 in the result as dimensions with size one. With this option,
687 the result will broadcast correctly against the input array.
688
689 .. versionadded:: 1.10.0
690
691 Returns
692 -------
693 median : ndarray
694 A new array holding the result is returned unless out is
695 specified, in which case a reference to out is returned.
696 Return data-type is `float64` for integers and floats smaller than
697 `float64`, or the input data-type, otherwise.
698
699 See Also
700 --------
701 mean
702
703 Notes
704 -----
705 Given a vector ``V`` with ``N`` non masked values, the median of ``V``
706 is the middle value of a sorted copy of ``V`` (``Vs``) - i.e.
707 ``Vs[(N-1)/2]``, when ``N`` is odd, or ``{Vs[N/2 - 1] + Vs[N/2]}/2``
708 when ``N`` is even.
709
710 Examples
711 --------
712 >>> x = np.ma.array(np.arange(8), mask=[0]*4 + [1]*4)
713 >>> np.ma.median(x)
714 1.5
715
716 >>> x = np.ma.array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)

Callers 7

test_2dMethod · 0.90
test_2d_waxisMethod · 0.90
test_3dMethod · 0.90
test_neg_axisMethod · 0.90
test_out_1dMethod · 0.90
test_outMethod · 0.90
test_keepdims_outMethod · 0.90

Calls 2

_ureduceFunction · 0.90
getdataFunction · 0.85

Tested by 7

test_2dMethod · 0.72
test_2d_waxisMethod · 0.72
test_3dMethod · 0.72
test_neg_axisMethod · 0.72
test_out_1dMethod · 0.72
test_outMethod · 0.72
test_keepdims_outMethod · 0.72