Numpy function to get the quantile that corresponds to a given value

Viewed 3230

I see a lot of questions like this one for R, but I couldn't find one specifically for Python, preferably using numpy.

Let's say I have an array of observations stored in x. I can get the value that accumulates q * 100 per cent of the population.

# Import numpy
import numpy as np

# Get 75th percentile
np.quantile(a=x, q=0.75)

However, I was wondering if there's a function that does the inverse. That is, a numpy function that takes a value as an input and returns q.

To further expand on this, scipy distribution objects have a ppf method that allows me to do this. I'm looking for something similar in numpy. Does it exist?

3 Answers

Not a ready-made function but a compact and reasonably fast snippet:

(a<value).mean()

You can (at least on my machine) squeeze out a few percent better performance by using np.count_nonzero

np.count_nonzero(a<value) / a.size

but tbh I wouldn't even bother.

There's a convenience function that does this. Note that it's not an exact inverse because the quantile/percentile functions are not exact. Given a finite array of observations, the percentiles will have discrete values; in other words, you may be specifying a q that falls between those values and the functions find the closest one.

from scipy import stats
import numpy as np

stats.percentileofscore(np.arange(0,1,0.12), .65, 'weak') / 100

If x is sorted, the value at index i is the i / len(x) percentile (or so, depending on how you want to treat boundary conditions). If x is not sorted, you can obtain the same value by substituting x.argsort().argsort()[i] for i (or just sorting x first). Since argsort is its own inverse, the double argsort tells you where each element of the original would fall in the sorted array.

If you want to find the result for arbitrary values not necessarily in x, you can apply np.searchsorted to a sorted version of x and interpolating on the result. You can use a more complicated method, like fitting a spline to the sorted data or something similar.

Related