Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Performing a t-test in Python | Statistical Testing
course content

Contenido del Curso

Learning Statistics with Python

Performing a t-test in PythonPerforming a t-test in Python

To conduct a t-test in Python, all you have to do is specify the alternative hypothesis and indicate whether variances are roughly equal (homogeneous).

The ttest_ind() function within scipy.stats handles the rest. Below is the syntax:

Parameters:

  • a — the first sample;
  • b — the second sample;
  • equal_var — set to True if variances are approximately equal, and False if they are not;
  • alternative — the type of alternative hypothesis:
    • 'two-sided' — indicates that the means are not equal;
    • 'less' — implies that the first mean is less than the second;
    • 'greater' — implies that the first mean is greater than the second.

Return values:

  • statistic — the value of the 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 is an example of applying the t-test to our heights dataset:

¿Todo estuvo claro?

Sección 6. Capítulo 6
course content

Contenido del Curso

Learning Statistics with Python

Performing a t-test in PythonPerforming a t-test in Python

To conduct a t-test in Python, all you have to do is specify the alternative hypothesis and indicate whether variances are roughly equal (homogeneous).

The ttest_ind() function within scipy.stats handles the rest. Below is the syntax:

Parameters:

  • a — the first sample;
  • b — the second sample;
  • equal_var — set to True if variances are approximately equal, and False if they are not;
  • alternative — the type of alternative hypothesis:
    • 'two-sided' — indicates that the means are not equal;
    • 'less' — implies that the first mean is less than the second;
    • 'greater' — implies that the first mean is greater than the second.

Return values:

  • statistic — the value of the 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 is an example of applying the t-test to our heights dataset:

¿Todo estuvo claro?

Sección 6. Capítulo 6
some-alt