SciPy Descriptive Statistics
Before running any test or fitting any model, you should describe your data: where is its centre, how spread out is it, is it symmetric, are there outliers? NumPy gives you the basics (mean, std, percentile); scipy.stats adds the rest.
A quick summary with describe
stats.describe() returns the size, range, mean, variance, skewness and kurtosis in one call:
import numpy as np
from scipy import stats
# Daily coffee sales of a small café over two weeks
sales = np.array([42, 38, 51, 47, 45, 60, 72, 40, 44, 49, 46, 58, 75, 41])
d = stats.describe(sales)
print("n :", d.nobs)
print("min, max :", [int(v) for v in d.minmax])
print("mean :", round(d.mean, 3))
print("variance :", round(d.variance, 3))
print("skewness :", round(d.skewness, 3))
print("kurtosis :", round(d.kurtosis, 3))n : 14
min, max : [38, 75]
mean : 50.571
variance : 134.264
skewness : 1.046
kurtosis : -0.093
describe uses the sample variance (dividing by n − 1). NumPy's var() divides by n unless you pass ddof=1 — a classic source of small mismatches.
Measures of centre
The mean is pulled by extreme values; the median is not. SciPy adds robust compromises such as the trimmed mean (drop a share of the smallest and largest values first), plus the geometric and harmonic means for growth rates and ratios:
import numpy as np
from scipy import stats
sales = np.array([42, 38, 51, 47, 45, 60, 72, 40, 44, 49, 46, 58, 75, 41])
print("mean :", round(sales.mean(), 2))
print("median :", np.median(sales))
print("trimmed (10%) :", round(stats.trim_mean(sales, 0.1), 2))
print("mode :", stats.mode([1, 2, 2, 3, 3, 3, 4]).mode)
growth = np.array([1.10, 1.25, 0.95, 1.08]) # yearly growth factors
print("geometric mean:", round(stats.gmean(growth), 4))
speeds = np.array([60, 40]) # km/h, same distance each
print("harmonic mean :", round(stats.hmean(speeds), 4))mean : 50.57
median : 46.5
trimmed (10%) : 49.58
mode : 3
geometric mean: 1.0898
harmonic mean : 48.0
The harmonic mean of 60 and 40 km/h is 48 km/h, the true average speed if you drive the same distance at each speed.
Measures of spread
| Measure | Function | Robust to outliers? |
|---|---|---|
| Variance / standard deviation | np.var, np.std (ddof=1) |
no |
| Interquartile range | stats.iqr |
yes |
| Median absolute deviation | stats.median_abs_deviation |
yes |
| Standard error of the mean | stats.sem |
no |
| Coefficient of variation | stats.variation |
no |
import numpy as np
from scipy import stats
sales = np.array([42, 38, 51, 47, 45, 60, 72, 40, 44, 49, 46, 58, 75, 41])
print("std (n-1) :", round(sales.std(ddof=1), 3))
print("IQR :", stats.iqr(sales))
print("MAD :", stats.median_abs_deviation(sales))
print("SEM :", round(stats.sem(sales), 3))
print("CV :", round(stats.variation(sales), 3))std (n-1) : 11.587
IQR : 13.75
MAD : 5.0
SEM : 3.097
CV : 0.221
Standard scores and percentiles
A z-score says how many standard deviations a value lies from the mean. zscore() standardises a whole array; percentileofscore() tells you what share of the data falls below a given value:
import numpy as np
from scipy import stats
sales = np.array([42, 38, 51, 47, 45, 60, 72, 40, 44, 49, 46, 58, 75, 41])
z = stats.zscore(sales, ddof=1)
print("z-scores:", np.round(z, 2))
print("days with |z| > 1.5:", sales[np.abs(z) > 1.5])
print("72 is at percentile:", round(stats.percentileofscore(sales, 72), 1))z-scores: [-0.74 -1.08 0.04 -0.31 -0.48 0.81 1.85 -0.91 -0.57 -0.14 -0.39 0.64
2.11 -0.83]
days with |z| > 1.5: [72 75]
72 is at percentile: 92.9
Statistics by group
binned_statistic() computes a statistic for groups of values defined by bins of another variable — a quick way to summarise a scatter of points:
import numpy as np
from scipy import stats
rng = np.random.default_rng(4)
age = rng.uniform(20, 60, size=200)
income = 1000 + 40 * age + rng.normal(0, 300, size=200)
res = stats.binned_statistic(age, income, statistic="mean", bins=[20, 30, 40, 50, 60])
for lo, hi, m in zip(res.bin_edges[:-1], res.bin_edges[1:], res.statistic):
print(f"age {lo:.0f}-{hi:.0f}: mean income {m:7.1f}")
counts = stats.binned_statistic(age, income, statistic="count", bins=[20, 30, 40, 50, 60])
print("people per bin:", counts.statistic.astype(int))age 20-30: mean income 1946.4
age 30-40: mean income 2402.0
age 40-50: mean income 2818.4
age 50-60: mean income 3266.1
people per bin: [38 50 52 60]
Note: SciPy's statistics functions take an
axisargument, just like NumPy, and many acceptnan_policy="omit"to ignore missing values instead of returningnan.
Test yourself
Which measure of centre is least affected by a few extreme values?
- mean
- median
- harmonic mean
- variance
What does stats.trim_mean(data, 0.1) do?
- Rounds the mean to 1 decimal
- Removes 10% of values from each end, then averages the rest
- Averages only the top 10%
- Returns the 10th percentile
Which spread measure is robust to outliers?
- Standard deviation
- Variance
- Interquartile range
- Coefficient of variation