statistics quantiles Describe Samples

Summarize samples with statistics: quantiles for quartiles and p90, fmean for a one-pass average, stdev for sample spread, all stdlib with no dependencies.

PythonData Integrity

Code

pythonsnippet
18 lines
from statistics import fmean, quantiles, stdev

latencies = [42, 18, 133, 7, 95, 61, 18, 210, 33, 76]

# quantiles 默认 n=4(四分位),method 控制插值约定
q = quantiles(latencies, n=4)
print("q1/med/q3:", q)

# 十分位最后一项即 p90:报告里最常引用的分位数
p90 = quantiles(latencies, n=10)[-1]
print("p90      :", p90)

# fmean 一次遍历取均值;stdev 是样本标准差(n-1 分母)
print("mean/sd  :", round(fmean(latencies), 1), round(stdev(latencies), 1))

# 超过 p90 的样本:容量告警的候选名单
count = sum(1 for x in latencies if x > p90)
print("above p90:", count, "of", len(latencies))

Output

Captured from a sandboxed run of this snippet in the build pipeline.

q1/med/q3: [18.0, 51.5, 104.5]
p90      : 202.3
mean/sd  : 69.3 63.1
above p90: 1 of 10

Inside the snippet

The snippet’s external surface is fmean, quantiles, stdev, all of it via statistics.

The file’s state is a handful of top-level bindings: latencies, p90, count.

What to watch for

  • Dependency check: statistics — not built-in, must be present in the project.

Using it

Running the file prints labeled results such as q1/med/q3:, p90 :, mean/sd : — each operation’s effect is visible in the output.

Interactive Tools for This Topic

FAQ

Do I need to install anything before running this?
No — every API used here ships with the language runtime, so there is nothing to install. Every import resolves inside the standard library — `statistics`. Its state lives in module-level bindings: `latencies`, `p90`, `count`.