Get Deciles & Percentiles of NumPy Array in Python (Example Code)

This tutorial demonstrates how to calculate the percentiles and deciles of a NumPy array in the Python programming language.

Preparing the Example

import numpy                                       # Load numpy
x = np.array([[8, 2, 1, 7, 7, 5],                  # Constructing a NumPy array in Python
              [4, 10, 5, 9, 1, 4]])
print(x)
# [[ 8  2  1  7  7  5]
#  [ 4 10  5  9  1  4]]

Example: Calculate Deciles & Percentiles of All Array Values in Python

print(np.quantile(x, np.arange(0.1, 1, 0.1)))      # Computing the deciles of all values
# [1.1 2.4 4.  4.4 5.  6.2 7.  7.8 8.9]
print(np.quantile(x, np.arange(0.01, 1, 0.01)))    # Computing the percentiles of all values
# [1.   1.   1.   1.   1.   1.   1.   1.   1.   1.1  1.21 1.32 1.43 1.54
#  1.65 1.76 1.87 1.98 2.18 2.4  2.62 2.84 3.06 3.28 3.5  3.72 3.94 4.
#  4.   4.   4.   4.   4.   4.   4.   4.   4.07 4.18 4.29 4.4  4.51 4.62
#  4.73 4.84 4.95 5.   5.   5.   5.   5.   5.   5.   5.   5.   5.1  5.32
#  5.54 5.76 5.98 6.2  6.42 6.64 6.86 7.   7.   7.   7.   7.   7.   7.
#  7.   7.   7.03 7.14 7.25 7.36 7.47 7.58 7.69 7.8  7.91 8.02 8.13 8.24
#  8.35 8.46 8.57 8.68 8.79 8.9  9.01 9.12 9.23 9.34 9.45 9.56 9.67 9.78
#  9.89]

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top