NumPy quantile()

The numpy.quantile() method computes the q-th quantile of the data along the specified axis.

Example

import numpy as np

# create an array
array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7])

# calculate the 0.25th, 0.50th and 0.75th quantile of the array q25 = np.quantile(array1, 0.25) q50 = np.quantile(array1, 0.50) q75 = np.quantile(array1, 0.75)
print(q25, q50, q75) # Output: 1.75 3.5 5.25

quantile() Syntax

The syntax of the numpy.quantile() method is:

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

quantile() Arguments

The numpy.quantile() method takes the following arguments:

  • array - input array (can be array_like)
  • q - qth quantile to find (can be array_like of float)
  • axis (optional) - axis or axes along which the quantiles are computed (int or tuple of int)
  • out (optional) - output array in which to place the result (ndarray)
  • keepdims (optional) - specifies whether to preserve the shape of the original array (bool)
  • override_input (optional) - bool value that determines if intermediate calculations can modify an array
  • method (optional) - the interpolation method to use
  • interpolation (optional) - the deprecated name for the method keyword argument

Notes: The default values of numpy.quantile() have the following implications:

  • axis = None - the quantile of the entire array is taken.
    • By default, keepdims and override_input will be False.
    • The interpolation method is 'linear'.
    • If the input contains integers or floats smaller than float64, the output data type is float64. Otherwise, the output data type is the same as that of the input.

quantile() Return Value

The numpy.quantile() method returns the q-th quantile(s) of the input array along the specified axis.


Quantile

The quantile is a statistical measure that represents the value below which a specific percentage of data falls. It helps analyze the distribution of a dataset.

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.

Note: numpy.quantile() and numpy.percentile() do the same thing. If you want to specify q from 0 to 100, use percentile() and if you want to specify q from 0.0 to 1.0, use quantile().


Example 1: Find the Quantile of an ndArray

import numpy as np

# create an array
array1 = np.array([[[0, 1],
                    [2, 3]],

                   [[4, 5],
                    [6, 7]]])

# find the 50th quantile of entire array quantile1 = np.quantile(array1, q = 0.50) # find the 50th quantile across axis 0 quantile2 = np.quantile(array1, q = 0.50, axis = 0) # find the 50th quantile across axis 0 and 1 quantile3 = np.quantile(array1, q = 0.50, axis = (0, 1))
print('\n50th quantile of the entire array:', quantile1) print('\n50th quantile across axis 0:\n', quantile2) print('\n50th quantile across axis 0 and 1:', quantile3)

Output

50th quantile of the entire array: 3.5

50th quantile across axis 0:
[[2. 3.]
 [4. 5.]]

50th quantile across axis 0 and 1: [3. 4.]

Example 2: Using Optional out Argument

The out parameter allows us to specify an output array where the result will be stored.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# create an output array
output = np.zeros(3)

# compute 25th quantile and store the result in the output array np.quantile(arr, 0.25, out = output, axis = 0)
print('25th quantile:', output)

Output

25th quantile: [1.75 2.75 3.75]

Example 3: Using Optional keepdims Argument

If keepdims is set to True, the resultant array's dimensions are the same as the original array.

import numpy as np

arr = np.array([[1, 2, 3],
                [4, 5, 6]])

# keepdims defaults to False result1 = np.quantile(arr, 0.50 , axis = 0) # pass keepdims as True result2 = np.quantile(arr, 0.50, axis = 0, keepdims = True)
print('Dimensions in original array:', arr.ndim) print('Without keepdims:', result1, 'with dimensions', result1.ndim) print('With keepdims:', result2, 'with dimensions', result2.ndim)

Output

Dimensions in original array: 2
Without keepdims: [2.5 3.5 4.5] with dimensions 1
With keepdims: [[2.5 3.5 4.5]] with dimensions 2