0
0
SciPydata~5 mins

Spearman correlation in SciPy

Choose your learning style9 modes available
Introduction

Spearman correlation helps us find how two things move together in order, even if the exact numbers don't match perfectly.

When you want to see if taller people tend to weigh more, without assuming exact weight differences.
When your data is ranked or ordered, like race positions or customer satisfaction ratings.
When the relationship between two variables is not straight but still follows a pattern.
When your data has outliers that might confuse other correlation methods.
When you want to check if two sets of exam scores are related by their ranks.
Syntax
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.

Examples
Calculate Spearman correlation between two simple lists.
SciPy
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)
Calculate Spearman correlation for numpy arrays with a perfect negative rank relationship.
SciPy
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)
Sample Program

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.

SciPy
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}")
OutputSuccess
Important Notes

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.

Summary

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.