@@ -2883,6 +2883,31 @@ def percentile(input, q, axis=None):
2883
2883
axis: None or int or list of int, optional
2884
2884
Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened version of the array.
2885
2885
"""
2886
+ if isinstance (q , (int | float )):
2887
+ q = [q ]
2888
+
2889
+ for percentile in q :
2890
+ if percentile < 0 or percentile > 100 :
2891
+ raise ValueError ("Percentiles must be in the range [0, 100]" )
2892
+
2893
+ quantiles = [x / 100 for x in q ]
2894
+
2895
+ return quantile (input , quantiles , axis )
2896
+
2897
+
2898
+ def quantile (input , q , axis = None ):
2899
+ """
2900
+ Computes the quantile along the given axis(es) of a tensor `input` using linear interpolation.
2901
+
2902
+ Parameters
2903
+ ----------
2904
+ input: TensorVariable
2905
+ The input tensor.
2906
+ q: float or list of floats
2907
+ Quantile or sequence of quantiles to compute, which must be between 0 and 1 inclusive.
2908
+ axis: None or int or list of int, optional
2909
+ Axis or axes along which the quantiles are computed. The default is to compute the quantile(s) along a flattened version of the array.
2910
+ """
2886
2911
x = as_tensor_variable (input )
2887
2912
x_ndim = x .type .ndim
2888
2913
@@ -2911,13 +2936,13 @@ def percentile(input, q, axis=None):
2911
2936
if isinstance (q , (int | float )):
2912
2937
q = [q ]
2913
2938
2914
- for percentile in q :
2915
- if percentile < 0 or percentile > 100 :
2916
- raise ValueError ("Percentiles must be in the range [0, 100 ]" )
2939
+ for quantile in q :
2940
+ if quantile < 0 or quantile > 1 :
2941
+ raise ValueError ("Quantiles must be in the range [0, 1 ]" )
2917
2942
2918
2943
result = []
2919
- for percentile in q :
2920
- k = (percentile / 100.0 ) * (input_shape - 1 )
2944
+ for quantile in q :
2945
+ k = (quantile ) * (input_shape - 1 )
2921
2946
k_floor = floor (k ).astype ("int64" )
2922
2947
k_ceil = ceil (k ).astype ("int64" )
2923
2948
@@ -2927,44 +2952,19 @@ def percentile(input, q, axis=None):
2927
2952
val2 = sorted_input [tuple (slices2 )]
2928
2953
2929
2954
d = k - k_floor
2930
- percentile_val = val1 + d * (val2 - val1 )
2955
+ quantile_val = val1 + d * (val2 - val1 )
2931
2956
2932
- result .append (percentile_val .squeeze (axis = - 1 ))
2957
+ result .append (quantile_val .squeeze (axis = - 1 ))
2933
2958
2934
2959
if len (result ) == 1 :
2935
2960
result = result [0 ]
2936
2961
else :
2937
2962
result = stack (result )
2938
2963
2939
- result .name = "percentile "
2964
+ result .name = "quantile "
2940
2965
return result
2941
2966
2942
2967
2943
- def quantile (input , q , axis = None ):
2944
- """
2945
- Computes the quantile along the given axis(es) of a tensor `input` using linear interpolation.
2946
-
2947
- Parameters
2948
- ----------
2949
- input: TensorVariable
2950
- The input tensor.
2951
- q: float or list of floats
2952
- Quantile or sequence of quantiles to compute, which must be between 0 and 1 inclusive.
2953
- axis: None or int or list of int, optional
2954
- Axis or axes along which the quantiles are computed. The default is to compute the quantile(s) along a flattened version of the array.
2955
- """
2956
- if isinstance (q , (int | float )):
2957
- q = [q ]
2958
-
2959
- for quantile in q :
2960
- if quantile < 0 or quantile > 1 :
2961
- raise ValueError ("Quantiles must be in the range [0, 1]" )
2962
-
2963
- percentiles = [100.0 * x for x in q ]
2964
-
2965
- return percentile (input , percentiles , axis )
2966
-
2967
-
2968
2968
# NumPy logical aliases
2969
2969
square = sqr
2970
2970
0 commit comments