🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

stats.ttest_ind()

AI & DATA SCIENCE // stats-ttest-ind

scipy.stats.ttest_ind() performs an independent two-sample t-test, checking whether the means of two independent groups are statistically significantly different from each other.

Syntax

scipy.stats.ttest_ind(a, b, equal_var=True)

Deep Dive Course

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.

editor.html
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))
localhost:3000

2Practical Example

Here is a real-world application of stats.ttest_ind() showing how it is used in production SciPy code.

editor.html
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)
localhost:3000

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.

editor.html
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))
localhost:3000

Examples

Example 01Basic Usage
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))
Example 02Advanced Example
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)

Best Practices

  • 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
  • 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
  • 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

Interview Question

Why doesn't a small p-value from ttest_ind() directly tell you the probability that the two groups are actually different?

Hint: Think about what a p-value is technically defined as, in terms of an assumption it starts from.

A p-value is defined as the probability of observing a difference at least as extreme as the one actually measured, assuming the null hypothesis is true, that there's actually no real difference between the groups' means. It's a statement about how surprising the observed data would be under that specific assumption, not a direct statement about the probability that the groups are truly different, or the probability that the null hypothesis itself is true — those would require additional information, like prior probabilities, that a p-value alone doesn't provide. A small p-value is evidence against the null hypothesis, but it isn't itself the probability the alternative is true.

Exercises

MediumPractice using stats.ttest_ind() in a real scenario.
View Solution
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))

Frequently Asked Questions

Why doesn't a small p-value from ttest_ind() directly tell you the probability that the two groups are actually different?

A p-value is defined as the probability of observing a difference at least as extreme as the one actually measured, assuming the null hypothesis is true, that there's actually no real difference between the groups' means. It's a statement about how surprising the observed data would be under that specific assumption, not a direct statement about the probability that the groups are truly different, or the probability that the null hypothesis itself is true — those would require additional information, like prior probabilities, that a p-value alone doesn't provide. A small p-value is evidence against the null hypothesis, but it isn't itself the probability the alternative is true.

Related Functions

stats-ksteststats-normaltestnp-mean