0
0
SciPydata~10 mins

Mann-Whitney U test in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mann-Whitney U test
Start with two independent samples
Rank all values together
Calculate U statistic for each sample
Compare U to distribution or calculate p-value
Decide if samples differ significantly
End
The test ranks combined data, calculates U statistics, then finds the p-value to check if two groups differ.
Execution Sample
SciPy
from scipy.stats import mannwhitneyu

sample1 = [5, 7, 8, 9]
sample2 = [1, 2, 3, 4]

stat, p = mannwhitneyu(sample1, sample2, alternative='two-sided')
print(stat, p)
This code runs the Mann-Whitney U test on two small samples and prints the U statistic and p-value.
Execution Table
StepActionDetailsResult
1Combine samplesSamples: [5,7,8,9] and [1,2,3,4]Combined: [1,2,3,4,5,7,8,9]
2Rank combined dataAssign ranks to combined valuesRanks: [1,2,3,4,5,6,7,8]
3Sum ranks for sample1Ranks for sample1 values (5,7,8,9)Sum ranks = 5+6+7+8 = 26
4Sum ranks for sample2Ranks for sample2 values (1,2,3,4)Sum ranks = 1+2+3+4 = 10
5Calculate U for sample1U1 = n1*n2 + n1(n1+1)/2 - R1U1 = 4*4 + 4*5/2 - 26 = 6
6Calculate U for sample2U2 = n1*n2 - U1U2 = 16 - 6 = 10
7Select smaller UU = min(U1, U2)U = 6
8Calculate p-valueUsing U and sample sizesp ≈ 0.0286
9Interpret resultp < 0.05 means significant differenceReject null hypothesis
10EndTest completeOutput: U=6, p=0.0286
💡 Test ends after calculating p-value and interpreting significance.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
sample1[5,7,8,9][5,7,8,9][5,7,8,9][5,7,8,9][5,7,8,9]
sample2[1,2,3,4][1,2,3,4][1,2,3,4][1,2,3,4][1,2,3,4]
combined[][1,2,3,4,5,7,8,9][1,2,3,4,5,7,8,9][1,2,3,4,5,7,8,9][1,2,3,4,5,7,8,9]
ranks[][1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8][1,2,3,4,5,6,7,8]
R1 (sum ranks sample1)026262626
U100666
U20001010
U00066
p-valueNoneNoneNoneNone0.0286
Key Moments - 3 Insights
Why do we rank all values together instead of comparing raw values?
Ranking all values together (see Step 2 in execution_table) allows the test to compare relative positions without assuming normal distribution.
Why do we pick the smaller U value for the test?
The Mann-Whitney U test uses the smaller U (Step 7) because it reflects the difference between groups more conservatively.
What does a small p-value mean in this test?
A small p-value (Step 8) means the two samples likely come from different groups, so we reject the idea they are the same.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the sum of ranks for sample1?
A10
B6
C26
D16
💡 Hint
Check the 'Result' column in Step 3 of execution_table.
At which step does the test decide which U value to use?
AStep 7
BStep 6
CStep 5
DStep 8
💡 Hint
Look for 'Select smaller U' in the Action column.
If sample1 had all values smaller than sample2, how would the U statistic change?
AU would be unchanged
BU would be zero
CU would be maximum possible
DU would be negative
💡 Hint
Recall U measures how many times values in one sample exceed the other; all smaller means minimum U.
Concept Snapshot
Mann-Whitney U test compares two independent samples.
It ranks combined data and calculates U statistics.
Uses smaller U to find p-value.
No assumption of normal distribution.
Small p-value means groups differ significantly.
Full Transcript
The Mann-Whitney U test compares two independent groups by ranking all their values together. It sums ranks for each group and calculates U statistics. The smaller U is used to find a p-value, which tells us if the groups differ significantly. This test does not assume normal distribution, making it useful for non-normal data. A small p-value means we reject the idea that the groups are the same.