Bird
Raised Fist0
SciPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the Mann-Whitney U test in data analysis?
easy
A. To test if a single sample mean equals a known value
B. To compare two independent groups without assuming normal distribution
C. To measure the correlation between two variables
D. To analyze the variance among multiple groups

Solution

  1. Step 1: Understand the test purpose

    The Mann-Whitney U test is a non-parametric test used to compare two independent groups.
  2. Step 2: Identify the key feature

    It does not assume the data follows a normal distribution, unlike t-tests.
  3. Final Answer:

    To compare two independent groups without assuming normal distribution -> Option B
  4. Quick Check:

    Non-parametric comparison = C [OK]
Hint: Remember: Mann-Whitney compares two groups without normality [OK]
Common Mistakes:
  • Confusing it with correlation tests
  • Assuming it compares means only
  • Using it for more than two groups
2. Which of the following is the correct way to import the Mann-Whitney U test function from scipy?
easy
A. import scipy.mannwhitneyu as mw
B. import mannwhitneyu from scipy.stats
C. from scipy import mann_whitney_u_test
D. from scipy.stats import mannwhitneyu

Solution

  1. Step 1: Recall scipy.stats module functions

    The Mann-Whitney U test function is named mannwhitneyu and is inside scipy.stats.
  2. Step 2: Check correct import syntax

    The correct Python import syntax is from scipy.stats import mannwhitneyu.
  3. Final Answer:

    from scipy.stats import mannwhitneyu -> Option D
  4. Quick Check:

    Correct import syntax = B [OK]
Hint: Use 'from scipy.stats import mannwhitneyu' to import [OK]
Common Mistakes:
  • Using wrong module or function name
  • Incorrect import syntax
  • Trying to import with 'import ... as' incorrectly
3. What will be the output of the following code snippet?
from scipy.stats import mannwhitneyu
x = [1, 3, 5]
y = [2, 4, 6]
stat, p = mannwhitneyu(x, y)
print(round(stat, 2), round(p, 3))
medium
A. 3.0 0.5
B. 4.5 0.25
C. 6.0 0.05
D. 0.0 1.0

Solution

  1. Step 1: Understand the data and test

    Lists x and y have values [1,3,5] and [2,4,6]. Mann-Whitney U test compares ranks between these two groups.
  2. Step 2: Calculate Mann-Whitney U statistic and p-value

    Using scipy, the U statistic is 3.0 and p-value is 0.5 (two-sided test by default).
  3. Final Answer:

    3.0 0.5 -> Option A
  4. Quick Check:

    U=3.0, p=0.5 from scipy output [OK]
Hint: Run code to get exact U and p values quickly [OK]
Common Mistakes:
  • Mixing up U statistic with p-value
  • Rounding incorrectly
  • Assuming p-value is always small
4. Identify the error in this Mann-Whitney U test code and select the fix:
from scipy.stats import mannwhitneyu
x = [1, 2, 3]
y = [4, 5, 6]
stat, p = mannwhitneyu(x, y, alternative='greater')
print(stat, p)
medium
A. Replace mannwhitneyu with mann_whitney_u
B. Change 'alternative' to 'two-sided'
C. No error; code runs correctly
D. Use 'alternative="greater"' with capital G

Solution

  1. Step 1: Check function parameters

    The mannwhitneyu function accepts an alternative parameter with values 'two-sided', 'less', or 'greater'.
  2. Step 2: Verify code correctness

    Using alternative='greater' is valid and the function call is correct.
  3. Final Answer:

    No error; code runs correctly -> Option C
  4. Quick Check:

    Correct parameter usage = D [OK]
Hint: Check scipy docs for valid 'alternative' values [OK]
Common Mistakes:
  • Using wrong alternative string case
  • Trying to use non-existent function names
  • Assuming default alternative is 'greater'
5. You have two small samples with skewed data: a = [2, 3, 5, 7] and b = [1, 4, 6, 8]. You want to test if sample a tends to have higher values than b. Which scipy Mann-Whitney U test call is correct?
hard
A. mannwhitneyu(a, b, alternative='greater')
B. mannwhitneyu(a, b, alternative='less')
C. mannwhitneyu(a, b, alternative='two-sided')
D. mannwhitneyu(b, a, alternative='greater')

Solution

  1. Step 1: Define the hypothesis direction

    We want to test if a tends to have higher values than b, so the alternative hypothesis is 'greater'.
  2. Step 2: Choose correct function call

    The call should be mannwhitneyu(a, b, alternative='greater') to test if a is stochastically greater than b.
  3. Final Answer:

    mannwhitneyu(a, b, alternative='greater') -> Option A
  4. Quick Check:

    Test if a > b = A [OK]
Hint: Use 'alternative="greater"' when testing if first sample is larger [OK]
Common Mistakes:
  • Swapping sample order and alternative incorrectly
  • Using 'less' when testing for greater
  • Choosing 'two-sided' when direction matters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard