Skip to main content
Albero Academy

Statistics with scipy.stats

Work with probability distributions, compute descriptive stats, and run the hypothesis tests analysts use daily.

Chapter 2SciPy15 min readBeginner friendly
  • #SciPy
  • #Statistics
  • #Tests

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:

the normal distribution
from scipy import stats
# Standard normal (mean 0, std 1)
stats.norm.pdf(0) # peak height of the bell curve
stats.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

summaries
import numpy as np
data = np.array([12, 15, 14, 10, 18, 20, 16])
stats.describe(data) # count, min/max, mean, variance, skew, kurtosis
stats.zscore(data) # standardise each value
stats.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":

comparing two groups
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

TestUse for
ttest_indCompare means of two independent groups
ttest_relCompare paired/before-after measurements
chi2_contingencyAssociation between two categorical variables
pearsonrLinear correlation between two numeric variables
f_onewayCompare 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

  1. 01scipy.stats is the analyst's SciPy submodule: distributions + hypothesis tests.
  2. 02Distribution objects (stats.norm) share methods: pdf, cdf, rvs.
  3. 03ttest_ind compares two group means and returns a statistic + p-value (<0.05 = significant).
  4. 04A small p-value means "probably real", not "large" — report effect size too.
  5. 05Pick the test by data shape: t-test, paired t-test, chi-squared, correlation, ANOVA.