SciPy.stats has a function called percentileofscore. To keep my package dependencies down, I want to source the most similar function possible from numpy, instead.
import numpy as np
a = np.array([3, 2, 1])
np.percentile(a, a)
>>>
array([1.06, 1.04, 1.02])
percentileofscore(a,a)
>>>
array([100. , 66.66666667, 33.33333333])
I'm not sure what is is that Numpy is doing... But it's not returning intuitive percentiles to me. How can I achieve the same functionality using built-in numpy methods.
Of note, by default, percentileofscore will average percentiles for ties. I do want to preserve this functionality. Ex [100, 100] should not return [0, 100] but [50, 50] instead.
You can actually take look at the implementation in Scipy, it is rather simple (https://github.com/scipy/scipy/blob/v1.12.0/scipy/stats/_stats_py.py#L2407). Reproducing this in Numpy gives:
Which prints:
I hope this helps!