How can a Kruskal-Wallis test be performed using Python?

The Kruskal-Wallis test is a non-parametric statistical test used to compare the medians of two or more independent samples. In Python, this test can be performed by using the “kruskal” function from the “scipy.stats” module.

First, the data for each sample must be organized into separate lists or arrays. Then, the “kruskal” function can be called, passing in the sample data as arguments. This will return the test statistic and p-value.

To interpret the results, the p-value should be compared to a chosen significance level. If the p-value is less than the significance level, it can be concluded that there is a significant difference between the medians of the samples. If the p-value is greater than the significance level, there is not enough evidence to reject the null hypothesis of equal medians.

In summary, the Kruskal-Wallis test can be performed using Python by organizing the data into separate lists or arrays and using the “kruskal” function from the “scipy.stats” module to obtain the test statistic and p-value, allowing for the comparison of medians between multiple independent samples.

Perform a Kruskal-Wallis Test in Python


A 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 .

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 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.

x