course content

Course Content

Learning Statistics with Python

Performing a t-test in PythonPerforming 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).
The ttest_ind() function from scipy.stats takes care of everything else. Here is its syntax:

Parameters:

  • a — first sample;
  • b — second sample;
  • equal_var — set True if variances are roughly equal and False if 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.

Returns:

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

Section 6.

Chapter 6