scipy.stats is the most-used SciPy submodule for analysts. It gives you probability distributions to model data and the hypothesis tests to make decisions from it — the computational side of the statistics tutorial.
Distributions
Each distribution is an object with the same methods — pdf, cdf, rvs (random samples), and more:
from scipy import stats# Standard normal (mean 0, std 1)stats.norm.pdf(0) # peak height of the bell curvestats.norm.cdf(1.96) # ≈ 0.975 (area to the left)stats.norm.rvs(size=1000) # 1000 random samples# A normal with mean 100, std 15 (e.g. IQ scores)stats.norm(loc=100, scale=15).cdf(115) # P(X < 115)
Descriptive stats
import numpy as npdata = np.array([12, 15, 14, 10, 18, 20, 16])stats.describe(data) # count, min/max, mean, variance, skew, kurtosisstats.zscore(data) # standardise each valuestats.mode(data) # most frequent value
The t-test
A t-test asks whether two groups' means differ more than chance would predict. It returns a statistic and a p-value — below 0.05 is the conventional threshold for "statistically significant":
before = [55, 62, 58, 60, 51]after = [70, 75, 68, 72, 69]t, p = stats.ttest_ind(before, after)print(f"t = {t:.2f}, p = {p:.4f}")# p < 0.05 → the improvement is unlikely to be pure chance
Other tests
| Test | Use for |
|---|---|
ttest_ind | Compare means of two independent groups |
ttest_rel | Compare paired/before-after measurements |
chi2_contingency | Association between two categorical variables |
pearsonr | Linear correlation between two numeric variables |
f_oneway | Compare means across 3+ groups (ANOVA) |
Practice
Simulate two groups of exam scores with stats.norm.rvs, run a ttest_ind, and interpret the p-value. Then compute the correlation between two related columns with pearsonr.
Key takeaways
- 01scipy.stats is the analyst's SciPy submodule: distributions + hypothesis tests.
- 02Distribution objects (stats.norm) share methods: pdf, cdf, rvs.
- 03ttest_ind compares two group means and returns a statistic + p-value (<0.05 = significant).
- 04A small p-value means "probably real", not "large" — report effect size too.
- 05Pick the test by data shape: t-test, paired t-test, chi-squared, correlation, ANOVA.
