Created
August 4, 2017 11:41
-
-
Save BastinRobin/e6f50e13c26b63ae8e0411b93b649986 to your computer and use it in GitHub Desktop.
Spearman implementation In Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import division | |
| import numpy as np | |
| ''' Spearman Correlation ''' | |
| x = [73, 76, 78, 65, 86, 82, 91] | |
| y = [77, 78, 79, 80, 86, 89, 95] | |
| col = [list(a) for a in zip(x, y)] | |
| xy = sorted(col, key=lambda x: x[0], reverse=True) | |
| for i, row in enumerate(xy): | |
| # appending x_ value | |
| row.append(i+1) | |
| _xy = sorted(xy, key=lambda x: x[1], reverse=True) | |
| for i, row in enumerate(_xy): | |
| # appending y_ value | |
| row.append(i+1) | |
| # appending d = (x_ - y_) | |
| row.append(row[2] - row[3]) | |
| # appending (x_ - y_)^2 | |
| row.append((row[2] - row[3]) ** 2) | |
| _xy = np.array(_xy) | |
| n = len(_xy) | |
| d = np.sum(_xy[:,4]) | |
| d2 = np.sum(_xy[:,5]) | |
| print (1 - (6*d2) / (n*(n**2 - 1)))*100 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment