Statistics

scipy.stats covers two related things: probability distributions you can query directly, and descriptive statistics you can compute over your own data.

Working with a distribution

Each distribution — norm for the normal distribution, expon for exponential, and dozens more — comes with methods like .pdf() (probability density at a point) and .cdf() (cumulative probability up to a point):

>>> the standard normal distribution
from scipy import stats

print(stats.norm.pdf(0))
print(stats.norm.cdf(1.96))
Output
0.3989422804014327
0.9750021048517795

stats.norm defaults to a standard normal distribution (mean 0, standard deviation 1). pdf(0) gives the height of the bell curve at its peak; cdf(1.96) says that about 97.5% of the distribution lies at or below 1.96 — the number behind the familiar "95% confidence interval" rule of thumb.

To use a different mean and spread, pass loc (the mean) and scale (the standard deviation):

>>> a shifted, wider distribution
from scipy import stats

dist = stats.norm(loc=100, scale=15)
print(dist.cdf(115))
Output
0.8413447460685429

With a mean of 100 and a standard deviation of 15 (a classic IQ-score-style distribution), about 84% of values fall at or below 115 — exactly one standard deviation above the mean.

Note: distribution parameters aren't always named the way you'd guess from a statistics textbook. stats.expon's scale parameter, for instance, is 1 / rate, not the rate itself — plug in the rate directly and you'll silently get the wrong distribution. Always check a distribution's specific parameterization in the SciPy docs before trusting the output.

Descriptive statistics on your own data

stats.describe computes several summary statistics over a dataset in one call:

>>> summarizing a small dataset
from scipy import stats

data = [4, 8, 6, 5, 3, 7]
summary = stats.describe(data)

print(summary.mean)
print(summary.variance)
print(summary.minmax)
Output
5.5
3.5
(3, 8)

summary.mean is the average of the six values. summary.variance is the sample variance (dividing by n - 1, not n — the standard choice when your data is a sample rather than an entire population). summary.minmax is a tuple of the smallest and largest values seen.