Learning Statistics with Python
Performing a t-test in Python
To perform a t-test in Python, you only need to choose the alternative hypothesis and whether variances are homogeneous(roughly equal).
ttest_ind() function from
scipy.stats takes care of everything else. Here is its syntax:
a— first sample;
b— second sample;
Trueif variances are roughly equal and
Falseif they aren't;
alternative— type of alternative hypothesis:
'two-sided'— means are not equal;
'less'— the first mean is less than the second;
'greater'— the first mean is greater than the second.
statistic— the value of t statistic;
pvalue— the p-value.
We are interested in the
pvalue. If it is lower than α(usually 0.05), then the t statistic is in the critical region, so we should accept the alternative hypothesis. And if
pvalue is greater than α — we accept the null hypothesis that means are equal.
Here an example of applying t-test to our heights dataset: