Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Correlation analysis (Pearson, Spearman) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Pearson correlation calculation
What is the output of this Python code calculating Pearson correlation between two lists?
Data Analysis Python
import numpy as np
from scipy.stats import pearsonr
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
correlation, p_value = pearsonr(x, y)
print(round(correlation, 2))
A0.50
B0.80
C1.00
D-1.00
Attempts:
2 left
💡 Hint
Pearson correlation measures linear relationship strength.
❓ data_output
intermediate
2:00remaining
Spearman correlation output for rank data
Given these two lists, what is the Spearman correlation coefficient output?
Data Analysis Python
from scipy.stats import spearmanr
x = [10, 20, 30, 40, 50]
y = [2, 1, 3, 5, 4]
correlation, _ = spearmanr(x, y)
print(round(correlation, 2))
A0.70
B0.60
C0.90
D0.80
Attempts:
2 left
💡 Hint
Spearman correlation measures monotonic relationship using ranks.
🧠 Conceptual
advanced
2:00remaining
Difference between Pearson and Spearman correlation
Which statement correctly describes the difference between Pearson and Spearman correlation?
APearson measures monotonic relationships; Spearman measures linear relationships.
BPearson measures linear relationships; Spearman measures monotonic relationships using ranks.
CBoth measure only linear relationships but use different formulas.
DSpearman correlation is only used for categorical data.
Attempts:
2 left
💡 Hint
Think about how each correlation handles data ordering and linearity.
🔧 Debug
advanced
2:00remaining
Identify the error in correlation calculation code
What error will this code raise when calculating Pearson correlation?
Data Analysis Python
from scipy.stats import pearsonr
x = [1, 2, 3]
y = [4, 5]
correlation, p = pearsonr(x, y)
AValueError: x and y must have the same length
BTypeError: unsupported operand type(s)
CIndexError: list index out of range
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
Check if the input lists have the same number of elements.
🚀 Application
expert
2:00remaining
Choosing correlation method for non-linear but monotonic data
You have two variables with a clear monotonic but non-linear relationship. Which correlation method should you use to best capture their association?
ASpearman correlation
BPearson correlation
CBoth Pearson and Spearman will give the same result
DNeither Pearson nor Spearman is suitable
Attempts:
2 left
💡 Hint
Consider which method uses ranks and can detect monotonic relationships.

Practice

(1/5)
1. What does the Pearson correlation coefficient measure between two variables?
easy
A. The rank order similarity between two variables
B. The difference in means between two groups
C. The strength and direction of a linear relationship
D. The frequency distribution of a variable

Solution

  1. Step 1: Understand Pearson correlation concept

    Pearson correlation measures how strongly two variables move together in a straight line.
  2. Step 2: Differentiate from other measures

    Unlike Spearman, Pearson focuses on linear relationships, not ranks or means.
  3. Final Answer:

    The strength and direction of a linear relationship -> Option C
  4. Quick Check:

    Pearson = linear relationship [OK]
Hint: Pearson = linear, Spearman = rank-based [OK]
Common Mistakes:
  • Confusing Pearson with Spearman correlation
  • Thinking Pearson measures differences in means
  • Assuming Pearson works for non-linear relationships
2. Which of the following Python code snippets correctly calculates the Spearman correlation between two pandas Series x and y?
easy
A. x.corr(y, method='spearman')
B. x.corr(y, method='pearson')
C. spearmanr(x, y)
D. corr(x, y, method='spearman')

Solution

  1. Step 1: Recall pandas correlation syntax

    In pandas, Series.corr() accepts a method argument with values like 'pearson' or 'spearman'.
  2. Step 2: Identify correct method call

    x.corr(y, method='spearman') uses x.corr(y, method='spearman'), which is the correct pandas syntax.
  3. Final Answer:

    x.corr(y, method='spearman') -> Option A
  4. Quick Check:

    pandas Series.corr with method='spearman' [OK]
Hint: Use Series.corr with method='spearman' for Spearman correlation [OK]
Common Mistakes:
  • Using 'pearson' instead of 'spearman' for Spearman correlation
  • Calling a non-existent function like corr() standalone
  • Confusing scipy's spearmanr with pandas syntax
3. Given the following code, what is the output of print(round(corr, 2))?
import pandas as pd
x = pd.Series([1, 2, 3, 4, 5])
y = pd.Series([5, 6, 7, 8, 8])
corr = x.corr(y, method='pearson')
medium
A. 0.75
B. 0.90
C. 0.85
D. 0.97

Solution

  1. Step 1: Calculate Pearson correlation manually

    Calculate covariance and standard deviations of x and y, then compute correlation. Here, x and y increase mostly together, so correlation is high.
  2. Step 2: Use pandas to confirm

    Running the code gives approximately 0.9746, which rounds to 0.97.
  3. Final Answer:

    0.97 -> Option D
  4. Quick Check:

    pandas Pearson correlation ≈ 0.97 [OK]
Hint: Use pandas corr() to get exact correlation quickly [OK]
Common Mistakes:
  • Confusing Pearson with Spearman correlation values
  • Rounding incorrectly or too early
  • Assuming correlation must be exactly 1 for similar sequences
4. Identify the error in this code snippet that attempts to calculate Spearman correlation:
from scipy.stats import spearmanr
x = [1, 2, 3, 4]
y = [4, 3, 2, 1]
result = spearmanr(x, y)
print(result.correlation)
medium
A. The function spearmanr requires numpy arrays, not lists
B. The attribute 'correlation' does not exist in the result object
C. The print statement should be inside a function
D. The import statement is incorrect

Solution

  1. Step 1: Check spearmanr return type

    spearmanr returns a namedtuple SpearmanRResult with fields statistic (Spearman correlation coefficient) and pvalue.
  2. Step 2: Confirm attribute access

    The code accesses result.correlation, but no such attribute exists. Use result.statistic instead.
  3. Final Answer:

    The attribute 'correlation' does not exist in the result object -> Option B
  4. Quick Check:

    spearmanr uses result.statistic [OK]
Hint: Use result.statistic for spearmanr correlation [OK]
Common Mistakes:
  • Assuming lists cannot be used with spearmanr
  • Misunderstanding the return type of spearmanr
  • Incorrect import or syntax errors
5. You have two variables: age (numeric) and education_level (ordinal ranks: 1=High School, 2=Bachelor, 3=Master, 4=PhD). Which correlation method is most appropriate to measure their association and why?
hard
A. Spearman, because education_level is ordinal and relationship may be monotonic
B. Pearson, because it handles ordinal data better
C. Neither, because correlation cannot be calculated between these variables
D. Pearson, because both are numeric variables

Solution

  1. Step 1: Identify variable types

    Age is numeric continuous; education_level is ordinal categorical (ranked).
  2. Step 2: Choose correlation method

    Spearman correlation is best for ordinal data and monotonic relationships, unlike Pearson which assumes linear and interval data.
  3. Final Answer:

    Spearman, because education_level is ordinal and relationship may be monotonic -> Option A
  4. Quick Check:

    Ordinal data + monotonic = Spearman [OK]
Hint: Use Spearman for ordinal or ranked data [OK]
Common Mistakes:
  • Using Pearson for ordinal data incorrectly
  • Thinking correlation can't be done with ordinal variables
  • Confusing monotonic with linear relationships
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