Spearman’s Correlation Coefficient is widely used in deep learning right now, which is very useful to estiment the correlation of two variables. In this tutorial, we will introduce how to calculate spearman’s correlation coefficient.
Method 1: Use scipy.stats
Preliminaries
from scipy.stats import spearmanr
Create a pair of data (x, y)
x = [1, 8, 3, 4, 5, 6, 7, 2, 9, 10] y = [1, 5, 8, 7, 10, 9, 3,6 , 2, 4]
This pair data is like:
You can learn how to implement a scatter plot with matplotlib in:
Best Practice to Implement Scatter Plot with Matplotlib – Matplotlib Tutorial
Calculate spearman correlation coefficient
value = spearmanr(x, y) print value[0]
The output is:
-0.175757575758
This value mean that x and y is week monotonic relationship.
Method 2: Calculate it with its formula
Spearman’s correlation coefficient is defined:
We can calculate it with code:
n = len(x) total = 0 for i in range(n): total += (x[i]-y[i])**2 spearman = 1 - float(6 * total) / (n * (n ** 2 - 1)) print spearman
The output is:
-0.175757575758