The Mann-Whitney U test helps us check if two groups are different when we can't assume normal data. It compares their values to see if one tends to be bigger than the other.
Mann-Whitney U test in SciPy
from scipy.stats import mannwhitneyu result = mannwhitneyu(x, y, alternative='two-sided')
x and y are the two groups of data you want to compare.
The alternative parameter can be 'two-sided', 'less', or 'greater' to specify the test direction.
from scipy.stats import mannwhitneyu x = [1, 3, 5] y = [2, 4, 6] result = mannwhitneyu(x, y, alternative='two-sided') print(result)
x tend to be less than those in y.result = mannwhitneyu(x, y, alternative='less') print(result.pvalue)
x tend to be greater than those in y.result = mannwhitneyu(x, y, alternative='greater') print(result.statistic)
This code compares exam scores from two classes to see if their score distributions differ without assuming normality.
from scipy.stats import mannwhitneyu # Two groups of exam scores class_a = [85, 78, 90, 88, 76] class_b = [80, 82, 84, 79, 75] # Perform Mann-Whitney U test result = mannwhitneyu(class_a, class_b, alternative='two-sided') print(f"U statistic: {result.statistic}") print(f"p-value: {result.pvalue:.4f}")
The test works well when data is not normal or when sample sizes are small.
A small p-value (usually below 0.05) means the groups are likely different.
The test compares ranks, not the actual values, so it is less sensitive to outliers.
The Mann-Whitney U test compares two groups without assuming normal data.
It uses ranks to check if one group tends to have higher or lower values.
Use it when data is skewed or ordinal, or when sample sizes are small.
Practice
Mann-Whitney U test in data analysis?Solution
Step 1: Understand the test purpose
The Mann-Whitney U test is a non-parametric test used to compare two independent groups.Step 2: Identify the key feature
It does not assume the data follows a normal distribution, unlike t-tests.Final Answer:
To compare two independent groups without assuming normal distribution -> Option BQuick Check:
Non-parametric comparison = C [OK]
- Confusing it with correlation tests
- Assuming it compares means only
- Using it for more than two groups
Solution
Step 1: Recall scipy.stats module functions
The Mann-Whitney U test function is namedmannwhitneyuand is insidescipy.stats.Step 2: Check correct import syntax
The correct Python import syntax isfrom scipy.stats import mannwhitneyu.Final Answer:
from scipy.stats import mannwhitneyu -> Option DQuick Check:
Correct import syntax = B [OK]
- Using wrong module or function name
- Incorrect import syntax
- Trying to import with 'import ... as' incorrectly
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))
Solution
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.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).Final Answer:
3.0 0.5 -> Option AQuick Check:
U=3.0, p=0.5 from scipy output [OK]
- Mixing up U statistic with p-value
- Rounding incorrectly
- Assuming p-value is always small
from scipy.stats import mannwhitneyu x = [1, 2, 3] y = [4, 5, 6] stat, p = mannwhitneyu(x, y, alternative='greater') print(stat, p)
Solution
Step 1: Check function parameters
Themannwhitneyufunction accepts analternativeparameter with values 'two-sided', 'less', or 'greater'.Step 2: Verify code correctness
Usingalternative='greater'is valid and the function call is correct.Final Answer:
No error; code runs correctly -> Option CQuick Check:
Correct parameter usage = D [OK]
- Using wrong alternative string case
- Trying to use non-existent function names
- Assuming default alternative is 'greater'
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?Solution
Step 1: Define the hypothesis direction
We want to test ifatends to have higher values thanb, so the alternative hypothesis is 'greater'.Step 2: Choose correct function call
The call should bemannwhitneyu(a, b, alternative='greater')to test ifais stochastically greater thanb.Final Answer:
mannwhitneyu(a, b, alternative='greater') -> Option AQuick Check:
Test if a > b = A [OK]
- Swapping sample order and alternative incorrectly
- Using 'less' when testing for greater
- Choosing 'two-sided' when direction matters
Ready to prove it?
Pass the test and this pattern moves unknown
