How to run a one-sample t-test in SciPy (with Code)
The Short Answer (TL;DR)
Call scipy.stats.ttest_1samp with popmean set to the fixed reference mean you want to test. Report the sample mean, difference from that reference, two-sided p-value and confidence interval for the population mean.
from scipy import stats
sample = [9.7, 10.5, 10.1, 10.8, 9.9, 10.6, 10.4, 10.2]
result = stats.ttest_1samp(sample, popmean=10.0, alternative="two-sided", nan_policy="raise"); print(result)
Detailed Implementation
This example uses synthetic data and does not need a network connection. Install the recorded package versions in a separate Python environment before running it.
python -m pip install numpy==2.5.4 scipy==1.18.1
from pathlib import Path
import numpy as np
from scipy import stats
# Independent synthetic observations and a reference specified in advance.
sample = np.array([9.7, 10.5, 10.1, 10.8, 9.9, 10.6, 10.4, 10.2])
reference_mean = 10.0
if sample.ndim != 1 or sample.size < 2 or not np.isfinite(sample).all():
raise ValueError("Use at least two finite observations in one dimension.")
if sample.std(ddof=1) == 0:
raise ValueError("The t standard error requires nonzero sample variance.")
result = stats.ttest_1samp(sample, popmean=reference_mean,
alternative="two-sided", nan_policy="raise")
interval = result.confidence_interval(confidence_level=0.95)
report = (
f"Sample size: {sample.size}\n"
f"Sample mean: {sample.mean():.6f}\n"
f"Mean minus reference: {sample.mean()-reference_mean:.6f}\n"
f"One-sample t: {result.statistic:.6f}\n"
f"Degrees of freedom: {result.df:.0f}\n"
f"Two-sided p-value: {result.pvalue:.8g}\n"
f"95% CI for population mean: [{interval.low:.6f}, {interval.high:.6f}]\n"
)
print(report)
Path("one-sample-t-test-output.txt").write_text(report, encoding="utf-8")
The report contains eight observations, a mean of 10.275 and a mean-minus-reference difference of 0.275 synthetic units. The 95% interval for the population mean includes the reference 10.0, consistent with a two-sided p-value above 0.05. The output is a text report, not a claim that the population mean equals the reference.
Is the reference mean known or estimated?
popmean states the null value and is treated as fixed. Choose it from the question before inspecting the sample. If it is estimated from another finite sample, an ordinary one-sample test ignores that estimate's uncertainty; an appropriate independent-sample or paired analysis is usually required instead.
Which assumptions support the test?
Observations should come from a suitable independent sampling process. With independent normal observations and nonzero variance, the t reference distribution is exact. Large-sample approximations need suitable moment and sampling conditions; a small, skewed or outlier-contaminated sample may not support them. The example rejects missing and non-finite values rather than quietly changing the sample.
Does a large p-value prove the reference is correct?
No. A p-value above a selected threshold means the test did not provide enough evidence against that specific mean under its assumptions. It does not prove equality or practical equivalence. Report the estimated difference and precision, and use a prespecified equivalence design if equivalence is the question.
Mathematical Context
Let xbar be the sample mean, s be the sample standard deviation calculated with n minus one in its variance denominator, and mu_0 be the fixed null mean. Divide xbar minus mu_0 by s divided by the square root of n to obtain the statistic, with n minus one degrees of freedom. The two-sided p-value is twice the Student t upper-tail probability beyond the absolute statistic. The result confidence_interval method returns limits for the population mean itself: xbar plus or minus a t critical value times the standard error. To express those limits as a mean difference from the reference, subtract mu_0 from both endpoints. A 95% confidence procedure covers a fixed population mean in 95% of repeated samples under its assumptions; it does not assign 95% probability to that fixed parameter after observing this interval.
\[ t=\frac{\bar{x}-\mu_0}{s/\sqrt{n}},\qquad \nu=n-1 \]
Reproducibility
Executed with Python 3.12.14; numpy 2.5.4, scipy 1.18.1. Automated checks confirm the stated output properties.