ttest_ind() returns a t-statistic and a p-value, where a small p-value, conventionally below 0.05, suggests the two groups' means are unlikely to be this different purely by random chance, providing evidence that a real difference exists between them. The equal_var parameter controls whether the test assumes both groups have the same underlying variance, the standard Student's t-test, the default, or allows for different variances, Welch's t-test, generally the more robust and recommended choice when you're not confident the two groups' variances are actually similar.
1Understanding stats.ttest_ind()
ttest_ind() returns a t-statistic and a p-value, where a small p-value, conventionally below 0.05, suggests the two groups' means are unlikely to be this different purely by random chance, providing evidence that a real difference exists between them. The equal_var parameter controls whether the test assumes both groups have the same underlying variance, the standard Student's t-test, the default, or allows for different variances, Welch's t-test, generally the more robust and recommended choice when you're not confident the two groups' variances are actually similar.
Set equal_var=False to use Welch's t-test instead of the standard Student's t-test, unless you have good reason to believe the two groups genuinely have similar variances — Welch's version is generally considered the safer default, since assuming equal variances when they're actually quite different can produce a misleading p-value.
from scipy import stats
import numpy as np
group_a = np.array([20, 22, 19, 24, 25])
group_b = np.array([28, 30, 27, 32, 29])
t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(round(p_value, 4))2Practical Example
Here is a real-world application of stats.ttest_ind() showing how it is used in production SciPy code.
from scipy import stats
import numpy as np
group_a = np.array([20, 22, 19, 24, 25])
group_b = np.array([21, 23, 20, 22, 24])
t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(p_value > 0.05)3Best Practices
Follow these guidelines when working with stats.ttest_ind():
1. Use equal_var=False, Welch's t-test, as a generally safer default, unless you have specific reason to believe the two groups have equal variance
2. Interpret the p-value as evidence against the null hypothesis of no difference, not as a direct probability that a real difference exists — a common statistical misinterpretation
3. Check that the groups are genuinely independent samples before using this test, since a paired/dependent-samples situation calls for a different test, like a paired t-test, instead
Tip: Set equal_var=False to use Welch's t-test instead of the standard Student's t-test, unless you have good reason to believe the two groups genuinely have similar variances — Welch's version is generally considered the safer default, since assuming equal variances when they're actually quite different can produce a misleading p-value.
from scipy import stats
import numpy as np
group_a = np.array([20, 22, 19, 24, 25])
group_b = np.array([28, 30, 27, 32, 29])
t_stat, p_value = stats.ttest_ind(group_a, group_b)
print(round(p_value, 4))