16.7 C
London
Friday, July 25, 2025
HomePythonANOVA in PythonHow to Perform a Kruskal-Wallis Test in Python

How to Perform a Kruskal-Wallis Test in Python

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories