Correlation analysis helps us find out how two things change together. It tells if one thing goes up when the other goes up or down.
Correlation analysis (Pearson, Spearman) in Data Analysis Python
from scipy.stats import pearsonr, spearmanr # For Pearson correlation correlation, p_value = pearsonr(data1, data2) # For Spearman correlation correlation, p_value = spearmanr(data1, data2)
pearsonr measures linear relationship between two numeric sets.
spearmanr measures monotonic relationship using ranks, good for non-linear or ordinal data.
from scipy.stats import pearsonr x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] correlation, p_value = pearsonr(x, y) print(correlation)
from scipy.stats import spearmanr x = [1, 2, 3, 4, 5] y = [5, 6, 7, 8, 7] correlation, p_value = spearmanr(x, y) print(correlation)
This program calculates both Pearson and Spearman correlations between hours studied and exam scores to see how strongly they are related.
from scipy.stats import pearsonr, spearmanr # Sample data: hours studied and exam scores hours_studied = [1, 2, 3, 4, 5, 6, 7, 8] exam_scores = [50, 55, 65, 70, 75, 80, 85, 90] # Calculate Pearson correlation pearson_corr, pearson_p = pearsonr(hours_studied, exam_scores) # Calculate Spearman correlation spearman_corr, spearman_p = spearmanr(hours_studied, exam_scores) print(f"Pearson correlation: {pearson_corr:.2f}") print(f"Spearman correlation: {spearman_corr:.2f}")
Correlation values range from -1 to 1. Close to 1 means strong positive relation, close to -1 means strong negative relation, and near 0 means no relation.
Use Pearson when data is linear and numeric. Use Spearman when data is not linear or has ranks.
Always check p-value to see if correlation is statistically significant (usually p < 0.05).
Correlation analysis shows how two variables move together.
Pearson measures linear relationships; Spearman measures monotonic relationships.
Both give a correlation value and a p-value to check significance.
Practice
Solution
Step 1: Understand Pearson correlation concept
Pearson correlation measures how strongly two variables move together in a straight line.Step 2: Differentiate from other measures
Unlike Spearman, Pearson focuses on linear relationships, not ranks or means.Final Answer:
The strength and direction of a linear relationship -> Option CQuick Check:
Pearson = linear relationship [OK]
- Confusing Pearson with Spearman correlation
- Thinking Pearson measures differences in means
- Assuming Pearson works for non-linear relationships
x and y?Solution
Step 1: Recall pandas correlation syntax
In pandas,Series.corr()accepts amethodargument with values like 'pearson' or 'spearman'.Step 2: Identify correct method call
x.corr(y, method='spearman') usesx.corr(y, method='spearman'), which is the correct pandas syntax.Final Answer:
x.corr(y, method='spearman') -> Option AQuick Check:
pandas Series.corr with method='spearman' [OK]
- Using 'pearson' instead of 'spearman' for Spearman correlation
- Calling a non-existent function like corr() standalone
- Confusing scipy's spearmanr with pandas syntax
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')
Solution
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.Step 2: Use pandas to confirm
Running the code gives approximately 0.9746, which rounds to 0.97.Final Answer:
0.97 -> Option DQuick Check:
pandas Pearson correlation ≈ 0.97 [OK]
- Confusing Pearson with Spearman correlation values
- Rounding incorrectly or too early
- Assuming correlation must be exactly 1 for similar sequences
from scipy.stats import spearmanr x = [1, 2, 3, 4] y = [4, 3, 2, 1] result = spearmanr(x, y) print(result.correlation)
Solution
Step 1: Check spearmanr return type
spearmanrreturns a namedtupleSpearmanRResultwith fieldsstatistic(Spearman correlation coefficient) andpvalue.Step 2: Confirm attribute access
The code accessesresult.correlation, but no such attribute exists. Useresult.statisticinstead.Final Answer:
The attribute 'correlation' does not exist in the result object -> Option BQuick Check:
spearmanr uses result.statistic [OK]
- Assuming lists cannot be used with spearmanr
- Misunderstanding the return type of spearmanr
- Incorrect import or syntax errors
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?Solution
Step 1: Identify variable types
Age is numeric continuous; education_level is ordinal categorical (ranked).Step 2: Choose correlation method
Spearman correlation is best for ordinal data and monotonic relationships, unlike Pearson which assumes linear and interval data.Final Answer:
Spearman, because education_level is ordinal and relationship may be monotonic -> Option AQuick Check:
Ordinal data + monotonic = Spearman [OK]
- Using Pearson for ordinal data incorrectly
- Thinking correlation can't be done with ordinal variables
- Confusing monotonic with linear relationships
Ready to prove it?
Pass the test and this pattern moves unknown
