t-test



To illustrate the use of the t-test, we will show a simple example using the iris dataset. Suppose we observe two independent samples, e.g. flower sepal lengths, and we are considering whether the two samples were drawn from the same population (e.g. the same species of flower or two species with similar sepal characteristics) or two different populations.

The t-test quantifies the difference between the arithmetic means of the two samples. The p-value quantifies the probability of obtaining the observed results, assuming the null hypothesis (that the samples are drawn from populations with the same population means) is true. A p-value larger than a chosen threshold (e.g. 5% or 0.05) indicates that our observation is not so unlikely to have occurred by chance. Therefore, we accept the null hypothesis of equal population means. If the p-value is smaller than our threshold, then we have evidence against the null hypothesis of equal population means.
 

T-Test Input




The inputs or parameters necessary for performing a t-test are:

- Two arrays a and b containing the data for sample 1 and sample 2

T-Test Outputs




The t-test returns the following:

- The calculated t-statistics
- The p-value
In [1]:
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
 

Load Iris Dataset

In [2]:
from sklearn import datasets
iris = datasets.load_iris()
sep_length = iris.data[:,0]
a_1, a_2 = train_test_split(sep_length, test_size=0.4, random_state=0)
b_1, b_2 = train_test_split(sep_length, test_size=0.4, random_state=1)
In [3]:
mu1 = np.mean(a_1)

mu2 = np.mean(b_1)

np.std(a_1)

np.std(b_1)
Out[3]:
np.float64(0.7912242428472069)
 
Calculate the sample means and sample variances
In [4]:
stats.ttest_ind(a_1, b_1, equal_var = False)
Out[4]:
TtestResult(statistic=np.float64(0.830066093774641), pvalue=np.float64(0.4076270841218669), df=np.float64(175.8171155714046))
 

Implement t-test

In [5]:
stats.ttest_ind(b_1, a_1, equal_var=False)
Out[5]:
TtestResult(statistic=np.float64(-0.830066093774641), pvalue=np.float64(0.4076270841218669), df=np.float64(175.8171155714046))
In [6]:
stats.ttest_ind(a_1, b_1, equal_var=True)
Out[6]:
TtestResult(statistic=np.float64(0.830066093774641), pvalue=np.float64(0.4076132965045395), df=np.float64(178.0))
In [7]:
a_1, a_2 = train_test_split(sep_length, test_size=0.4, random_state=0)
b_1, b_2 = train_test_split(sep_length, test_size=0.5, random_state=1)
 

Observations


We observe that the using “true” or “false” for the “equal-var” parameter does not change the t-test results that much. We also observe that interchanging the order of the sample arrays a_1 and b_1 yields a negative t-test value, but does not change the magnitude of the t-test value, as expected. Since the calculated p-value is way larger than the threshold value of 0.05, we can reject the null hypothesis that the difference between the means of sample 1 and sample 2 are significant. This shows that the sepal lengths for sample 1 and sample 2 were drawn from same population data.
 

Implementation in Python: Using Unequal Sample Size

In [8]:
a_1, a_2 = train_test_split(sep_length, test_size=0.4, random_state=0)
b_1, b_2 = train_test_split(sep_length, test_size=0.5, random_state=1)
 
Calculate the sample means and sample variances
In [9]:
mu1 = np.mean(a_1)

mu2 = np.mean(b_1)

np.std(a_1)

np.std(b_1)
Out[9]:
np.float64(0.8016915450055172)
 

Implement t-test



In [10]:
stats.ttest_ind(a_1, b_1, equal_var = False)
Out[10]:
TtestResult(statistic=np.float64(0.808385246795547), pvalue=np.float64(0.4200557921940715), df=np.float64(161.80531235936976))
In [11]:
stats.ttest_ind(a_1, b_1, equal_var = False)
Out[11]:
TtestResult(statistic=np.float64(0.808385246795547), pvalue=np.float64(0.4200557921940715), df=np.float64(161.80531235936976))
 

Observations




We observe that using samples with unequal size does not change the t-statistics and p-value significantly.




























































Summary of t-test Results: Iris Sepal Length
Equal Sample Size Unequal Sample Size
n₁ 90 90
n₂ 90 75
μ₁ 5.838888889 5.838888889
μ₂ 5.734444444 5.734444444
s₁ 0.884897288 0.884897288
s₂ 0.791224243 0.801691545
Δμ 0.104444444 0.104444444
t 0.830066094 0.808385247
p-value 0.407627084 0.420055792




In summary, we’ve shown how a simple t-test could be implemented using the scipy library in python.
In [ ]: