Quantile of NumPy Array in Python (Example) | Get Quartile with np.quantile Function


In this tutorial, I’ll explain how to calculate the quantiles of a NumPy array in the Python programming language.

The tutorial looks as follows:

Let’s get started.


Example Data & Libraries

First, we have to import the NumPy library to Python:

import numpy as np                                      # Import NumPy library to Python

In addition, have a look at the following 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]]

The previous output of the Python console shows the structure of our example data – It’s a NumPy array with 15 values in three rows and five columns.


Example: Quartiles of Values in NumPy Array

This example demonstrates how to calculate the quartiles of a NumPy array.

For this task, we can apply the quantile function in combination with the arange function. Within the arange function, we have to specify the intervals of our quantiles (i.e. 0.25 to return the quartiles):

print(np.quantile(my_array, np.arange(0.25, 1, 0.25)))  # Get quartiles of all array values
# [2.  5.  5.5]


Video & Further Resources

I have recently published a video on my YouTube channel, which demonstrates the contents of this tutorial. You can find the video below.



Additionally, you might want to have a look at the related tutorials on https://www.statisticsglobe.com/. I have published several tutorials about similar topics such as data conversion, descriptive statistics, indices, and lists.


To summarize: At this point you should have learned how to find the quantiles of a NumPy array in the Python programming language. Let me know in the comments section, in case you have additional comments or 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.