Python Tutorial
SciPy Statistics
scipy.stats provides distributions, descriptive stats, and hypothesis tests.
Describe a Sample
from scipy import stats
data = [12, 15, 14, 16, 15, 13, 17]
print(stats.describe(data))
print(stats.zscore(data))Normal Distribution
print(stats.norm.pdf(0))
print(stats.norm.cdf(1.96))
print(stats.norm.rvs(size=5, random_state=1))t-Test
result = stats.ttest_1samp(data, popmean=14)
print(result.statistic, result.pvalue)📘 Real-World Deep Dive
Knowing <strong>SciPy Stats (SciPy)</strong> well is what turns SciPy from a curiosity into a daily tool — you'll reach for it in nearly every real project.
Real-Life Scenario
An end-to-end usage of SciPy Stats that you'd actually see in a data pipeline or analytics notebook.
Real-Life Example
from scipy import stats
print(stats.norm.cdf(1.96)) # ≈ 0.975
print(stats.ttest_1samp([2,3,4,5,6], popmean=4))Expected Output
(see source)Common mistakes
- Many SciPy functions take
methodstrings with subtle spelling differences ("trust-constr"vs."trust-constr") — readscipy.optimize.least_squaresdocs. - Sparse matrices need explicit conversion to dense (
toarray()) before being fed to functions that don't acceptscipy.sparse. scipy.signalfunctions often return arrays whose length differs from input — always inspectlen(out)defensively.- Treating SciPy Stats as a black box without reading the docs — the API has subtle defaults that bite when you scale.
🚀 Performance & Best Practices
- Use vectorised
scipy.statsdistributions instead of looping per-sample for large parametric studies. scipy.sparse.csr_matrixis the right format for arithmetic;csc_matrixis right for slicing columns.- Prefer Cython/Numba (or NumPy ufuncs) over Python loops inside SciPy callbacks (e.g.
odeint). - When working with SciPy, prefer vectorised / batched operations over Python loops.
🧪 Try It Yourself
- Reproduce the snippet on a representative slice of your own data.
- Profile the snippet with
cProfileortimeitand find the single biggest improvement. - Generalise the snippet into a small, reusable function you can drop into future projects.