# Discovering Significant Differences Between Groups Using Kruskal-Wallis Test with Python

## Kruskal-Wallis Test: A Comprehensive Guide

### Definition and Purpose

The Kruskal-Wallis Test is a non-parametric statistical test that can be used to test for statistically significant differences between the medians of two or more independent groups. It’s a non-parametric analog of the One-Way ANOVA test, which is used for normally distributed data. The Kruskal-Wallis Test is used when the data is not normally distributed and cannot be transformed to fit a normal distribution.

The Kruskal-Wallis Test compares the medians of two or more independent groups to determine whether there is a statistically significant difference in the central tendency of the groups. In other words, it determines whether the differences between group medians are significant enough to reject the null hypothesis that the medians are equal.

To perform the Kruskal-Wallis Test, we must have at least three independent groups. Each group must have a sample size greater than 5, and the groups must have different participants.

The primary purpose of the Kruskal-Wallis Test is to determine whether there are significant differences among the groups being compared. If the Kruskal-Wallis Test results are statistically significant, we can conclude that at least one of the groups is significantly different from the others.

### How to Conduct a Kruskal-Wallis Test in Python

When conducting the Kruskal-Wallis Test in Python, the first step is to enter the necessary data. This could be in the form of a CSV file, Excel spreadsheet, or by inputting directly into Python. The data should be formatted as follows:

• Group 1: [data points]
• Group 2: [data points]
• Group 3: [data points]

Once the data is entered, you can then use the `kruskal()` function from the `scipy.stats` library to conduct the Kruskal-Wallis Test.

The `kruskal()` function requires the data to be in the form of each group’s data as separate lists or arrays. The function then returns the test statistic and p-value for the test.

The null hypothesis for the Kruskal-Wallis Test is that the medians of the groups are equal, while the alternative hypothesis is that at least one of the groups has a different median.

If the p-value is less than the significance level (α) (typically 0.05), we can reject the null hypothesis and conclude that there is a statistically significant difference between at least one pair of groups. Interpreting the results of the Kruskal-Wallis Test involves examining both the p-value and the test statistic.

If the p-value is less than α, we reject the null hypothesis and conclude that there is a statistically significant difference between at least one of the groups. If the test statistic is high, it indicates that the differences between the group medians are large, further supporting the conclusion that the groups are significantly different.

### Example of Kruskal-Wallis Test in Python

Suppose we want to compare the effect of three different fertilizers on plant growth. We measured the plant height of ten plants for each fertilizer at the end of the growing period.

#### The data is as follows:

• Group 1: [10, 11, 9, 12, 11, 10, 8, 12, 9, 11]
• Group 2: [15, 13, 16, 14, 12, 13, 14, 15, 13, 15]
• Group 3: [18, 17, 19, 16, 20, 19, 17, 18, 19, 20]

We will now conduct the Kruskal-Wallis Test using Python to determine whether there is a statistically significant difference in plant growth between the three fertilizers. First, we import the necessary libraries:

``````import numpy as np
from scipy.stats import kruskal``````

Then, we input the data into Python:

``````group1 = [10, 11, 9, 12, 11, 10, 8, 12, 9, 11]
group2 = [15, 13, 16, 14, 12, 13, 14, 15, 13, 15]
group3 = [18, 17, 19, 16, 20, 19, 17, 18, 19, 20]``````

Finally, we use the `kruskal()` function to perform the test:

``````stat, p = kruskal(group1, group2, group3)
print("Test Statistic: %.3f, p-value: %.3f" % (stat, p))``````

#### The output will be:

``Test Statistic: 17.836, p-value: 0.000``

The p-value is less than α (0.05), indicating that there is a statistically significant difference in plant growth between the three fertilizers. We can reject the null hypothesis and conclude that at least one of the fertilizers has a different effect on plant growth.

### Conclusion:

In this article, we have explored the Kruskal-Wallis Test, a non-parametric statistical test used to determine whether there are significant differences among two or more independent groups. We described its definition and purpose and provided a step-by-step guide on how to conduct the test using Python.

We also provided an example of a Kruskal-Wallis Test using hypothetical data. By using statistical tests like the Kruskal-Wallis Test, we can gain a better understanding of whether there are significant differences among groups, which is useful in a variety of settings, such as scientific research, business, and healthcare.

To summarize, the Kruskal-Wallis Test is a non-parametric statistical test used to determine whether there are significant differences among two or more independent groups. The test compares the medians of the groups being studied, and if the results are statistically significant, we can conclude that at least one of the groups is significantly different from the others.

Using Python makes performing a Kruskal-Wallis Test efficient and straightforward. Its applicability across scientific and business settings for gaining insight when comparing multiple groups makes understanding the Kruskal-Wallis Test important.

This is especially important when data doesn’t tend to follow a normal distribution or hasn’t been transformed to fit one. Overall, understanding which statistical test to use and how to perform the test is essential for meaningful analysis and actionable conclusions.