39 — Python Power Tools

scipy.stats & statsmodels#

Python✓ Mathematical
◆ The PatternStatistical tests, regression diagnostics, time series — the Python stats foundation

scipy.stats has every distribution and statistical test. statsmodels adds regression diagnostics, time series models (ARIMA, ADF), and proper statistical inference with confidence intervals — what sklearn deliberately leaves out.

// scipy.stats + statsmodels workflow
# scipy.stats — statistical tests
from scipy.stats import (
    ttest_ind, ttest_rel, mannwhitneyu,
    ks_2samp, shapiro, spearmanr
)

# Two-sample t-test
t_stat, p_val = ttest_ind(group_a, group_b)

# Normality check
stat, p = shapiro(data)

# statsmodels — regression with full diagnostics
import statsmodels.api as sm

X_sm = sm.add_constant(X)
model = sm.OLS(y, X_sm).fit()
print(model.summary())  # R², coefficients, p-values, CI

# Time series — ADF stationarity test
from statsmodels.tsa.stattools import adfuller
result = adfuller(series)
print(f"ADF Stat: {result[0]:.3f}, p: {result[1]:.4f}")
Taskscipy.statsstatsmodels
Compare two groupsttest_ind, mannwhitneyu
Normalityshapiro, normaltest
RegressionOLS with summary()
Time seriesARIMA, adfuller, acf
Distributionsnorm, beta, gamma, etc.
sklearn vs statsmodels: sklearn is for prediction (fit/predict). statsmodels is for inference (coefficients, p-values, diagnostics). Use both — they complement each other.
← Previous
pandas-ta & yfinance
Open in the full reader, with the topic sidebar →