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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
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.
- Quantile in Python
- Quantile by Group in Python
- Percentile & Decile in Python
- Convert pandas DataFrame Index to List & NumPy Array in Python
- Get Median of Array with np.median Function of NumPy Library in Python
- Convert pandas DataFrame to NumPy Array in Python
- Introduction to Python
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.