Bird
Raised Fist0
SciPydata~20 mins

Wilcoxon signed-rank test in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Wilcoxon Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Wilcoxon test on paired samples
What is the output of the following code using scipy's Wilcoxon signed-rank test on paired samples?
SciPy
from scipy.stats import wilcoxon

sample1 = [20, 22, 19, 24, 30]
sample2 = [21, 20, 20, 25, 29]

stat, p = wilcoxon(sample1, sample2)
print(round(stat, 2), round(p, 3))
A7.5 1.000
B3.0 0.438
C2.0 0.688
D0.0 0.063
Attempts:
2 left
💡 Hint
Remember Wilcoxon test compares paired differences and returns a test statistic and p-value.
❓ data_output
intermediate
1:30remaining
Number of zero differences excluded in Wilcoxon test
Given two paired samples, how many zero differences are excluded by the Wilcoxon signed-rank test in the following data?
SciPy
from scipy.stats import wilcoxon

x = [5, 7, 8, 6, 9]
y = [5, 6, 8, 7, 9]

stat, p = wilcoxon(x, y, zero_method='wilcox')
print(stat)
A3
B0
C1
D2
Attempts:
2 left
💡 Hint
Check how many pairs have zero difference between x and y.
🔧 Debug
advanced
1:30remaining
Identify the error in Wilcoxon test usage
What error will this code raise when running the Wilcoxon signed-rank test?
SciPy
from scipy.stats import wilcoxon

sample1 = [1, 2, 3]
sample2 = [1, 2]

stat, p = wilcoxon(sample1, sample2)
print(stat, p)
ANo error, outputs test statistic and p-value
BTypeError: wilcoxon() missing 1 required positional argument
CIndexError: list index out of range
DValueError: The samples must have the same length
Attempts:
2 left
💡 Hint
Check if the input arrays have the same length.
🚀 Application
advanced
1:30remaining
Choosing correct zero_method for Wilcoxon test
You have paired data with some zero differences. Which zero_method option in scipy.stats.wilcoxon excludes zero differences from ranking but keeps the sample size unchanged?
A'exclude'
B'wilcox'
C'pratt'
D'zsplit'
Attempts:
2 left
💡 Hint
Check scipy documentation for zero_method options.
🧠 Conceptual
expert
1:30remaining
Interpretation of Wilcoxon signed-rank test p-value
If the Wilcoxon signed-rank test returns a p-value of 0.02 for paired samples, what does this imply about the null hypothesis at a 5% significance level?
AThere is no evidence to reject the null hypothesis; the samples are identical.
BThere is strong evidence to reject the null hypothesis; the median difference is not zero.
CThe test is inconclusive; more data is needed.
DThe null hypothesis is accepted; the median difference is zero.
Attempts:
2 left
💡 Hint
Recall what a p-value less than significance level means.

Practice

(1/5)
1.

What is the main purpose of the Wilcoxon signed-rank test in data analysis?

easy
A. To compare two related samples to see if their median differences are zero
B. To measure the correlation between two variables
C. To test if a sample comes from a normal distribution
D. To compare means of two independent groups

Solution

  1. 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.
  2. Step 2: Identify what it tests

    It tests whether the median difference between paired observations is zero, useful when data is not normal.
  3. Final Answer:

    To compare two related samples to see if their median differences are zero -> Option A
  4. Quick Check:

    Wilcoxon tests median difference = To compare two related samples to see if their median differences are zero [OK]
Hint: Wilcoxon tests paired median differences, not means or correlation [OK]
Common Mistakes:
  • Confusing it with correlation tests
  • Using it for independent samples
  • Thinking it tests means instead of medians
2.

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?
easy
A. scipy.wilcoxon(x, y)
B. stats.Wilcoxon(x, y)
C. stats.wilcox(x, y)
D. stats.wilcoxon(x, y)

Solution

  1. Step 1: Recall the function name and module

    The Wilcoxon signed-rank test function is wilcoxon in scipy.stats.
  2. Step 2: Check correct syntax

    Calling stats.wilcoxon(x, y) is correct; capitalization and module usage matter.
  3. Final Answer:

    stats.wilcoxon(x, y) -> Option D
  4. Quick Check:

    Correct function name and module = stats.wilcoxon(x, y) [OK]
Hint: Function is lowercase 'wilcoxon' in scipy.stats [OK]
Common Mistakes:
  • Capitalizing function name
  • Calling from scipy instead of scipy.stats
  • Using wrong function names like wilcox
3.

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))
medium
A. 3.0 0.250
B. 1.0 0.500
C. 0.0 0.125
D. 2.0 0.750

Solution

  1. Step 1: Calculate differences and ranks

    Differences: -1, 0, 1, -1; ignoring zero, absolute differences: 1, 1, 1; ranks (ties averaged): 2, 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.
  3. Step 3: Verify output

    The actual output from running the code is 1.0 0.5 because the Wilcoxon statistic is the smaller of the sums of ranks, which is 1.0, and the p-value is 0.5.
  4. Final Answer:

    1.0 0.500 -> Option B
  5. Quick Check:

    Wilcoxon stat and p-value match 1.0 0.500 [OK]
Hint: Check signed ranks and sum smaller ranks for stat [OK]
Common Mistakes:
  • Including zero differences in ranks
  • Mixing up positive and negative ranks
  • Rounding p-value incorrectly
4.

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)
medium
A. Lists x and y have different lengths causing an error
B. Wilcoxon test requires numpy arrays, not lists
C. Wilcoxon test cannot be used with integer data
D. Missing import for numpy

Solution

  1. Step 1: Check input data lengths

    Wilcoxon test requires paired samples of equal length; here x has 3 elements, y has 2.
  2. Step 2: Identify error cause

    Different lengths cause a ValueError when running the test.
  3. Final Answer:

    Lists x and y have different lengths causing an error -> Option A
  4. Quick Check:

    Equal length inputs required = Lists x and y have different lengths causing an error [OK]
Hint: Paired samples must have same length for Wilcoxon [OK]
Common Mistakes:
  • Assuming lists are invalid input
  • Ignoring length mismatch
  • Thinking data type must be float
5.

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?

hard
A. Paired t-test
B. Independent t-test
C. Wilcoxon signed-rank test
D. Chi-square test

Solution

  1. Step 1: Identify data type and test conditions

    Data is paired (before and after) and not normally distributed.
  2. Step 2: Choose appropriate test

    Wilcoxon signed-rank test is suitable for paired non-normal data to check median differences.
  3. Final Answer:

    Wilcoxon signed-rank test -> Option C
  4. Quick Check:

    Non-normal paired data = Wilcoxon test Wilcoxon signed-rank test [OK]
Hint: Use Wilcoxon for paired non-normal data changes [OK]
Common Mistakes:
  • Using paired t-test despite non-normality
  • Choosing independent t-test for paired data
  • Using chi-square for continuous data
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