Python:NumPy .quantile()
Anonymous contributor
Published Apr 19, 2025
Contribute to Docs
The .quantile() function in NumPy returns the qth quantile of an array along a specified axis. Quantiles are the division points that separate a data set into equal probabilities. For example, the 25th quantile is the point which 25% of the data set falls below.
Syntax
numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, weights=None)
Parameters:
a: The input array containing the data to compute the quantiles from.q: The quantile(s) to compute. This can be a float or array-like of floats between0and1, where0.5represents the median.axis(Optional): The axis or axes on which to calculate the quantile.axis=0computes along columns, andaxis=1computes along rows. If set toNone(default), the input is flattened before computation.out(Optional): Specifies a different array in which to place the result. It must have the same shape as the expected result.overwrite_input(Optional): IfTrue, the input arrayamay be modified to save memory. Default isFalse.method(Optional): The method used to calculate the quantile. The default is'linear'. Valid options include:'inverted_cdf','averaged_inverted_cdf','closest_observation','interpolated_inverted_cdf','hazen','weibull','median_unbiased', and'normal_unbiased'.keepdims(Optional): IfTrue, the reduced axes are retained with size one, maintaining the number of dimensions in the output.weights(Optional): An array of weights corresponding to values ina, used to influence the quantile calculation. This parameter is only supported by the'inverted_cdf'method. The shape ofweightsmust either matcha, or be 1-dimensional with a length equal toawhen flattened.
Return value:
The .quantile() function returns the qth quantile(s) of an array as a NumPy array (ndarray) or a scalar (float64) if the result is a single value.
Example: Computing multiple quantiles from data
The following example creates an array and then uses .quantile() to calculate various quantiles from the data:
import numpy as npa = np.array([[0,1,2],[3,4,5]])print(np.quantile(a, .25))# Computes the 25th quantile along a flattened axisprint(np.quantile(a, .5, axis=0))# Computes the 50th quantile along the vertical axisprint(np.quantile(a, .5, axis=1))# Computes the 50th quantile along the horizontal axisprint(np.quantile(a, .75, axis=1, keepdims=True))# Computes the 75th quantile along the horizontal axis, while retaining the original dimensions of the input array
This code produces the following output:
1.25[1.5 2.5 3.5][1. 4.][[1.5][4.5]]
Codebyte Example
The following codebyte example computes various quantiles for an input array, a:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours