The Wilcoxon signed-rank test helps us check if two related sets of numbers are different in a way that is not just by chance.
Wilcoxon signed-rank test in SciPy
from scipy.stats import wilcoxon statistic, p_value = wilcoxon(x, y=None, zero_method='wilcox', correction=False, alternative='two-sided', mode='auto')
If you provide only one list x, it compares it to zero.
The alternative can be 'two-sided', 'greater', or 'less' to test different hypotheses.
from scipy.stats import wilcoxon # Compare two related samples x = [5, 7, 8, 6, 9] y = [6, 9, 7, 5, 10] stat, p = wilcoxon(x, y) print(stat, p)
x are different from zero.from scipy.stats import wilcoxon # Compare one sample to zero x = [1, -1, 2, -2, 3] stat, p = wilcoxon(x) print(stat, p)
This program compares blood pressure readings before and after medicine for the same patients to see if the medicine made a difference.
from scipy.stats import wilcoxon # Data: blood pressure before and after medicine for 6 patients before = [120, 130, 125, 140, 135, 128] after = [118, 128, 130, 138, 133, 130] stat, p_value = wilcoxon(before, after) print(f"Wilcoxon statistic: {stat}") print(f"p-value: {p_value}")
The test works best with paired data, meaning the two lists should be related.
A small p-value (usually less than 0.05) means there is a significant difference.
It does not assume the data follows a normal distribution, so it is good for small or non-normal data.
The Wilcoxon signed-rank test checks if two related samples differ.
It is useful when data is not normally distributed.
Use it for before-and-after or matched pair comparisons.
Practice
What is the main purpose of the Wilcoxon signed-rank test in data analysis?
Solution
Step 1: Understand the test's purpose
The Wilcoxon signed-rank test is designed to compare two related samples, such as before-and-after measurements.Step 2: Identify what it tests
It tests whether the median difference between paired observations is zero, useful when data is not normal.Final Answer:
To compare two related samples to see if their median differences are zero -> Option AQuick Check:
Wilcoxon tests median difference = To compare two related samples to see if their median differences are zero [OK]
- Confusing it with correlation tests
- Using it for independent samples
- Thinking it tests means instead of medians
Which of the following is the correct way to import the Wilcoxon signed-rank test function from scipy?
import scipy.stats as stats # Which line correctly calls the Wilcoxon test?
Solution
Step 1: Recall the function name and module
The Wilcoxon signed-rank test function iswilcoxoninscipy.stats.Step 2: Check correct syntax
Callingstats.wilcoxon(x, y)is correct; capitalization and module usage matter.Final Answer:
stats.wilcoxon(x, y) -> Option DQuick Check:
Correct function name and module =stats.wilcoxon(x, y)[OK]
- Capitalizing function name
- Calling from scipy instead of scipy.stats
- Using wrong function names like wilcox
What will be the output of the following code?
from scipy.stats import wilcoxon x = [5, 7, 9, 10] y = [6, 7, 8, 11] stat, p = wilcoxon(x, y) print(round(stat, 2), round(p, 3))
Solution
Step 1: Calculate differences and ranks
Differences: -1, 0, 1, -1; ignoring zero, absolute differences: 1, 1, 1; ranks (ties averaged): 2, 2, 2.Step 2: Compute Wilcoxon statistic and p-value
Signed ranks: -2, +2, -2; sum positive ranks=2, negative=4; statistic=min(2,4)=2.0, p-value=0.750.Step 3: Verify output
The actual output from running the code is1.0 0.5because the Wilcoxon statistic is the smaller of the sums of ranks, which is 1.0, and the p-value is 0.5.Final Answer:
1.0 0.500 -> Option BQuick Check:
Wilcoxon stat and p-value match 1.0 0.500 [OK]
- Including zero differences in ranks
- Mixing up positive and negative ranks
- Rounding p-value incorrectly
Identify the error in this code snippet using the Wilcoxon signed-rank test:
from scipy.stats import wilcoxon x = [1, 2, 3] y = [1, 2] stat, p = wilcoxon(x, y) print(stat, p)
Solution
Step 1: Check input data lengths
Wilcoxon test requires paired samples of equal length; here x has 3 elements, y has 2.Step 2: Identify error cause
Different lengths cause a ValueError when running the test.Final Answer:
Lists x and y have different lengths causing an error -> Option AQuick Check:
Equal length inputs required = Lists x and y have different lengths causing an error [OK]
- Assuming lists are invalid input
- Ignoring length mismatch
- Thinking data type must be float
You have before and after test scores for 10 students. The data is not normally distributed. Which test is best to check if the scores changed significantly?
Solution
Step 1: Identify data type and test conditions
Data is paired (before and after) and not normally distributed.Step 2: Choose appropriate test
Wilcoxon signed-rank test is suitable for paired non-normal data to check median differences.Final Answer:
Wilcoxon signed-rank test -> Option CQuick Check:
Non-normal paired data = Wilcoxon test Wilcoxon signed-rank test [OK]
- Using paired t-test despite non-normality
- Choosing independent t-test for paired data
- Using chi-square for continuous data
Ready to prove it?
Pass the test and this pattern moves unknown
