Implementing Common Hypothesis Tests in Python: Z‑test, t‑test, F‑test and Data Exploration
This article demonstrates how to conduct various hypothesis tests—including Z‑test, one‑sample and two‑sample t‑tests, F‑test, as well as distribution fitting and outlier detection—using Python libraries such as Statsmodels, SciPy, and pandas on both the Iris dataset and a human temperature dataset.
This article teaches how to perform common hypothesis tests with Python.
It explains that the choice of test depends on the underlying distribution: for example, when two sample variances follow an F‑distribution, the interval estimation uses F‑critical values and the F‑test is applied.
Python packages used
Statsmodels – statistical modeling and inference.
SciPy – scientific computing, including statistical functions.
Data import example (Iris dataset)
from sklearn.datasets import load_iris
import numpy as np
# Load Iris data
iris = load_iris()
iris = pd.DataFrame(iris.data, columns=['sepal_length','sepal_width','petal_legth','petal_width'])
print(iris)One‑sample Z‑test on Iris petal length (null hypothesis: mean = 4.2)
import statsmodels.stats.weightstats
z, pval = statsmodels.stats.weightstats.ztest(iris['petal_legth'], value=4.2)
print(z, pval) # Output: p < 0.05 → reject H0One‑sample t‑test on Iris petal length (null hypothesis: mean = 4.0)
import scipy.stats
t, pval = scipy.stats.ttest_1samp(iris['petal_legth'], popmean=4.0)
print(t, pval) # Output: p > 0.05 → fail to reject H0Two‑sample t‑test (simulated)
# Split Iris data
iris_1 = iris[iris.petal_legth >= 2]
iris_2 = iris[iris.petal_legth < 2]
print(np.mean(iris_1['petal_legth']))
print(np.mean(iris_2['petal_legth']))
import scipy.stats
t, pval = scipy.stats.ttest_ind(iris_1['petal_legth'], iris_2['petal_legth'])
print(t, pval) # p < 0.05 → reject H0Exercise: Human temperature dataset
The dataset contains 130 rows with three columns: Gender (1 = male, 2 = female), Temperature, and HeartRate.
Load and describe data
import numpy as np
import pandas as pd
from scipy import stats
data = pd.read_csv('C:\\Users\\baihua\\Desktop\\test.csv')
print(data.head())
print(data.describe())
# Mean temperature ≈ 98.25°FOne‑sample Z‑test for mean temperature = 98.6°F
import statsmodels.stats.weightstats
z, pval = statsmodels.stats.weightstats.ztest(data['Temperature'], value=98.6)
print(z, pval) # p < 0.05 → reject H0Normality test (Kolmogorov‑Smirnov)
stats.kstest(data['Temperature'], 'norm')
# Result: statistic=1.0, pvalue=0.0 → not normalFit t‑distribution and perform KS test
np.random.seed(1)
ks = stats.t.fit(data['Temperature'])
df, loc, scale = ks
t_estm = stats.t.rvs(df=df, loc=loc, scale=scale, size=data.shape[0])
print(stats.ks_2samp(data['Temperature'], t_estm)) # p≈0.43 → cannot reject t‑fitFit chi‑square distribution and perform KS test
chi_sq = stats.chi2.fit(data['Temperature'])
df, loc, scale = chi_sq
chi_estm = stats.chi2.rvs(df=df, loc=loc, scale=scale, size=data.shape[0])
print(stats.ks_2samp(data['Temperature'], chi_estm)) # p≈0.40 → cannot reject chi‑square fitIdentify outliers using chi‑square quantiles (2.5% and 92.5%)
lower = chi_sq_distribution.ppf(0.025)
upper = chi_sq_distribution.ppf(0.925)
print(data['Temperature'][data['Temperature'] < lower])
print(data['Temperature'][data['Temperature'] > upper])Gender temperature difference (two‑sample t‑test)
male = data[data['Gender'] == 1]['Temperature']
female = data[data['Gender'] == 2]['Temperature']
t, pval = scipy.stats.ttest_ind(male, female)
print(t, pval) # p < 0.05 → significant differenceCorrelation between temperature and heart rate
stat, p = stats.pearsonr(data['HeartRate'], data['Temperature'])
print('stat=%.3f, p=%.3f' % (stat, p)) # stat≈0.004 → no correlationFigures illustrating distributions and scatter plots are included in the original article.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.