Independent group comparison
Compare average measurements from two unrelated groups.
SciPy · Function
Tests whether two independent samples have identical expected means. It returns a t-statistic, p-value, and degrees of freedom, with optional Welch, one-sided, trimming, NaN-handling, and resampling-related controls.
The function tests whether two independent samples have the same expected mean. It computes a t-statistic from the difference between sample means and its standard error, then returns the statistic, p-value, and degrees of freedom; set equal_var=False for Welch’s test when equal population variances should not be assumed.
scipy.stats.ttest_ind(a, b, *, axis=0, equal_var=True, nan_policy='propagate', alternative='two-sided', trim=0, method=None, keepdims=False)| Name | Type | Description |
|---|---|---|
a | array_like | First sample of scores. |
b | array_like | Second sample of scores. |
axis | int or None | Axis along which to compute the test; default is 0. |
equal_var | bool | Whether to assume equal population variances; default is True. |
nan_policy | {‘propagate’, ‘omit’, ‘raise’} | How to handle NaNs: propagate, omit, or raise. |
alternative | {‘two-sided’, ‘less’, ‘greater’} | Alternative hypothesis: two-sided, less, or greater. |
trim | float | Fraction of data trimmed from each end; default is 0. |
method | ResamplingMethod or None | Method used for the calculation; default is None. |
keepdims | bool | Whether reduced axes are retained; default is False. |
TtestResult with statistic, pvalue, and df attributes; each may be a scalar or ndarray.
import numpy as np
from scipy.stats import ttest_ind
control = np.array([10, 11, 9, 10, 12])
treatment = np.array([14, 15, 13, 16, 14])
result = ttest_ind(control, treatment, equal_var=False)
print(result.statistic, result.pvalue, result.df)Runs Welch’s independent-samples t-test and prints the t-statistic, p-value, and degrees of freedom.
Compare average measurements from two unrelated groups.
Analyze data with unequal group variances.
Test a directional claim about one group’s mean.
Reduce sensitivity to extreme observations.
It tests the null hypothesis that two independent samples have identical expected means. The result includes a t-statistic, p-value, and degrees of freedom.
Follow-up: How would you avoid assuming equal variances? Set equal_var=False, which performs Welch’s t-test.
With equal_var=True, the function uses the standard independent two-sample t-test and assumes equal population variances. With equal_var=False, it uses Welch’s test instead.
Follow-up: Which parameter controls this choice? equal_var, whose default is True.
The alternative parameter selects the hypothesis direction: two-sided tests unequal means, while less and greater test whether the first mean is respectively below or above the second.
Follow-up: What happens with NaNs? nan_policy controls whether they propagate, are omitted, or cause an error.
scipy.stats.ttest_ind_from_statsscipy.stats.ttest_relscipy.stats.ttest_1samp