statistics.quantiles

Full name
statistics.quantiles
Library
statistics
Syntax

statistics.quantiles(data, *, n=4, method='exclusive')

Description

The statistics.quantiles function returns the quantiles corresponding to the numbers contained in the iterable data. If the parameter n takes the value 4, the function returns the numerical values corresponding to the first, second and third quartiles. If n takes the value 100, the function returns the numeric values corresponding to the percentiles ranging from 1 to 100. In general, for a value of the parameter n, the function will return the highest n-1 quantiles.

Parameters
  • data: Iterable from whose numerical values you want to obtain the quantiles.
  • n: Optional argument. Number of quantiles to identify. The function will return the numeric values of the upper n-1 quantiles. By default it takes the value 4.
  • method: Optional argument that can take the values "exclusive" or "inclusive" and that defines the method of calculating the quantiles. By default it takes the value "exclusive".
Result

The statistics.quantiles function returns a list with the n-1 requested quantiles.

Examples

We can generate a list of 20 random numbers between 0 and 100 with the following code:

import random
data = [random.randint(0, 100) for _ in range(20)]
sorted(data)
[9, 12, 12, 18, 32, 39, 40, 42, 45, 55, 60, 68, 71, 77, 78, 79, 81, 87, 90, 93]

Now we can get the upper three quartiles using the statistics.quantiles function:

statistics.quantiles(data, n = 4)
[28.5, 50.0, 64.75]

The quantiles returned by the function do not have to belong to data:

statistics.quantiles([0, 2, 4, 6, 8], n = 4)
[1.0, 4.0, 7.0]

...and they can be both integer and real values:

statistics.quantiles([0, 2, 4, 6], n = 4)
[0.5, 3.0, 5.5]
Submitted by admin on Wed, 04/07/2021 - 10:11