Easy way to perform Mann-Whitney U-test with Python.


The Mann-Whitney U-test (usually called U-test) is used to compare differences between two independent data groups which are not normally distributed. The Mann-Whitney U test is often considered the nonparametric alternative to the independent t-test. Scale of data measurements should be ordinal, interval or ratio.
The null hypothesis of the U-test is that the distribution underlying sample x is the same as the distribution underlying sample y.
Mann-Whitney U test is used for every field, but is frequently used in psychology, healthcare, nursing, business, and many other disciplines.



Generating initial data for Mann-Whitney u-test:



import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

# the data (different sample sizes)
N1 = 30
N2 = 35

data1 = np.random.poisson(2,N1)
data2 = np.random.poisson(1,N2)

plt.plot(1+np.random.randn(N1)/10,data1,'ks',markerfacecolor='w')
plt.plot(2+np.random.randn(N2)/10,data2,'ro',markerfacecolor='w')

plt.xlim([0,3])
plt.xticks([1,2],labels=('data1','data2'))
plt.xlabel('Data group')
plt.ylabel('Data value')
plt.show()

Generating initial data for Mann-Whitney u-test

Mann-Whitney u-test using the Python scipy library:



U,p = stats.mannwhitneyu(data1,data2)

print(U,p)

OUT: 320.0 0.0027477016627586097



See also related topics: