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

SciPy · Function

stats.ttest_rel

Tests whether two related or repeated samples have different means. It returns a TtestResult containing the t-statistic, p-value, and degrees of freedom.

Since
1.6.0

How it works

The function compares two same-shaped, paired samples by testing their within-pair differences against a zero mean. It can operate along a selected axis, choose how to handle missing values, test a directional or two-sided alternative, and optionally preserve reduced dimensions.

Syntax

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

Parameters

NameTypeDescription
aarray_likeFirst paired sample; must have the same shape as b.
barray_likeSecond paired sample; must have the same shape as a.
axisint or None, default 0Axis along which to compute the test. If None, compute over the whole arrays.
nan_policy{‘propagate’, ‘raise’, ‘omit’}, default ‘propagate’Defines how to handle NaNs: propagate, raise, or omit.
alternative{‘two-sided’, ‘less’, ‘greater’}, default ‘two-sided’Defines the alternative hypothesis: two-sided, less, or greater.
keepdimsbool, default FalseIf True, reduced axes are retained with size one.

Returns

TtestResult with statistic, pvalue, and df attributes. statistic is the t-statistic; pvalue is the p-value for the chosen alternative; df is one less than the sample size along the test axis.

Key points

  • Use this function for paired or repeated observations rather than independent samples.
  • The default two-sided alternative tests for a difference in the underlying means.
  • The less alternative tests whether the first input has a smaller underlying mean.
  • The greater alternative tests whether the first input has a larger underlying mean.
  • The returned degrees of freedom equal the paired sample count along the test axis minus one.

Common mistakes

  • Pass paired observations in matching positions; the two input arrays must have identical shapes.
  • With the default missing-value policy, NaNs cause the corresponding result to be NaN.
  • Using omit can change the effective sample size independently for each axis slice.

Example: Paired measurements

import numpy as np
from scipy import stats

before = np.array([10, 12, 9, 11, 13])
after = np.array([11, 13, 10, 12, 14])
result = stats.ttest_rel(after, before)
print(result.statistic, result.pvalue, result.df)

Compares paired measurements and prints the t-statistic, p-value, and degrees of freedom.

When to use it

Before-and-after analysis

Compare measurements from the same subjects before and after an intervention.

Matched-condition comparison

Evaluate two conditions measured on each participant or experimental unit.

Axis-wise testing

Run paired tests independently across rows, columns, or other dimensions.

Interview questions

QWhat statistical test does ttest_rel perform?

It is a paired-sample t-test for related or repeated measurements. The null hypothesis is that the two related samples have identical average values, and the calculation is based on their paired differences.

Follow-up: How is the statistic formed? Compute the mean of a - b and divide it by the standard error of those differences; the degrees of freedom are one less than the paired sample size.

QWhat do the shape and axis requirements mean?

a and b must have the same shape because values at corresponding positions form pairs. The axis argument selects the dimension along which each paired test is computed; None flattens both inputs conceptually into one test.

Follow-up: What happens with multidimensional inputs? A result is produced for each remaining axis position, unless keepdims=True retains the reduced axis with length one.

QHow does alternative affect the p-value?

The alternative can be two-sided, less, or greater. These correspond respectively to unequal means, the first sample having a smaller mean, or the first sample having a larger mean than the second.

Follow-up: How should missing values be handled? Choose propagate, raise, or omit depending on whether NaNs should invalidate, reject, or be excluded from each calculation.

  • scipy.stats.ttest_ind
  • scipy.stats.ttest_1samp
  • scipy.stats.TtestResult

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