# Nonparametric Statistical Tests using Python: An Introductory Tutorial

1. Mann-Whitney U Test,
2. Wilcoxon Signed-Rank Test,
3. Kruskal-Wallis H Test and
4. Friedman Test.

# Mann-Whitney U Test

• observations in each sample are independent and identically distributed (iid),
• observations in each sample can be ranked.
• H0: the distributions of both samples are equal,
• H1: the distributions of both samples are not equal.
`from numpy.random import seedfrom numpy.random import randn# seed the random number generatorseed(1)# generate two independent samplesdata1 = 15*randn(50)+30data2 = 15*randn(50)+31`
`# Mann-Whitney U test from scipy.stats import mannwhitneyu # compare samplesstat, p = mannwhitneyu(data1, data2) # print the test statistic and p-valueprint(‘stat: ‘,stat, ‘\np-value: ‘,p)`
`stat: 1062.0 p-value: 0.19615247557393267`
`# interpretalpha = 0.05 # 5% significance level if p > alpha:   print(‘Fails to reject H0, i.e., same distribution’)else:   print(‘Rejects H0, i.e., different distribution’) `
`Fails to reject H0, i.e., same distribution`

# Wilcoxon Signed-Rank Test

• observations in each sample are independent and identically distributed (iid),
• observations in each sample can be ranked,
• observations across each sample are paired.
• H0: the distributions of both samples are equal,
• H1: the distributions of both samples are not equal.
`# Wilcoxon Signed-Rank Testfrom scipy.stats import wilcoxon # compare samples stat, p = wilcoxon(data1, data2)# print the test statistic and p-valueprint(‘stat: ‘,stat, ‘\np-value: ‘,p)`
`stat:  506.0 p-value:  0.20429624539024516 `
`# interpretalpha = 0.05 # 5% significance level if p > alpha:   print(‘Fails to reject H0, i.e., same distribution’)else:   print(‘Rejects H0, i.e., different distribution’) `
`Fails to reject H0, i.e., same distribution`

# Kruskal-Wallis H Test

• observations in each sample are independent and identically distributed (iid),
• observations in each sample can be ranked.
• H0: the distributions of all samples are equal,
• H1: the distributions of one or more samples are not equal.
`# Kruskal-Wallis H Testfrom scipy.stats import kruskal # compare samples stat, p = kruskal(data1, data2)# print the test statistic and p-valueprint(‘stat: ‘,stat, ‘\np-value: ‘,p)`
`stat:  1.6797148514851301 p-value:  0.1949623463490322`
`# interpretalpha = 0.05 # 5% significance level if p > alpha:   print(‘Fails to reject H0, i.e., same distribution’)else:   print(‘Rejects H0, i.e., different distribution’)`
`Fails to reject H0, i.e., same distribution `

# Friedman Test

• observations in each sample are independent and identically distributed (iid),
• observations in each sample can be ranked,
• observations across each sample are paired.
• H0: the distributions of all samples are equal,
• H1: the distributions of one or more samples are not equal.
`from numpy.random import seedfrom numpy.random import randn# seed the random number generatorseed(1)# generate two independent samplesdata1 = 15*randn(50)+30data2 = 15*randn(50)+30.50data3 = 15*randn(50)+31`
`# Friedman Test from scipy.stats import friedmanchisquare# compare samples stat, p = friedmanchisquare(data1, data2, data3)# print the test statistic and p-valueprint(‘stat: ‘,stat, ‘\np-value: ‘,p)`
`stat:  1.5600000000000591 p-value:  0.45840601130520997`
`# interpretalpha = 0.05 # 5% significance level if p > alpha:   print(‘Fails to reject H0, i.e., same distribution’)else:   print(‘Rejects H0, i.e., different distribution’)`
`Fails to reject H0, i.e., same distribution`

--

--

## More from Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.

Love podcasts or audiobooks? Learn on the go with our new app.

## Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.