Jul-18-2017, 01:13 PM
Dear Python Users,
Please, help me to identify what is wrong with the code. I need to construct a "class" to calculate the correlation between two lists. For this I use the following code:
Please, help me to identify what is wrong with the code. I need to construct a "class" to calculate the correlation between two lists. For this I use the following code:
import math
class Correlation:
def __init__(self, a = [], b = []):
self.a = a
self.b = b
def getA(self):
return self.a
def getB(self):
return self.b
def Mean1(self):
tsum = 0
for x in range(0, len(self.a)):
tsum = tsum + self.a[x]
return tsum/len(self.a)
def Mean2(self):
tsum = 0
for x in range(0, len(self.b)):
tsum = tsum + self.b[x]
return tsum/len(self.b)
def Sample_Standard_Deviation1(self):
Mean_of_L1 = Correlation.Mean1(self.a)
tsum = 0
for x in range(0, len(self.a)):
tsum = tsum + (self.a[x] - Mean_of_L1) ** 2
return math.sqrt(tsum / (len(self.a) - 1))
def Sample_Standard_Deviation2(self):
Mean_of_L1 = Correlation.Mean2(self.b)
tsum = 0
for x in range(0, len(self.b)):
tsum = tsum + (self.a[x] - Mean_of_L1) ** 2
return math.sqrt(tsum / (len(self.b) - 1))
def Correlation_Coefficient(self):
meanx = Correlation.Mean1(self.a)
meany = Correlation.Mean2(self.b)
stdx = Correlation.Sample_Standard_Deviation(self.a)
stdy = Correlation.Sample_Standard_Deviation(self.b)
tsum = 0
for idx in range(0,len(self.a)):
tsum = tsum + ((self.a[idx] - meanx)/stdx) * ((self.b[idx] - meany) / stdy)
return tsum / (len(self.a) - 1)
and to run the program I use:import Correlation as c
x = [1,2,3]
y = [10,20,30]
c1 = c.Correlation(x, y)
print("Correlation is %.2f" % c1.Correlation_Coefficient())
However, I get an error indicating that ''list' object has no attribute 'a''. Can you, please, help me to resolve this issue.
