#### Quick-reference guide to the 15 statistical hypothesis tests that you need inapplied machine learning, with sample code in Python.

Although there are hundreds of statistical hypothesis tests that you could use, there is only a small subset that you may need to use in a machine learning project.

In this post, you will discover a cheat sheet for the most popular statistical hypothesis tests for a machine learning project with examples using the Python API.

Each statistical test is presented in a consistent way, including:

• The name of the test.
• What the test is checking.
• The assumptions of the test.
• How the test result is interpreted.
• Python API for using the test.

Note, when it comes to assumptions such as the expected distribution of or sample size, the results of a given test are likely to degrade gracefully rather than become immediately unusable if an assumption is violated.

Generally, data samples need to be representative of the domain and large enough to expose their distribution to analysis.

In some cases, the data can be corrected to meet the assumptions, such as correcting a nearly normal distribution to be normal by removing outliers, or using a correction to the degrees of freedom in a statistical test when samples have differing variance, to name two examples.

Finally, there may be multiple tests for a given concern, e.g. normality. We cannot get crisp answers to questions with statistics; instead, we get probabilistic answers. As such, we can arrive at different answers to the same question by considering the question in different ways. Hence the need for multiple different tests for some questions we may have about data.

Let’s get started.

Statistical Hypothesis Tests in Python Cheat Sheet
Photo by davemichuda, some rights reserved.

## Tutorial Overview

This tutorial is divided into four parts; they are:

1. Normality Tests
2. Correlation Tests
3. Parametric Statistical Hypothesis Tests
4. Nonparametric Statistical Hypothesis Tests

## 1. Normality Tests

This section lists statistical tests that you can use to check if your data has a Gaussian distribution.

### Shapiro-Wilk Test

Tests whether a data sample has a Gaussian distribution.

Assumptions

• Observations in each sample are independent and identically distributed (iid).

Interpretation

• H0: the sample has a Gaussian distribution.
• H1: the sample does not have a Gaussian distribution.

Python Code

### D’Agostino’s K^2 Test

Tests whether a data sample has a Gaussian distribution.

Assumptions

• Observations in each sample are independent and identically distributed (iid).

Interpretation

• H0: the sample has a Gaussian distribution.
• H1: the sample does not have a Gaussian distribution.

Python Code

### Anderson-Darling Test

Tests whether a data sample has a Gaussian distribution.

Assumptions

• Observations in each sample are independent and identically distributed (iid).

Interpretation

• H0: the sample has a Gaussian distribution.
• H1: the sample does not have a Gaussian distribution.

Python Code

## 2. Correlation Tests

This section lists statistical tests that you can use to check if two samples are related.

### Pearson’s Correlation Coefficient

Tests whether two samples have a monotonic relationship.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample are normally distributed.
• Observations in each sample have the same variance.

Interpretation

• H0: the two samples are independent.
• H1: there is a dependency between the samples.

Python Code

### Spearman’s Rank Correlation

Tests whether two samples have a monotonic relationship.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.

Interpretation

• H0: the two samples are independent.
• H1: there is a dependency between the samples.

Python Code

### Kendall’s Rank Correlation

Tests whether two samples have a monotonic relationship.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.

Interpretation

• H0: the two samples are independent.
• H1: there is a dependency between the samples.

Python Code

### Chi-Squared Test

Tests whether two categorical variables are related or independent.

Assumptions

• Observations used in the calculation of the contingency table are independent.
• 25 or more examples in each cell of the contingency table.

Interpretation

• H0: the two samples are independent.
• H1: there is a dependency between the samples.

Python Code

## 3. Parametric Statistical Hypothesis Tests

This section lists statistical tests that you can use to compare data samples.

### Student’s t-test

Tests whether the means of two independent samples are significantly different.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample are normally distributed.
• Observations in each sample have the same variance.

Interpretation

• H0: the means of the samples are equal.
• H1: the means of the samples are unequal.

Python Code

### Paired Student’s t-test

Tests whether the means of two paired samples are significantly different.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample are normally distributed.
• Observations in each sample have the same variance.
• Observations across each sample are paired.

Interpretation

• H0: the means of the samples are equal.
• H1: the means of the samples are unequal.

Python Code

### Analysis of Variance Test (ANOVA)

Tests whether the means of two or more independent samples are significantly different.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample are normally distributed.
• Observations in each sample have the same variance.

Interpretation

• H0: the means of the samples are equal.
• H1: one or more of the means of the samples are unequal.

Python Code

### Repeated Measures ANOVA Test

Tests whether the means of two or more paired samples are significantly different.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample are normally distributed.
• Observations in each sample have the same variance.
• Observations across each sample are paired.

Interpretation

• H0: the means of the samples are equal.
• H1: one or more of the means of the samples are unequal.

Python Code

Currently not supported in Python.

## 4. Nonparametric Statistical Hypothesis Tests

### Mann-Whitney U Test

Tests whether the distributions of two independent samples are equal or not.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.

Interpretation

• H0: the distributions of both samples are equal.
• H1: the distributions of both samples are not equal.

Python Code

### Wilcoxon Signed-Rank Test

Tests whether the distributions of two paired samples are equal or not.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.
• Observations across each sample are paired.

Interpretation

• H0: the distributions of both samples are equal.
• H1: the distributions of both samples are not equal.

Python Code

### Kruskal-Wallis H Test

Tests whether the distributions of two or more independent samples are equal or not.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.

Interpretation

• H0: the distributions of all samples are equal.
• H1: the distributions of one or more samples are not equal.

Python Code

### Friedman Test

Tests whether the distributions of two or more paired samples are equal or not.

Assumptions

• Observations in each sample are independent and identically distributed (iid).
• Observations in each sample can be ranked.
• Observations across each sample are paired.

Interpretation

• H0: the distributions of all samples are equal.
• H1: the distributions of one or more samples are not equal.

Python Code

This section provides more resources on the topic if you are looking to go deeper.

## Summary

In this tutorial, you discovered the key statistical hypothesis tests that you may need to use in a machine learning project.

Specifically, you learned:

• The types of tests to use in different circumstances, such as normality checking, relationships between variables, and differences between samples.
• The key assumptions for each test and how to interpret the test result.
• How to implement the test using the Python API.

Do you have any questions?

Did I miss an important statistical test or key assumption for one of the listed tests?
Let me know in the comments below.

## Get a Handle on Statistics for Machine Learning!

#### Develop a working understanding of statistics

…by writing lines of code in python

Discover how in my new Ebook:
Statistical Methods for Machine Learning

It provides self-study tutorials on topics like:
Hypothesis Tests, Correlation, Nonparametric Stats, Resampling, and much more…