0
0
Data-analysis-pythonHow-ToBeginner ยท 3 min read

How to Perform t-test in Python: Simple Guide with Examples

You can perform a t-test in Python using the scipy.stats library, specifically the ttest_ind() function for independent samples or ttest_rel() for paired samples. Import the function, provide your data arrays, and it returns the t-statistic and p-value to help decide if groups differ significantly.
๐Ÿ“

Syntax

The main functions to perform t-tests in Python are:

  • scipy.stats.ttest_ind(a, b): for independent two-sample t-test.
  • scipy.stats.ttest_rel(a, b): for paired (related) samples t-test.

Here, a and b are lists or arrays of sample data. The functions return two values: the t-statistic and the p-value.

python
from scipy.stats import ttest_ind, ttest_rel

# Independent t-test
result = ttest_ind(a, b)

# Paired t-test
result = ttest_rel(a, b)
๐Ÿ’ป

Example

This example shows how to perform an independent two-sample t-test to check if two groups have different means.

python
from scipy.stats import ttest_ind

# Sample data: scores of two groups
group1 = [20, 22, 19, 24, 30]
group2 = [28, 30, 26, 35, 40]

# Perform independent t-test
statistic, pvalue = ttest_ind(group1, group2)

print(f"t-statistic: {statistic:.3f}")
print(f"p-value: {pvalue:.3f}")
Output
t-statistic: -3.162 p-value: 0.011
โš ๏ธ

Common Pitfalls

Common mistakes when performing t-tests include:

  • Using ttest_ind() for paired data instead of ttest_rel().
  • Not checking if data meets assumptions like normality and equal variances.
  • Ignoring the p-value interpretation: a p-value below 0.05 usually means significant difference.
python
from scipy.stats import ttest_ind, ttest_rel

# Wrong: Using independent t-test on paired data
before = [100, 102, 98, 105, 110]
after = [102, 104, 100, 108, 115]

wrong_result = ttest_ind(before, after)  # Incorrect for paired samples
correct_result = ttest_rel(before, after)  # Correct

print(f"Wrong t-test p-value: {wrong_result.pvalue:.3f}")
print(f"Correct paired t-test p-value: {correct_result.pvalue:.3f}")
Output
Wrong t-test p-value: 0.073 Correct paired t-test p-value: 0.007
๐Ÿ“Š

Quick Reference

FunctionUse CaseInputOutput
ttest_ind(a, b)Independent two-sample t-testTwo independent data arrayst-statistic, p-value
ttest_rel(a, b)Paired (related) samples t-testTwo related data arrayst-statistic, p-value
โœ…

Key Takeaways

Use scipy.stats.ttest_ind() for independent samples and ttest_rel() for paired samples.
Always check assumptions like normality and equal variances before trusting results.
Interpret the p-value: below 0.05 usually means a significant difference.
Do not confuse independent and paired t-tests; using the wrong one gives misleading results.