Python statistics.quantiles() Method

Python statistics 模块Python statistics Module

Python statistics.quantiles()Used to calculate the quantiles of a given dataset.

Syntax

The syntax for 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-- Optional parameter, indicating the number of quantiles to calculate, default is 4.
  • method-- Optional parameter, indicating the method for calculating quantiles, default is "exclusive".

Return Value

The function calculates and returns a list of quantiles as the result based on the given dataset.

Example

The following example demonstrates the usage of 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')
print(quantiles)

Output result:

[3.25, 5.5, 7.75]

In the above example, the given dataset is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], and the quantiles() method is used to calculate the list of quartiles [3.25, 5.5, 7.75].

Quartiles are statistics that describe the distribution of a dataset, 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 of the data, which is the median, and the third quartile (Q3) is the 75% quantile of the data.

The quantiles() method also provides the ability to calculate other quantiles, such as tertiles, quintiles, etc. Different numbers of quantiles can be obtained by adjusting the value of the parameter n. The method for calculating quantiles can be chosen as "exclusive" or "inclusive", controlling the calculation approach. By default, the "exclusive" method is used to calculate quantiles.

Please note that the quantiles() method requires the dataset to be ordered, so the data needs to be sorted before use.

Python statistics 模块Python statistics Module

Other Extensions