Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.mannwhitneyu

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.

How it works

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.

Syntax

scipy.stats.mannwhitneyu(x, y, use_continuity=True, alternative='two-sided', axis=0, method='auto', *, nan_policy='propagate', keepdims=False)

Parameters

NameTypeDescription
xarray-likeFirst sample; array-like input.
yarray-likeSecond sample; array-like input.
use_continuitybool, optionalApply continuity correction when using the asymptotic method.
alternative{'less', 'two-sided', 'greater'}, optionalDefines the alternative hypothesis: 'less', 'two-sided', or 'greater'.
axisint or None, optionalAxis along which to perform the test.
method{'auto', 'asymptotic', 'exact'}, optionalP-value calculation method: 'auto', 'asymptotic', or 'exact'.
nan_policy{'propagate', 'omit', 'raise'}, optionalHow to handle NaN values: 'propagate', 'omit', or 'raise'.
keepdimsbool, optionalWhether reduced axes are retained in the output shape.

Returns

A result object containing statistic, the Mann–Whitney U statistic, and pvalue, the p-value for the chosen alternative.

Key points

  • Use this function when comparing two independent samples with a rank-based, nonparametric test.
  • The null hypothesis states that the distributions underlying the two samples are the same.
  • It can be used to assess a location difference between distributions without assuming a parametric form.
  • The method parameter controls whether the p-value uses exact or asymptotic computation.
  • The asymptotic method uses a normal approximation and adjusts for ties.
  • Auto mode chooses the exact method for a small, tie-free sample; otherwise it chooses the asymptotic method.

Common mistakes

  • The exact method does not correct for tied observations; use the asymptotic method when tie handling matters.
  • For multidimensional inputs, shapes must broadcast in every dimension except the selected axis.

Example: Compare two independent samples

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.

When to use it

Independent-group comparison

Comparing measurements from two unrelated groups without assuming normality.

Directional rank hypothesis

Testing whether one sample tends to have larger or smaller values than another.

Vectorized testing

Analyzing batched or multidimensional samples along a selected dimension.

Interview questions

QWhat does mannwhitneyu test?

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.

QHow does alternative affect the result?

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.

QHow is the p-value method selected?

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.ranksums
  • scipy.stats.wilcoxon
  • scipy.stats.mstats.mannwhitneyu

Source: mannwhitneyu. Checked 2026-10-15.