Spearman correlation helps us find how two things move together in order, even if the exact numbers don't match perfectly.
Spearman correlation in SciPy
from scipy.stats import spearmanr correlation, p_value = spearmanr(x, y)
x and y are lists or arrays of numbers of the same length.
The function returns two values: the Spearman correlation coefficient and a p-value to test significance.
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)
from scipy.stats import spearmanr import numpy as np x = np.array([10, 20, 30, 40]) y = np.array([40, 30, 20, 10]) correlation, p_value = spearmanr(x, y) print(correlation)
This code finds the Spearman correlation between hours studied and exam ranks. It shows how strongly the order of hours studied relates to exam performance.
from scipy.stats import spearmanr # Sample data: hours studied and exam ranks hours_studied = [2, 3, 5, 8, 13] exam_ranks = [5, 4, 3, 2, 1] correlation, p_value = spearmanr(hours_studied, exam_ranks) print(f"Spearman correlation: {correlation:.2f}") print(f"P-value: {p_value:.4f}")
A Spearman correlation of 1 means perfect increasing order match, -1 means perfect decreasing order match.
The p-value helps check if the correlation is likely due to chance; a small p-value means it's probably real.
Spearman correlation works well even if the data is not normally distributed.
Spearman correlation measures how well two variables relate by their order or rank.
It is useful when data is not linear or has outliers.
Use scipy.stats.spearmanr to calculate it easily in Python.