# How to Perform Welch’s t-test in Python

The most common way to compare the means between two independent groups is to use a two-sample t-test. However, this test assumes that the variances between the two groups is equal.

If you suspect that the variance between the two groups is not equal, then you can instead use Welch’s t-test, which is the non-parametric equivalent of the two-sample t-test.

To perform Welch’s t-test in Python, we can use the ttest_ind() function from the SciPy library, which uses the following syntax:

ttest_ind(a, b, equal_var=False)

where:

• a: First array of data values
• b: Second array of data values
• equal_var: Specifies no assumption of equal variances between the two arrays

This tutorial explains how to use this function to perform Welch’s t-test in Python.

### Example: Welch’s t-test in Python

Suppose we want to compare the exam scores of 12 students who used an exam prep booklet to prepare for some exam vs. 12 students who did not.

The following code shows how to perform Welch’s t-test in Python to determine if the mean exam scores are equal between the two groups:

```#import ttest_ind() function
from scipy import stats

#define two arrays of data
booklet = [90, 85, 88, 89, 94, 91, 79, 83, 87, 88, 91, 90]
no_booklet = [67, 90, 71, 95, 88, 83, 72, 66, 75, 86, 93, 84]

#perform Welch's t-test
stats.ttest_ind(booklet, no_booklet, equal_var = False)

Ttest_indResult(statistic=2.23606797749, pvalue=0.04170979503207)
```

The test statistic turns out to be 2.2361 and the corresponding p-value is 0.0417.

Since this p-value is less than .05, we can reject the null hypothesis of the test and conclude that there is a statistically significant difference in mean exam scores between the two groups.

Note that the two sample sizes in this example were equal, but Welch’s t-test still works even if the two sample sizes are not equal.

## 2 Replies to “How to Perform Welch’s t-test in Python”

1. George says:

This test is NOT the non-parametric equivalent of the two-sample t-test. It just doesn’t pool samples to estimate variance. But it’s still parametric, of course

2. Bob Sinclair says:

Thanks Zach.
Would be good to post variance of each student set
>>> np.var(booklet)
>>> 14.909722222222223
>>> np.var(no_booklet)
>>> 95.47222222222223

Ideally, would be great to modify the datasets so that the null hypothesis is rejected only in the Welch’s (equal_var=False) scenario as here t-test and Wech’s t-test are 0.036 and 0.041 respectively.