NumPy quantile() Function



The NumPy quantile() function computes the q-th quantile (or percentile) of the data along a specified axis. A quantile is a value below which a given percentage of observations fall. For example, the 0.5 quantile (50th percentile) is the median of the data, while the 0.25 quantile (25th percentile) is the first quartile, and the 0.75 quantile (75th percentile) is the third quartile.

Syntax

Following is the syntax of the NumPy quantile() function −

numpy.quantile(a, q, axis=None, out=None, overwrite_input=False, method='linear', keepdims=False, weights=None, interpolation=None)

Parameters

Following are the parameters of the NumPy quantile() function −

  • a: Input array, it can be a NumPy array, list, or a scalar value.
  • q: The quantile(s) to compute, which can be a single number or a list/array of numbers. It should be in the range [0, 1] (for example, 0.25 for the 25th percentile, 0.5 for the median).
  • axis (optional): The axis along which to compute the quantile. Default is None, which means that the quantile is computed over the entire array.
  • out (optional): A location into which the result is stored. If provided, it must have the same shape as the expected output.
  • overwrite_input (optional): If True, the input array is modified in place. Default is False.
  • weights: If weights=None, then all data in a are assumed to have a weight equal to one. Only method=inverted_cdf supports weights.
  • keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
  • interpolation(optional): Deprecated name for the method keyword argument.
  • method (optional): The interpolation method to use when the desired quantile lies between two data points. It can be one of the following:
  • linear(default): Performs linear interpolation between the two closest data points.
  • lower: Returns the lower of the two data points.
  • higher: Returns the higher of the two data points.
  • midpoint: Returns the midpoint of the two data points.
  • nearest: Returns the nearest data point.

Return Values

This function returns the q-th quantile(s) of the input array along the specified axis. The result is a scalar or array depending on the input and the value of the q parameter.

Example

Following is a basic example to compute the 50th percentile (median) of an array using the NumPy quantile() function −

import numpy as np
# input array
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# applying quantile
result = np.quantile(x, 0.5)  # 50th percentile (median)
print("Quantile Result (50th percentile):", result)

Output

Following is the output of the above code −

Quantile Result (50th percentile): 5.0

Example: Specifying a Different Quantile

The quantile() function can also compute other quantiles, such as the 25th or 75th percentile. In the following example, we have computed the 25th and 75th percentiles −

import numpy as np
# input array
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# applying quantile for 25th and 75th percentiles
result = np.quantile(x, [0.25, 0.75])
print("Quantile Result (25th and 75th percentiles):", result)

Output

Following is the output of the above code −

Quantile Result (25th and 75th percentiles): [3. 7.]

Example: Multi-dimensional Array

The quantile() function operates on multi-dimensional arrays. In the following example, we have created a 2D NumPy array, and computed the 50th percentile (median) along a specific axis −

import numpy as np
# 2D array
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# applying quantile along axis 0 (columns)
result = np.quantile(x, 0.5, axis=0)
print("Quantile Result along axis 0 (median):", result)

Output

Following is the output of the above code −

Quantile Result along axis 0 (median): [4. 5. 6.]

Example: Using Different Methods for Interpolation

In the following example, we have used different interpolation methods to compute the 50th percentile of an array. We have demonstrated the linear, lower, higher, and midpoint methods −

import numpy as np
# input array
x = np.array([1, 3, 5, 7, 9])
# applying quantile with different methods
result_linear = np.quantile(x, 0.5, method='linear')
result_lower = np.quantile(x, 0.5, method='lower')
result_higher = np.quantile(x, 0.5, method='higher')
result_midpoint = np.quantile(x, 0.5, method='midpoint')
print("Linear Method:", result_linear)
print("Lower Method:", result_lower)
print("Higher Method:", result_higher)
print("Midpoint Method:", result_midpoint)

Output

Following is the output of the above code −

Linear Method: 5.0
Lower Method: 3.0
Higher Method: 7.0
Midpoint Method: 5.0

Example: Plotting 'quantile()' Function

In the following example, we have plotted the behavior of the quantile function for a range of input values. We have calculated and plotted the 25th, 50th, and 75th percentiles −

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 1, 100)  # input range
y_25 = np.quantile(x, 0.25)
y_50 = np.quantile(x, 0.5)
y_75 = np.quantile(x, 0.75)
plt.plot(x, np.full_like(x, y_25), label="25th percentile")
plt.plot(x, np.full_like(x, y_50), label="50th percentile")
plt.plot(x, np.full_like(x, y_75), label="75th percentile")
plt.title("Quantile Function")
plt.xlabel("Input")
plt.ylabel("Quantile Value")
plt.legend()
plt.grid()
plt.show()

Output

The plot demonstrates the constant nature of each quantile (25th, 50th, 75th) as input values remain unchanged for each quantile −

Arctanh
numpy_statistical_functions.htm
Advertisements