Percentiles & Deciles of NumPy Array in Python (Example)

 

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

The article will contain these contents:

Let’s dive right into the programming part!

 

Example Data & Software Libraries

We first need to load the NumPy library to Python, in order to use the corresponding functions:

import numpy as np                                      # Import NumPy

We’ll also have to construct some example data:

my_array = np.array([[1, 2, 7, 2, 3],                   # Create example array
                     [7, 1, 1, 5, 6],
                     [5, 2, 5, 5, 8]])
print(my_array)                                         # Print example array
# [[1 2 7 2 3]
#  [7 1 1 5 6]
#  [5 2 5 5 8]]

Have a look at the previous Python console output. It shows that our example data is a NumPy array containing 15 values.

 

Example: Percentiles & Deciles of Values in NumPy Array

This example shows how to get the percentiles and deciles of a NumPy array in Python.

To compute the percentiles, we can apply the quantile and arange functions of the NumPy package as shown below:

print(np.quantile(my_array, np.arange(0.01, 1, 0.01)))  # Get percentiles of array values
# [1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.   1.
#  1.1  1.24 1.38 1.52 1.66 1.8  1.94 2.   2.   2.   2.   2.   2.   2.
#  2.   2.   2.   2.   2.   2.   2.   2.04 2.18 2.32 2.46 2.6  2.74 2.88
#  3.04 3.32 3.6  3.88 4.16 4.44 4.72 5.   5.   5.   5.   5.   5.   5.
#  5.   5.   5.   5.   5.   5.   5.   5.   5.   5.   5.   5.   5.   5.
#  5.   5.08 5.22 5.36 5.5  5.64 5.78 5.92 6.06 6.2  6.34 6.48 6.62 6.76
#  6.9  7.   7.   7.   7.   7.   7.   7.   7.02 7.16 7.3  7.44 7.58 7.72
#  7.86]

The previous output of the Python console shows the percentiles of our example array.

Note that we have specified the sequence of quantiles that we wanted to return using the arange function. We can now modify this sequence to get the deciles of our array:

print(np.quantile(my_array, np.arange(0.1, 1, 0.1)))    # Get deciles of array values
# [1.  1.8 2.  2.6 5.  5.  5.  6.2 7. ]

Looks good!

 

Video, Further Resources & Summary

Do you need more explanations on the contents of the present tutorial? Then I can recommend watching the following video on my YouTube channel. In the video, I show the Python code of this article:

 

The YouTube video will be added soon.

 

In addition, you might have a look at the other articles on my website. A selection of tutorials that are related to the calculation of the percentiles and deciles of a NumPy array is shown below.

 

To summarize: At this point you should have learned how to find the percentiles and deciles of a NumPy array in Python. Don’t hesitate to let me know in the comments section, in case you have any further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Top