0
0
SciPydata~5 mins

Mann-Whitney U test in SciPy

Choose your learning style9 modes available
Introduction

The Mann-Whitney U test helps us check if two groups are different when we can't assume normal data. It compares their values to see if one tends to be bigger than the other.

Comparing test scores of two different classes when scores are not normally distributed.
Checking if two medicines have different effects based on patient recovery times that are skewed.
Comparing customer satisfaction ratings between two stores when ratings are ordinal.
Testing if two machines produce different quality levels when data is not symmetric.
Syntax
SciPy
from scipy.stats import mannwhitneyu

result = mannwhitneyu(x, y, alternative='two-sided')

x and y are the two groups of data you want to compare.

The alternative parameter can be 'two-sided', 'less', or 'greater' to specify the test direction.

Examples
Basic test comparing two small lists with a two-sided hypothesis.
SciPy
from scipy.stats import mannwhitneyu

x = [1, 3, 5]
y = [2, 4, 6]
result = mannwhitneyu(x, y, alternative='two-sided')
print(result)
Tests if values in x tend to be less than those in y.
SciPy
result = mannwhitneyu(x, y, alternative='less')
print(result.pvalue)
Tests if values in x tend to be greater than those in y.
SciPy
result = mannwhitneyu(x, y, alternative='greater')
print(result.statistic)
Sample Program

This code compares exam scores from two classes to see if their score distributions differ without assuming normality.

SciPy
from scipy.stats import mannwhitneyu

# Two groups of exam scores
class_a = [85, 78, 90, 88, 76]
class_b = [80, 82, 84, 79, 75]

# Perform Mann-Whitney U test
result = mannwhitneyu(class_a, class_b, alternative='two-sided')

print(f"U statistic: {result.statistic}")
print(f"p-value: {result.pvalue:.4f}")
OutputSuccess
Important Notes

The test works well when data is not normal or when sample sizes are small.

A small p-value (usually below 0.05) means the groups are likely different.

The test compares ranks, not the actual values, so it is less sensitive to outliers.

Summary

The Mann-Whitney U test compares two groups without assuming normal data.

It uses ranks to check if one group tends to have higher or lower values.

Use it when data is skewed or ordinal, or when sample sizes are small.