Reference · Statistics

stats.quantiles

signature
stats.quantiles(list, int=, str=) -> list

What it does

The cut points that divide a list of numbers into n parts of equal probability, as Python's statistics.quantiles: n - 1 floats in rising order. n is 4 by default (quartiles); 10 gives deciles, 100 percentiles. The method is "exclusive" by default, which treats the data as a sample and places the cut points as if the smallest and largest values were not quite the extremes; "inclusive" treats the data as the whole population, so the first and last cut points cannot go past the data. The list need not be sorted.

Examples

print(stats.quantiles([1, 2, 3, 4, 5]))print(stats.quantiles([1, 2, 3, 4, 5], 4, "inclusive"))print(stats.quantiles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10))
output
[1.5, 3.0, 4.5][2.0, 3.0, 4.0][1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]

Edge cases

print(stats.quantiles([1, 2, 3], 1), stats.quantiles([5]))print(stats.quantiles([1, 2], 4), stats.quantiles([1, 2], 4, "inclusive"))print(stats.quantiles([3, 1, 2], 2, null), stats.quantiles([3, 1, 2], 2.0))
output
[] [5.0, 5.0, 5.0][0.75, 1.5, 2.25] [1.25, 1.5, 1.75][2.0] [2.0]

Errors

No data, an n outside 1 to 10,000, a method other than "exclusive" or "inclusive", or an item that is not a number are each a runtime error, not an error value.

this one fails
print(stats.quantiles([1, 2, 3], 0))
output
line 1: `stats.quantiles`: n must be from 1 to 10000, got 0
this one fails
print(stats.quantiles([1, 2, 3], 4, "nearest"))
output
line 1: `stats.quantiles`: unknown method "nearest" (exclusive or inclusive)
this one fails
print(stats.quantiles([]))
output
line 1: `stats.quantiles` requires at least one data point

Chaining

let data = [105, 129, 87, 86, 111, 111, 89, 81, 108, 92, 110, 100, 75, 105, 103, 109, 76, 119, 99, 91]print(data | stats.quantiles)print(data | stats.quantiles(10) | map((q) => round(q, 1)))let [q1, q2, q3] = data | stats.quantiles(4, "inclusive")print(q3 - q1)
output
[87.5, 101.5, 109.75][76.5, 86.2, 89.6, 94.8, 101.5, 105.0, 108.7, 110.8, 118.2]20.75

See also

stats.percentile, stats.median, stats.describe, sort

Thread
9Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.quantiles(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo