0
0
SciPydata~20 mins

Spearman correlation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spearman Correlation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Spearman correlation output for simple arrays
What is the output of the following code that calculates Spearman correlation between two arrays?
SciPy
from scipy.stats import spearmanr
x = [1, 2, 3, 4, 5]
y = [5, 6, 7, 8, 7]
correlation, pvalue = spearmanr(x, y)
print(round(correlation, 2))
A1.0
B-0.9
C0.7
D0.9
Attempts:
2 left
💡 Hint
Spearman correlation measures monotonic relationship, not linear.
data_output
intermediate
2:00remaining
Spearman correlation matrix from DataFrame columns
Given a DataFrame with columns A, B, and C, what is the Spearman correlation matrix output?
SciPy
import pandas as pd
from scipy.stats import spearmanr

data = {'A': [1, 2, 3, 4], 'B': [4, 3, 2, 1], 'C': [1, 3, 2, 4]}
df = pd.DataFrame(data)
correlation, _ = spearmanr(df)
print(correlation)
A
[[ 1.  -1.   0.8]
 [-1.   1.  -0.8]
 [ 0.8 -0.8  1. ]]
B
[[ 1.   1.  -0.8]
 [ 1.   1.  -0.8]
 [-0.8 -0.8  1. ]]
C
[[ 1.   0.8 -1. ]
 [ 0.8  1.  -1. ]
 [-1.  -1.   1. ]]
D
[[ 1.   0.5  0.5]
 [ 0.5  1.   0.5]
 [ 0.5  0.5  1. ]]
Attempts:
2 left
💡 Hint
Spearman correlation matrix is symmetric with 1s on diagonal.
🔧 Debug
advanced
2:00remaining
Identify the error in Spearman correlation code
What error does this code raise when trying to calculate Spearman correlation?
SciPy
from scipy.stats import spearmanr
x = [1, 2, 3]
y = [4, 5]
spearmanr(x, y)
AValueError: x and y must have the same length
BTypeError: spearmanr() missing 1 required positional argument
CIndexError: list index out of range
DNo error, returns correlation
Attempts:
2 left
💡 Hint
Check if input arrays have the same length.
🧠 Conceptual
advanced
2:00remaining
Understanding Spearman correlation properties
Which statement about Spearman correlation is TRUE?
AIt is sensitive to outliers more than Pearson correlation.
BIt measures linear relationships only.
CIt uses ranks of data values to measure monotonic relationships.
DIt requires data to be normally distributed.
Attempts:
2 left
💡 Hint
Think about how Spearman correlation handles data values.
🚀 Application
expert
2:00remaining
Interpreting Spearman correlation p-value
You calculate Spearman correlation between two variables and get correlation=0.6 and p-value=0.07. What is the correct interpretation?
AThere is no relationship because p-value is greater than 0.05.
BThere is a moderate monotonic relationship but it is NOT statistically significant at 0.05 level.
CThere is a strong monotonic relationship and it is statistically significant at 0.05 level.
DThe correlation is weak and p-value indicates strong significance.
Attempts:
2 left
💡 Hint
Check both correlation strength and p-value significance.