Mean comparisons often start with the question of whether an observed difference is larger than expected from sample variation. In SciPy, the scipy.stats t-test functions return a result object with the test statistic, p-value, degrees of freedom, and confidence interval methods that can feed a report or a later analysis step.
Use ttest_1samp() when one sample is compared with a reference mean, ttest_rel() when two measurements come from the same subjects or units, and ttest_ind() when the two groups are independent. The independent-sample calculation uses Welch's test with equal_var=False so the result does not assume equal population variances.
The p-value only answers the stated hypothesis test. Keep the sample relationship, the alternative hypothesis, and the confidence interval beside the result so the tested difference remains visible when the output is copied into a notebook, issue, or project report.
Steps to run a t-test with SciPy:
- Create a Python script named ttest_run.py.
- ttest_run.py
import numpy as np from scipy import stats sample = np.array([10.2, 9.8, 10.5, 10.1, 9.9, 10.4]) before = np.array([72, 75, 71, 70, 74, 73]) after = np.array([68, 72, 69, 66, 71, 70]) group_a = np.array([14.2, 13.8, 15.0, 14.5, 13.9, 14.7]) group_b = np.array([12.6, 13.1, 12.9, 12.4, 13.0, 12.8]) one_sample = stats.ttest_1samp(sample, popmean=10.0) paired = stats.ttest_rel(after, before) welch = stats.ttest_ind(group_a, group_b, equal_var=False) ci = welch.confidence_interval(confidence_level=0.95) print(f"one-sample vs 10.0: statistic={one_sample.statistic:.3f}, pvalue={one_sample.pvalue:.4f}, df={one_sample.df:.1f}") print(f"paired after-before: statistic={paired.statistic:.3f}, pvalue={paired.pvalue:.4f}, df={paired.df:.1f}") print(f"welch group A vs B: statistic={welch.statistic:.3f}, pvalue={welch.pvalue:.4f}, df={welch.df:.1f}") print(f"welch 95% CI: {ci.low:.2f} to {ci.high:.2f}") print("reject equal means at alpha=0.05:", welch.pvalue < 0.05)
The paired test subtracts before from after because ttest_rel(after, before) tests the mean of after - before. For the independent groups, equal_var=False runs Welch's t-test.
- Run the script.
$ python3 ttest_run.py one-sample vs 10.0: statistic=1.342, pvalue=0.2374, df=5.0 paired after-before: statistic=-10.304, pvalue=0.0001, df=5.0 welch group A vs B: statistic=7.087, pvalue=0.0001, df=7.8 welch 95% CI: 1.04 to 2.06 reject equal means at alpha=0.05: True
- Match the function to the sample relationship before replacing the arrays.
Use ttest_1samp() for one sample against popmean, ttest_rel() for paired measurements in the same order, and ttest_ind() for unrelated groups.
- Read pvalue against the selected significance threshold.
The Welch line prints True because 0.0001 < 0.05 for the independent groups. The one-sample result prints a larger p-value, so those sample values do not reject a mean of 10.0 at the same threshold.
- Read df and confidence_interval() from the result object.
For ttest_ind(group_a, group_b, equal_var=False), the confidence interval describes the population mean difference group_a - group_b. Welch's test can return a fractional df because it estimates degrees of freedom from the two group variances.
- Set alternative only when the hypothesis is directional.
stats.ttest_ind(group_a, group_b, equal_var=False, alternative="greater")
The default alternative=“two-sided” tests for any mean difference. Use “greater” or “less” only when the direction was chosen before looking at the result.
- Remove the demo script when it was only created for the check.
$ rm ttest_run.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.