Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.ttest_1samp

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.

How it works

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.

Syntax

scipy.stats.ttest_1samp(a, popmean, axis=0, nan_policy='propagate', alternative='two-sided', *, keepdims=False)

Parameters

NameTypeDescription
aarray_likeSample observations.
popmeanfloat or array_likeExpected value under the null hypothesis. If array-like, it must match a's shape excluding the tested axis.
axisint or None, optionalAxis along which to compute the test; None uses the whole array.
nan_policy{'propagate', 'raise', 'omit'}, optionalHow to handle NaN values: propagate, raise, or omit.
alternative{'two-sided', 'less', 'greater'}, optionalDefines the alternative hypothesis; default is two-sided.
keepdimsbool, optionalWhether reduced axes are retained in the output shape.

Returns

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.

Key points

  • Use this function to test a sample mean against a specified reference mean.
  • The null hypothesis states that the sample's expected mean equals popmean.
  • Set axis=None to flatten the input and perform one test over all observations.
  • pvalue reflects the selected alternative hypothesis.
  • df equals the sample size along the tested axis minus one.
  • The t-statistic divides the mean difference by the standard error.

Common mistakes

  • When popmean is array-like, its shape must match a after removing the tested axis.
  • Choose nan_policy deliberately: the default propagates NaN values, while omit excludes them and raise reports an error.

Example: One-sample mean test

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.

When to use it

Compare a sample with a reference mean

Assess whether measurements differ from a known target or benchmark mean.

Vectorized axis-wise testing

Run separate one-sample tests across rows, columns, or other array dimensions.

One-sided inference

Evaluate directional hypotheses using less or greater alternatives.

Missing-data-aware testing

Analyze datasets containing missing observations with an explicit NaN policy.

Interview questions

QWhat hypothesis does ttest_1samp test?

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.

QHow does axis affect the calculation?

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.

QWhat does the returned result contain?

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_rel
  • scipy.stats.ttest_ind
  • scipy.stats.ttest_1sampResult

Source: scipy.stats.ttest_1samp. Checked 2026-10-15.