9951 explained code solutions for 126 technologies


python-scipyHow do I calculate the quantile of a numpy array using Python?


To calculate the quantile of a numpy array using Python, you can use the numpy.quantile() function. This function takes three arguments: the array, the quantile value, and an optional interpolation method. The quantile value is a number between 0 and 1, with 0.5 representing the median value. The interpolation method can be either 'linear' or 'lower' (default).

Here is an example of how to use the numpy.quantile() function:

import numpy as np

# Create an array
arr = np.array([1, 2, 3, 4, 5])

# Calculate the 0.75 quantile
quantile_75 = np.quantile(arr, 0.75)

print(quantile_75)

Output example

4.25

The code above has the following parts:

  1. import numpy as np: Imports the numpy library and assigns it the alias np.
  2. arr = np.array([1, 2, 3, 4, 5]): Creates an array with the given values.
  3. quantile_75 = np.quantile(arr, 0.75): Calculates the 0.75 quantile of the array.
  4. print(quantile_75): Prints the calculated quantile.

For more information, see the numpy.quantile() documentation.

Edit this code on GitHub