(a, axis=None, out=None, overwrite_input=False)
| 754 | |
| 755 | |
| 756 | def _median(a, axis=None, out=None, overwrite_input=False): |
| 757 | # when an unmasked NaN is present return it, so we need to sort the NaN |
| 758 | # values behind the mask |
| 759 | if np.issubdtype(a.dtype, np.inexact): |
| 760 | fill_value = np.inf |
| 761 | else: |
| 762 | fill_value = None |
| 763 | if overwrite_input: |
| 764 | if axis is None: |
| 765 | asorted = a.ravel() |
| 766 | asorted.sort(fill_value=fill_value) |
| 767 | else: |
| 768 | a.sort(axis=axis, fill_value=fill_value) |
| 769 | asorted = a |
| 770 | else: |
| 771 | asorted = sort(a, axis=axis, fill_value=fill_value) |
| 772 | |
| 773 | if axis is None: |
| 774 | axis = 0 |
| 775 | else: |
| 776 | axis = normalize_axis_index(axis, asorted.ndim) |
| 777 | |
| 778 | if asorted.shape[axis] == 0: |
| 779 | # for empty axis integer indices fail so use slicing to get same result |
| 780 | # as median (which is mean of empty slice = nan) |
| 781 | indexer = [slice(None)] * asorted.ndim |
| 782 | indexer[axis] = slice(0, 0) |
| 783 | indexer = tuple(indexer) |
| 784 | return np.ma.mean(asorted[indexer], axis=axis, out=out) |
| 785 | |
| 786 | if asorted.ndim == 1: |
| 787 | idx, odd = divmod(count(asorted), 2) |
| 788 | mid = asorted[idx + odd - 1:idx + 1] |
| 789 | if np.issubdtype(asorted.dtype, np.inexact) and asorted.size > 0: |
| 790 | # avoid inf / x = masked |
| 791 | s = mid.sum(out=out) |
| 792 | if not odd: |
| 793 | s = np.true_divide(s, 2., casting='safe', out=out) |
| 794 | s = np.lib._utils_impl._median_nancheck(asorted, s, axis) |
| 795 | else: |
| 796 | s = mid.mean(out=out) |
| 797 | |
| 798 | # if result is masked either the input contained enough |
| 799 | # minimum_fill_value so that it would be the median or all values |
| 800 | # masked |
| 801 | if np.ma.is_masked(s) and not np.all(asorted.mask): |
| 802 | return np.ma.minimum_fill_value(asorted) |
| 803 | return s |
| 804 | |
| 805 | counts = count(asorted, axis=axis, keepdims=True) |
| 806 | h = counts // 2 |
| 807 | |
| 808 | # duplicate high if odd number of elements so mean does nothing |
| 809 | odd = counts % 2 == 1 |
| 810 | l = np.where(odd, h, h - 1) |
| 811 | |
| 812 | lh = np.concatenate([l, h], axis=axis) |
| 813 |
nothing calls this directly
no test coverage detected
searching dependent graphs…