Use scipy.stats.percentileofscore:
# libs required
from scipy import stats
import pandas as pd
import numpy as np
# generate ramdom data with same seed (to be reproducible)
np.random.seed(seed=1)
df = pd.DataFrame(np.random.uniform(0, 1, (10)), columns=['a'])
# quantile function
x = df.quantile(0.5)[0]
# inverse of quantile
stats.percentileofscore(df['a'], x)
Answer from fernandosjp on Stack OverflowUse scipy.stats.percentileofscore:
# libs required
from scipy import stats
import pandas as pd
import numpy as np
# generate ramdom data with same seed (to be reproducible)
np.random.seed(seed=1)
df = pd.DataFrame(np.random.uniform(0, 1, (10)), columns=['a'])
# quantile function
x = df.quantile(0.5)[0]
# inverse of quantile
stats.percentileofscore(df['a'], x)
Sorting can be expensive, if you look for a single value I'd guess you'd be better of computing it with:
s = pd.Series(np.random.uniform(size=1000))
( s < 0.7 ).astype(int).mean() # =0.7ish
There's probably a way to avoid the int(bool) shenanigan.
Inverse function of `quantile()`
python - Numpy implementation of the Quantile function - Cross Validated
python - Numpy function to get the quantile that corresponds to a given value - Stack Overflow
BUG: Quantile function on complex numbers doesn't error
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