Mann-Whitney U Test

Performing a Mann-Whitney U test to compare two independent samples with SciPy

Python
import numpy as np
from scipy import stats

# Generate two sample datasets
np.random.seed(42)
group1 = np.random.normal(loc=5, scale=1, size=50)
group2 = np.random.normal(loc=5.5, scale=1, size=50)

# Perform Mann-Whitney U test
statistic, p_value = stats.mannwhitneyu(group1, group2, alternative='two-sided')

print(f"Mann-Whitney U statistic: {statistic:.4f}")
print(f"p-value: {p_value:.4f}")

if p_value < 0.05:
    print("Reject the null hypothesis: There is a significant difference between the two groups.")
else:
    print("Fail to reject the null hypothesis: There is no significant difference between the two groups.")
Click Run or press shift + ENTER to run code.