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

SciPy · Function

stats.ttest_ind

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.

How it works

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.

Syntax

scipy.stats.ttest_ind(a, b, *, axis=0, equal_var=True, nan_policy='propagate', alternative='two-sided', trim=0, method=None, keepdims=False)

Parameters

NameTypeDescription
aarray_likeFirst sample of scores.
barray_likeSecond sample of scores.
axisint or NoneAxis along which to compute the test; default is 0.
equal_varboolWhether 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.
trimfloatFraction of data trimmed from each end; default is 0.
methodResamplingMethod or NoneMethod used for the calculation; default is None.
keepdimsboolWhether reduced axes are retained; default is False.

Returns

TtestResult with statistic, pvalue, and df attributes; each may be a scalar or ndarray.

Key points

  • Use ttest_ind to compare the means of two independent samples.
  • The default test assumes equal population variances.
  • The t-statistic is the difference in sample means divided by the standard error.
  • alternative accepts two-sided, less, or greater to specify the hypothesis direction.
  • Set equal_var=False to perform Welch’s t-test without the equal-variance assumption.

Common mistakes

  • The default assumes equal population variances; use equal_var=False for Welch’s test when that assumption is not appropriate.

Example: Compare two independent samples

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.

When to use it

Independent group comparison

Compare average measurements from two unrelated groups.

Welch’s t-test

Analyze data with unequal group variances.

One-sided hypothesis testing

Test a directional claim about one group’s mean.

Trimmed-mean comparison

Reduce sensitivity to extreme observations.

Interview questions

QWhat does ttest_ind test?

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.

QWhat is the difference between equal_var=True and False?

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.

QHow does alternative affect the p-value?

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_stats
  • scipy.stats.ttest_rel
  • scipy.stats.ttest_1samp

Source: ttest_ind — SciPy v1.18.0 Manual. Checked 2026-10-15.