Mann–Whitney U Test

Maths: Statistics for machine learning

3 min read

Published Oct 22 2025, updated Oct 23 2025


40
0
0
0

Machine LearningMathsNumPyPandasPythonStatistics

The Mann–Whitney U Test is a non-parametric statistical test used to compare two independent groups to determine whether their distributions differ — typically in terms of central tendency (median).

It’s an alternative to the independent samples t-test, but unlike the t-test, it does not assume normality of data.


In simple terms:

“The Mann–Whitney test checks if one group tends to have higher or lower values than another, without assuming the data are normally distributed.”




When to Use It

  • Two groups - Independent (not paired) samples
  • Data type - Ordinal or continuous (but not necessarily normal)
  • Goal - Compare central tendency (medians) between groups
  • Not for paired data - For paired data, use the Wilcoxon signed-rank test instead



Example Question

“Do customers from Region A spend more on average than customers from Region B?”

If the spending data are not normally distributed (e.g., skewed or contain outliers), the Mann–Whitney U test is the better choice than a t-test.




How It Works

Instead of comparing means directly, the Mann–Whitney test:

  1. Combines all data from both groups
  2. Ranks the data from smallest to largest (1 = smallest)
  3. Calculates the sum of ranks for each group
  4. Computes a U statistic — the number of times observations in one group precede observations in the other
  5. Uses that U value to test whether the groups’ rank distributions are significantly different

Because it uses ranks, it’s robust to non-normal distributions and outliers.




Hypotheses

  • H₀ (Null Hypothesis) - The two groups come from identical distributions (no difference in medians)
  • H₁ (Alternative Hypothesis) - The two groups come from different distributions (one tends to be higher/lower)



Test Statistic

For samples of size n1​ and n2:

Mann–Whitney formula

Where R1​ = sum of ranks for group 1.
The smaller of U1​ and U2 is used as the test statistic.

For large samples (n, n > 20), U is approximately normally distributed, so we can convert it to a Z-score.




Example in Python

Let’s say you want to test whether two marketing campaigns lead to different spending amounts.

import numpy as np
from scipy.stats import mannwhitneyu

# Example data (non-normal)
campaign_A = np.array([45, 50, 52, 60, 61, 62, 65, 70, 72])
campaign_B = np.array([40, 42, 48, 51, 55, 57, 59, 61, 63])

# Perform Mann–Whitney U Test
stat, p = mannwhitneyu(campaign_A, campaign_B, alternative='two-sided')

print(f"Mann–Whitney U Statistic: {stat:.3f}")
print(f"P-value: {p:.3f}")

Interpretation:

  • p < 0.05 → reject H₀ → significant difference between groups
  • p ≥ 0.05 → fail to reject H₀ → no significant difference



Advantages

  • Doesn’t assume normality
  • Can handle ordinal data (ranks, ratings, etc.)
  • Robust to outliers
  • Works with unequal sample sizes



Limitations

  • Assumes distributions have the same shape (only medians differ)
  • Less powerful than t-test when data are normal
  • Doesn’t tell you by how much the groups differ — only that they do



Python code

import pingouin as pg
import numpy as np
import pandas as pd

# Example: Two groups with non-normal distributions
np.random.seed(42)
df = pd.DataFrame({
    'Col1': np.random.exponential(scale=2, size=30),
    'Col2': np.random.exponential(scale=3, size=30)
})

# Run Mann–Whitney U test
results = pg.mwu(x=df['Col1'], y=df['Col2'], alternative='two-sided')
print(results)

Output:

     U-val alternative p-val RBC CLES
MWU 335.0 two-sided 0.09049 -0.255556 0.372222

Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact