How to Perform a Kruskal-Wallis Test in Python


A Kruskal-Wallis Test is used to determine whether or not there is a statistically significant difference between the medians of three or more independent groups. It is considered to be the non-parametric equivalent of the One-Way ANOVA.

This tutorial explains how to conduct a Kruskal-Wallis Test in Python.

Example: Kruskal-Wallis Test in Python

Researchers want to know if three different fertilizers lead to different levels of plant growth. They randomly select 30 different plants and split them into three groups of 10, applying a different fertilizer to each group. At the end of one month they measure the height of each plant.

Use the following steps to perform a Kruskal-Wallis Test to determine if the median growth is the same across the three groups.

Step 1: Enter the data.

First, we’ll create three arrays to hold our our plant measurements for each of the three groups:

group1 = [7, 14, 14, 13, 12, 9, 6, 14, 12, 8]
group2 = [15, 17, 13, 15, 15, 13, 9, 12, 10, 8]
group3 = [6, 8, 8, 9, 5, 14, 13, 8, 10, 9]

Step 2: Perform the Kruskal-Wallis Test.

Next, we’ll perform a Kruskal-Wallis Test using the kruskal() function from the scipy.stats library:

from scipy import stats

#perform Kruskal-Wallis Test 
stats.kruskal(group1, group2, group3)

(statistic=6.2878, pvalue=0.0431)

Step 3: Interpret the results.

The Kruskal-Wallis Test uses the following null and alternative hypotheses:

The null hypothesis (H0): The median is equal across all groups.

The alternative hypothesis: (Ha): The median is not equal across all groups.

In this case, the test statistic is 6.2878 and the corresponding p-value is 0.0431. Since this p-value is less than 0.05, we can reject the null hypothesis that the median plant growth is the same for all three fertilizers. We have sufficient evidence to conclude that the type of fertilizer used leads to statistically significant differences in plant growth.

Leave a Reply

Your email address will not be published. Required fields are marked *