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

Function _quantile

numpy/lib/function_base.py:4765–4851  ·  view source on GitHub ↗

Private function that doesn't support extended axis or keepdims. These methods are extended to this function using _ureduce See nanpercentile for parameter usage It computes the quantiles of the array for the given axis. A linear interpolation is performed based on the `interpol

(
        arr: np.array,
        quantiles: np.array,
        axis: int = -1,
        method="linear",
        out=None,
)

Source from the content-addressed store, hash-verified

4763
4764
4765def _quantile(
4766 arr: np.array,
4767 quantiles: np.array,
4768 axis: int = -1,
4769 method="linear",
4770 out=None,
4771):
4772 """
4773 Private function that doesn't support extended axis or keepdims.
4774 These methods are extended to this function using _ureduce
4775 See nanpercentile for parameter usage
4776 It computes the quantiles of the array for the given axis.
4777 A linear interpolation is performed based on the `interpolation`.
4778
4779 By default, the method is "linear" where alpha == beta == 1 which
4780 performs the 7th method of Hyndman&Fan.
4781 With "median_unbiased" we get alpha == beta == 1/3
4782 thus the 8th method of Hyndman&Fan.
4783 """
4784 # --- Setup
4785 arr = np.asanyarray(arr)
4786 values_count = arr.shape[axis]
4787 # The dimensions of `q` are prepended to the output shape, so we need the
4788 # axis being sampled from `arr` to be last.
4789
4790 if axis != 0: # But moveaxis is slow, so only call it if necessary.
4791 arr = np.moveaxis(arr, axis, destination=0)
4792 # --- Computation of indexes
4793 # Index where to find the value in the sorted array.
4794 # Virtual because it is a floating point value, not an valid index.
4795 # The nearest neighbours are used for interpolation
4796 try:
4797 method = _QuantileMethods[method]
4798 except KeyError:
4799 raise ValueError(
4800 f"{method!r} is not a valid method. Use one of: "
4801 f"{_QuantileMethods.keys()}") from None
4802 virtual_indexes = method["get_virtual_index"](values_count, quantiles)
4803 virtual_indexes = np.asanyarray(virtual_indexes)
4804
4805 supports_nans = (
4806 np.issubdtype(arr.dtype, np.inexact) or arr.dtype.kind in 'Mm')
4807
4808 if np.issubdtype(virtual_indexes.dtype, np.integer):
4809 # No interpolation needed, take the points along axis
4810 if supports_nans:
4811 # may contain nan, which would sort to the end
4812 arr.partition(concatenate((virtual_indexes.ravel(), [-1])), axis=0)
4813 slices_having_nans = np.isnan(arr[-1, ...])
4814 else:
4815 # cannot contain nan
4816 arr.partition(virtual_indexes.ravel(), axis=0)
4817 slices_having_nans = np.array(False, dtype=bool)
4818 result = take(arr, virtual_indexes, axis=0, out=out)
4819 else:
4820 previous_indexes, next_indexes = _get_indexes(arr,
4821 virtual_indexes,
4822 values_count)

Callers 1

_quantile_ureduce_funcFunction · 0.85

Calls 10

takeFunction · 0.90
_get_indexesFunction · 0.85
_get_gammaFunction · 0.85
_lerpFunction · 0.85
keysMethod · 0.80
reshapeMethod · 0.80
concatenateFunction · 0.50
partitionMethod · 0.45
ravelMethod · 0.45
anyMethod · 0.45

Tested by

no test coverage detected