*59*

AÂ **Mann-Whitney U test** is used to compare the differences between two samples when the sample distributions are not normally distributed and the sample sizes are small (n

It is considered to be the nonparametric equivalent to theÂ **two sample t-test**.

This tutorial explains how to conduct a Mann-Whitney U test in Python.

**Example: Mann-Whitney U Test in Python**

Researchers want to know if a fuel treatment leads to a change in the average mpg of a car. To test this, they measure the mpg of 12 cars with the fuel treatment and 12 cars without it.

Since the sample sizes are small and the researchers suspect that the sample distributions are not normally distributed, they decided to perform a Mann-Whitney U test to determine if there is a statistically significant difference in mpg between the two groups.

Perform the following steps to conduct a Mann-Whitney U test in Python.

**Step 1: Create the data.**

First, weâ€™ll create two arrays to hold the mpg values for each group of cars:

group1 = [20, 23, 21, 25, 18, 17, 18, 24, 20, 24, 23, 19]group2 = [24, 25, 21, 22, 23, 18, 17, 28, 24, 27, 21, 23]

**Step 2: Conduct a Mann-Whitney U Test.**

Next, weâ€™ll use the mannwhitneyu() function from the scipy.stats library to conduct a Mann-Whitney U test, which uses the following syntax:

**mannwhitneyu(x, y, use_continuity=True, alternative=None)**

where:

**x:Â**an array of sample observations from group 1**y:Â**an array of sample observations from group 2**use_continuity:Â**whether a continuity correction (1/2) should be taken into account. Default is True.**alternative:Â**defines the alternative hypothesis. Default is â€˜Noneâ€™ which computes a p-value half the size of the â€˜two-sidedâ€™ p-value. Other options include â€˜two-sidedâ€™, â€˜lessâ€™, and â€˜greater.â€™

Hereâ€™s how to use this function in our specific example:

import scipy.stats as stats #perform the Mann-Whitney U test stats.mannwhitneyu(group1, group2, alternative='two-sided') (statistic=50.0, pvalue=0.2114)

The test statistic is **50.0Â **and the corresponding two-sided p-value isÂ **0.2114**.

**Step 3: Interpret the results.**

In this example, the Mann-Whitney U Test uses the following null and alternative hypotheses:

**H _{0}:Â **The mpg is equal between the two groups

**H _{A}:Â **The mpg isÂ

*notÂ*equal between the two groups

Since the p-value (**0.2114**) is not less than 0.05, we fail to reject the null hypothesis.

This means we do not have sufficient evidence to say that the true mean mpg is different between the two groups.

**Additional Resources**

The following tutorials explain how to perform a Mann-Whitney U Test in different statistical software:

How to Perform a Mann-Whitney U Test in Excel

How to Perform a Mann-Whitney U Test in R

How to Perform a Mann-Whitney U Test in SPSS