Python statistics.quantiles() Method
Python statistics.quantiles() Used to compute the quantiles of a given data set.
Syntax
The syntax of the statistics.quantiles() method is as follows:
statistics.quantile(data, *, n=4, method='exclusive')
Parameter Description:
- data -- an iterable containing numeric values, such as a list or tuple.
- n -- an optional parameter indicating the number of quantiles to compute, defaults to 4.
- method -- an optional parameter indicating the method for computing quantiles, defaults to "exclusive".
Return Value
The function computes and returns a list of quantiles from the given data set as the result.
Example
The following example demonstrates the quantiles() function:
Example
import
statistics
data
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
]
quantiles
=
statistics.
quantiles
(
data
,
n
=
4
,
method
=
'inclusive'
)
(
quantiles
)
Output result:
[3.25, 5.5, 7.75]
In the above example, the given data set is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], and the quantiles() method computes the list of quartiles [3.25, 5.5, 7.75].
Quartiles are statistics describing the distribution of a data set, dividing it into four equal parts. The first quartile (Q1) is the 25% quantile of the data, the second quartile (Q2) is the 50% quantile, that is, the median, and the third quartile (Q3) is the 75% quantile.
The quantiles() method can also compute other quantiles, such as terciles and quintiles; by adjusting the parameter n you can get different numbers of quantiles. The method for computing quantiles can be "exclusive" or "inclusive", which controls how quantiles are calculated. By default, the "exclusive" method is used.
Note that the quantiles() method requires the data set to be ordered, so you need to sort the data before using it.
Python statistics Module