On this page
statistics
Prev: stat | Up: stdlib_module | Next: string
Rel: numbers, math, cmath, decimal, fractions
Ref: docs.python.org — statistics
Tags: #public #python3 #stdlib
Import
import statistics
Overview
Zero-dependency descriptive statistics (mean, fmean, median, mode, stdev, pstdev, variance, quantiles, correlation, linear_regression, NormalDist) for small-to-medium numeric datasets. Reach for statistics when you want clean summary metrics without pulling in NumPy/SciPy.
Quick Start
import statistics
latencies_ms = [12.4, 15.1, 14.8, 13.0, 42.0]
assert round(statistics.fmean(latencies_ms), 2) == 19.46
assert statistics.median(latencies_ms) == 14.8
assert round(statistics.stdev(latencies_ms), 2) == 12.64
Implementation Walkthrough
1. Percentiles / Quartiles (quantiles) & Linear Regression
import statistics
samples = [10, 12, 14, 15, 18, 22, 25, 30, 45, 90]
q1, med, q3 = statistics.quantiles(samples, n=4)
assert q1 < med < q3
xs = [1.0, 2.0, 3.0, 4.0]
ys = [2.1, 3.9, 6.2, 7.9]
fit = statistics.linear_regression(xs, ys)
assert 1.8 < fit.slope < 2.2
2. Normal Distributions & Z-Scores (NormalDist)
from statistics import NormalDist
dist = NormalDist(mu=100, sigma=15)
assert round(dist.zscore(130), 2) == 2.0
assert 0.97 < dist.cdf(130) < 0.98
Gotchas & Boundaries
stdev()is sample standard deviation (n - 1); usepstdev()for population standard deviation (n).- Empty inputs raise
StatisticsError(andstdev()requires at least 2 points): Guard inputs before calling. - Prefer
fmean()overmean()for floats:fmean()runs faster in floating-point arithmetic;mean()preserves exactDecimalandFractiontypes. - Not meant for multi-million-row arrays: Use NumPy or DuckDB for large numerical analytics.
Common Functions
- statistics.correlation() —
correlation(x, y, /, *, method='linear')— Pearson's correlation coefficient Return the Pearson's correlation coefficient for two inputs. Pearson's correlation coefficient r takes values between -1 and +1. It measures - statistics.covariance() —
covariance(x, y, /)— Covariance Return the sample covariance of two inputs x and y. Covariance is a measure of the joint variability of two inputs. - statistics.fmean() —
fmean(data, weights=None)— Convert data to floats and compute the arithmetic mean. - statistics.geometric_mean() —
geometric_mean(data)— Convert data to floats and compute the geometric mean. - statistics.harmonic_mean() —
harmonic_mean(data, weights=None)— Return the harmonic mean of data. - statistics.kde() —
kde(data, h, kernel='normal', *, cumulative=False)— Kernel Density Estimation: Create a continuous probability density function or cumulative distribution function from discrete samples. - statistics.kde_random() —
kde_random(data, h, kernel='normal', *, seed=None)— Return a function that makes a random selection from the estimated probability density function created by kde(data, h, kernel). - statistics.linear_regression() —
linear_regression(x, y, /, *, proportional=False)— Slope and intercept for simple linear regression. - statistics.mean() —
mean(data)— Return the sample arithmetic mean of data. - statistics.median() —
median(data)— Return the median (middle value) of numeric data. - statistics.median_grouped() —
median_grouped(data, interval=1.0)— Estimates the median for numeric data binned around the midpoints of consecutive, fixed-width intervals. - statistics.median_high() —
median_high(data)— Return the high median of data. - statistics.median_low() —
median_low(data)— Return the low median of numeric data. - statistics.mode() —
mode(data)— Return the most common data point from discrete or nominal data. - statistics.multimode() —
multimode(data)— Return a list of the most frequently occurring values. - statistics.pstdev() —
pstdev(data, mu=None)— Return the square root of the population variance. - statistics.pvariance() —
pvariance(data, mu=None)— Return the population variance ofdata. - statistics.quantiles() —
quantiles(data, *, n=4, method='exclusive')— Divide data into n continuous intervals with equal probability. - statistics.stdev() —
stdev(data, xbar=None)— Return the square root of the sample variance. - statistics.variance() —
variance(data, xbar=None)— Return the sample variance of data.
Classes & Objects
- statistics.NormalDist — Normal distribution of a random variable (Methods:
.cdf(),.from_samples(),.inv_cdf(),.mean(),.median(),.mode()) - statistics.StatisticsError —
statistics.StatisticsErrorclass. (Methods:.add_note(),.with_traceback())
Linked from
- tuts/python3
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/math
- tuts/python3/stdlib_module/stat
- tuts/python3/stdlib_module/statistics/correlation
- tuts/python3/stdlib_module/statistics/covariance
- tuts/python3/stdlib_module/statistics/fmean
- tuts/python3/stdlib_module/statistics/geometric_mean
- tuts/python3/stdlib_module/statistics/harmonic_mean
- tuts/python3/stdlib_module/statistics/kde
- tuts/python3/stdlib_module/statistics/kde_random
- tuts/python3/stdlib_module/statistics/linear_regression
- tuts/python3/stdlib_module/statistics/mean
- tuts/python3/stdlib_module/statistics/median
- tuts/python3/stdlib_module/statistics/median_grouped
- tuts/python3/stdlib_module/statistics/median_high
- tuts/python3/stdlib_module/statistics/median_low
- tuts/python3/stdlib_module/statistics/mode
- tuts/python3/stdlib_module/statistics/multimode
- tuts/python3/stdlib_module/statistics/NormalDist
- tuts/python3/stdlib_module/statistics/pstdev
- tuts/python3/stdlib_module/statistics/pvariance
- tuts/python3/stdlib_module/statistics/quantiles
- tuts/python3/stdlib_module/statistics/StatisticsError
- tuts/python3/stdlib_module/statistics/stdev
- tuts/python3/stdlib_module/statistics/variance
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/string
- tuts/python3/tutorials/popular_application_domains
- tuts/python3/tutorials/popular_application_domains/data_science_and_machine_learning
Related notes
- tuts/python3/stdlib_module/math Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stat Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/correlation Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/covariance Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/fmean Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/geometric_mean Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/harmonic_mean Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/kde Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/kde_random Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/statistics/linear_regression Linked here; links to this page; shared tags: #stdlib