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.
Answer from loopy walt on Stack OverflowNumPy
numpy.org › doc › stable › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.5 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...
How do I ignore NaN values in a quantile?
Use numpy.nanquantile() when NaN values should be excluded from the calculation rather than propagated.
pythonpool.com
pythonpool.com › home › numpy › numpy quantile(): q, axis, method, nan, and examples
Numpy Quantile() Explained With Examples - Python Pool
What is the difference between quantile() and percentile()?
They express the same idea with different scales: quantile uses q from 0 to 1, while percentile uses values from 0 to 100.
pythonpool.com
pythonpool.com › home › numpy › numpy quantile(): q, axis, method, nan, and examples
Numpy Quantile() Explained With Examples - Python Pool
Why does the method argument matter?
Different estimators interpolate or select sample values differently; choose and document the method when reproducibility or statistical interpretation matters.
pythonpool.com
pythonpool.com › home › numpy › numpy quantile(): q, axis, method, nan, and examples
Numpy Quantile() Explained With Examples - Python Pool
ProgramCreek
programcreek.com › python › example › 125382 › numpy.quantile
Python Examples of numpy.quantile
Formula: (25th percentile + 2*50th percentile + 75th percentile)/4 Parameters ---------- data : array-like an iterable, either a list or a numpy array Returns ------- the trimean: float """ q1 = np.quantile(data, 0.25) q3 = np.quantile(data, 0.75) median = np.median(data) return (q1 + 2*median ...
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.1 Manual
An array of weights associated with the values in a. Each value in a contributes to the quantile according to its associated weight. The weights array can either be 1-D (in which case its length must be the size of a along the given axis) or of the same shape as a.
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .quantile()
Python:NumPy | Built-in Functions | .quantile() | Codecademy
April 19, 2025 - The .quantile() function in NumPy ... separate a data set into equal probabilities. For example, the 25th quantile is the point which 25% of the data set falls below....
Top answer 1 of 4
23
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.
2 of 4
11
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
NumPy
numpy.org › devdocs › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.6.dev0 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers, the output data-type is float64.
Skytowner
skytowner.com › explore › numpy_quantile_method
NumPy | quantile method with Examples
Numpy's quantile(~) method returns the interpolated value at the specified quantile. Note that this method is exactly the same as the percentile(~), just that the quantile(~) method takes a value between 0 and 1 - not 0 and 100.
Programiz
programiz.com › python-programming › numpy › methods › quantile
NumPy quantile()
In NumPy, the quantile() function computes the q-th quantile of data along the specified axis. The q-th quantile represents the value below which q percent of the data falls. For example, the 0.50th quantile (also known as the median) divides the data into two equal halves.
Codecademy
codecademy.com › learn › learn-statistics-with-python › modules › quartiles-quantiles-and-interquartile-range › cheatsheet
Learn Statistics with Python: Quartiles, Quantiles, and Interquartile Range Cheatsheet | Codecademy
It returns the value at the qth quantile. For example, numpy.quantile(data, 0.25) returns the value at the first quartile of the dataset data.
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.0 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...
NumPy
numpy.org › doc › 1.22 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v1.22 Manual
Given a vector V of length N, the q-th quantile of V is the value q of the way from the minimum to the maximum in a sorted copy of V. The values and distances of the two nearest neighbors as well as the method parameter will determine the quantile if the normalized ranking does not match the ...
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.2 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...
NumPy
numpy.org › doc › 1.21 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v1.21 Manual
June 22, 2021 - Given a vector V of length N, the q-th quantile of V is the value q of the way from the minimum to the maximum in a sorted copy of V. The values and distances of the two nearest neighbors as well as the interpolation parameter will determine the quantile if the normalized ranking does not match ...
SciPy
docs.scipy.org › doc › numpy-1.17.0 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v1.17 Manual
>>> a = np.array([[10, 7, 4], [3, 2, 1]]) >>> a array([[10, 7, 4], [ 3, 2, 1]]) >>> np.quantile(a, 0.5) 3.5 >>> np.quantile(a, 0.5, axis=0) array([6.5, 4.5, 2.5]) >>> np.quantile(a, 0.5, axis=1) array([7., 2.]) >>> np.quantile(a, 0.5, axis=1, keepdims=True) array([[7.], [2.]]) >>> m = np.quantile(a, 0.5, axis=0) >>> out = np.zeros_like(m) >>> np.quantile(a, 0.5, axis=0, out=out) array([6.5, 4.5, 2.5]) >>> m array([6.5, 4.5, 2.5]) >>> b = a.copy() >>> np.quantile(b, 0.5, axis=1, overwrite_input=True) array([7., 2.]) >>> assert not np.all(a == b) numpy.nanpercentile ·
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.3 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...
NumPy
numpy.org › doc › 1.19 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v1.19 Manual
June 29, 2020 - Given a vector V of length N, the q-th quantile of V is the value q of the way from the minimum to the maximum in a sorted copy of V. The values and distances of the two nearest neighbors as well as the interpolation parameter will determine the quantile if the normalized ranking does not match ...