Compare a sample with a reference mean
Assess whether measurements differ from a known target or benchmark mean.
SciPy · Function
Tests whether the mean of one sample equals a specified population mean under a one-sample t-test. It supports axis-wise computation, configurable NaN handling, alternative hypotheses, and dimension-preserving output.
The function compares the mean of independent observations with a specified population mean. It computes a t-statistic from the difference between the sample mean and hypothesized mean divided by the standard error, then returns the statistic, p-value, and degrees of freedom.
scipy.stats.ttest_1samp(a, popmean, axis=0, nan_policy='propagate', alternative='two-sided', *, keepdims=False)| Name | Type | Description |
|---|---|---|
a | array_like | Sample observations. |
popmean | float or array_like | Expected value under the null hypothesis. If array-like, it must match a's shape excluding the tested axis. |
axis | int or None, optional | Axis along which to compute the test; None uses the whole array. |
nan_policy | {'propagate', 'raise', 'omit'}, optional | How to handle NaN values: propagate, raise, or omit. |
alternative | {'two-sided', 'less', 'greater'}, optional | Defines the alternative hypothesis; default is two-sided. |
keepdims | bool, optional | Whether reduced axes are retained in the output shape. |
An object with statistic, pvalue, and df attributes. statistic is the t-statistic, pvalue is the p-value for the selected alternative, and df is the degrees of freedom.
import numpy as np
from scipy.stats import ttest_1samp
sample = np.array([9.8, 10.2, 10.1, 9.9, 10.0])
result = ttest_1samp(sample, popmean=10.0)
print(result.statistic, result.pvalue, result.df)Tests whether the sample mean differs from 10.0 and prints the statistic, p-value, and degrees of freedom.
Assess whether measurements differ from a known target or benchmark mean.
Run separate one-sample tests across rows, columns, or other array dimensions.
Evaluate directional hypotheses using less or greater alternatives.
Analyze datasets containing missing observations with an explicit NaN policy.
It performs a one-sample t-test of the null hypothesis that the expected sample mean equals popmean. The returned statistic is based on the sample mean, hypothesized mean, and standard error.
Follow-up: How do you interpret the statistic's sign? It is positive when the sample mean exceeds popmean and negative when it is smaller.
The axis parameter selects the dimension along which each test is computed, and None flattens the input before testing. The result can therefore contain one statistic and p-value per remaining slice.
Follow-up: What happens with a multidimensional popmean? It must match the shape of a with the tested axis removed.
The result contains statistic, pvalue, and df. The degrees of freedom are one less than the number of observations along the tested axis.
Follow-up: How can missing values be handled? Use nan_policy='propagate', 'raise', or 'omit' according to the desired behavior.
scipy.stats.ttest_relscipy.stats.ttest_indscipy.stats.ttest_1sampResult