What is the statistics quantiles() method in Python?

Overview

The statistics.quantiles() method in Python is used to return the quantiles that correspond to the numbers n contained in the iterable containing data.

To explain further, when you assign a number to the n parameter, the function returns the corresponding n-1 quartiles. For example, if the n parameter is assigned as 10 for deciles, the statistics.quantiles() method will return 10-1=9 cut points of equal intervals.

A quantile defines a particular data set. In other words, a quantile determines how many values in a distribution are above or below a certain limit. A quantile can be a quartile, percentiles, and quintile.

Syntax

Statistics.quantiles(data, *, n=4, method = ‘exclusive’)

Parameter values

Parameter

Description


data

An iterable that contains the data from which you want to obtain the quantiles. This is required.


n

The number of quantiles you want. This parameter takes an integer value, which is 4 by default. This is optional.


method

The method by which the quantiles are to be calculated; exclusive or inclusive. It is exclusive by default.

Return value

The statistics.quantiles() method returns a list that contains the numeric values of the upper n-1 quantiles.

Example

Now, let’s use the statistics.quantiles() method to return the upper quartile values of a data set.

Code

import statistics
data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
# to return the upper three quartiles
third_quartiles = statistics.quantiles(data1, n=4)
print('The third quartile of the given data set is', third_quartiles)

Explanation

  • Line 1: We import the statistics module.
  • Line 3: We create our data set named data1.
  • Line 5: We use the statistics.quantiles() method to return the three upper quartiles of the data set and assign it to a variable called third_quartiles.
  • Line 6: We print the output of our variable third_quartile.