Fundamentals 9 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Implementing Common Hypothesis Tests in Python: Z‑test, t‑test, F‑test and Data Exploration

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 H0

One‑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 H0

Two‑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 H0

Exercise: 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°F

One‑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 H0

Normality test (Kolmogorov‑Smirnov)

stats.kstest(data['Temperature'], 'norm')
# Result: statistic=1.0, pvalue=0.0 → not normal

Fit 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‑fit

Fit 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 fit

Identify 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 difference

Correlation between temperature and heart rate

stat, p = stats.pearsonr(data['HeartRate'], data['Temperature'])
print('stat=%.3f, p=%.3f' % (stat, p))  # stat≈0.004 → no correlation

Figures illustrating distributions and scatter plots are included in the original article.

Pythonstatisticsdata analysishypothesis testingt-testz-test
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.