Independent-group comparison
Comparing measurements from two unrelated groups without assuming normality.
SciPy · Function
Performs the Mann–Whitney U rank test on two independent samples. It tests whether the distributions underlying the samples are the same and returns a statistic with a p-value.
The function ranks two independent samples and evaluates whether their underlying distributions differ. It returns a statistic and p-value, selecting an exact calculation for sufficiently small tie-free samples and an asymptotic calculation otherwise when method is set to auto.
scipy.stats.mannwhitneyu(x, y, use_continuity=True, alternative='two-sided', axis=0, method='auto', *, nan_policy='propagate', keepdims=False)| Name | Type | Description |
|---|---|---|
x | array-like | First sample; array-like input. |
y | array-like | Second sample; array-like input. |
use_continuity | bool, optional | Apply continuity correction when using the asymptotic method. |
alternative | {'less', 'two-sided', 'greater'}, optional | Defines the alternative hypothesis: 'less', 'two-sided', or 'greater'. |
axis | int or None, optional | Axis along which to perform the test. |
method | {'auto', 'asymptotic', 'exact'}, optional | P-value calculation method: 'auto', 'asymptotic', or 'exact'. |
nan_policy | {'propagate', 'omit', 'raise'}, optional | How to handle NaN values: 'propagate', 'omit', or 'raise'. |
keepdims | bool, optional | Whether reduced axes are retained in the output shape. |
A result object containing statistic, the Mann–Whitney U statistic, and pvalue, the p-value for the chosen alternative.
import numpy as np
from scipy.stats import mannwhitneyu
before = np.array([1, 2, 3, 4, 5])
after = np.array([3, 4, 5, 6, 7])
result = mannwhitneyu(before, after, alternative="two-sided")
print(result.statistic, result.pvalue)Computes a two-sided Mann–Whitney U test and prints its statistic and p-value.
Comparing measurements from two unrelated groups without assuming normality.
Testing whether one sample tends to have larger or smaller values than another.
Analyzing batched or multidimensional samples along a selected dimension.
It is a nonparametric test for two independent samples. Its null hypothesis is that the distributions underlying x and y are the same.
Follow-up: When is the test often interpreted as a location comparison? The documentation notes it is often used to test a difference in location between distributions.
The alternative selects the hypothesis direction: less, greater, or two-sided. The default is two-sided, so the returned p-value assesses a difference in either direction.
Follow-up: What should be chosen for a directional hypothesis? Select less or greater according to the specified ordering of the two samples.
With method='auto', SciPy uses exact computation when one sample has fewer than eight observations and there are no ties. Otherwise it uses the asymptotic method, which corrects for ties.
Follow-up: What is the limitation of method='exact'? It computes the exact null distribution but makes no correction for ties.
scipy.stats.ranksumsscipy.stats.wilcoxonscipy.stats.mstats.mannwhitneyu