Skip to content

Instantly share code, notes, and snippets.

/spearman_eng.py
Created Nov 6, 2014

Embed
What would you like to do?
script for Spearman Correlation
import csv
class csvFile:
File = ''
def openFile(self, path, delimitador):
self.File = csv.reader(open(path), delimiter=delimitador)
def Values(self):
matrixXY = []
for values in self.File:
matrixXY.append( [float(values[0]), float(values[1]) ] )
return(matrixXY)
class statistics:
def createMatrixDi(self, matrixxy):
matrixxy.sort()
vectorY = []
index = 1
for values in matrixxy:
vectorY.append( [ values[1], index ])
index += 1
x = 0
matrix= []
while (x < len(vectorY)):
matrix.append( [(x+1), vectorY[x][1]] )
x += 1
print "The error is here:"
print "vectorY: %s\n" %vectorY
print "matrix: %s\n" %matrix
print "error: the second number in the matrix is always equal to the first"
print
return(matrix, len(vectorY))
def calculateDi2(self, matrix):
sum = 0.
for value in matrix:
sum = sum + pow( (value[0] + value[1] ) ,2)
return sum
def calculateSpearman(self, sumDi2, n):
p = 0.
p = 1 - ( (6* sumDi2)/( pow(n,3) - n ) )
return p
file = csvFile()
delimitador = ','
print "Use a CSV file with two columns and no empty lines "
print "Name of file to open: "
path = raw_input()
print
file.openFile(path, delimitador)
matrixXY = file.Values()
calculum = statistics()
Matrix, num = calculum.createMatrixDi(matrixXY)
sumDi2 = calculum.calculateDi2(Matrix)
p = calculum.calculateSpearman(sumDi2, num)
print "Spearman correlation: %s" %p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.