Python Forum
Class construction in python
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class construction in python
#1
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:

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.
Reply
#2
This call here is the problem:

meanx = Correlation.Mean1(self.a)
By referencing the class (Correlation) rather than the instance (self), you are calling an unbound method. But the Mean1 method is written as a bound method, which you would normally call by referencing self. You are then providing a parameter to the call. Since you have called it unbound, it is not providing the automatic self parameter, the parameter you provide (self.a) becomes the self parameter within the method call. Since self.a is a list, you getting the error you saw.

You want to call it as a bound parameter:

meanx = self.Mean1()
That will provide the automatic parameter self (equal to the instance) to the method, and self.a will resolve correctly. You would need to adjust your other calls using Correlation similarly.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for your help! I changed the code as you suggested, but I still get the same mistake - line15 in Mean1
'list' object has no attribute 'a'. Do you know if I enter lists correctly?

Thank you for your help!

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 = self.Mean1()
        meany = self.Mean2()
        stdx = self.Sample_Standard_Deviation1()
        stdy = self.Sample_Standard_Deviation2()
        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) 
import Correlation as c

x = [1,2,3]
y = [10,20,30]

c1 = c.Correlation(x, y)
print("Correlation is %.2f" % c1.Correlation_Coefficient())
Reply
#4
You didn't change them all. You've still got that same code in your sample standard deviation methods.

Is there some reason you're using a class for this? I would leave calculating a correlation to functions. Also, Python doesn't really use getters and setters. If you really need that sort of functionality, look into properties. Otherwise just use normal attribute access.

Oh, and in the future, please post the full traceback for the error you are getting. It makes it much easier for us to figure out what the problem is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Total electric construction cost project SecondSandwich94 2 2,148 Jul-21-2021, 09:37 PM
Last Post: deanhystad
  I'm blocked in the construction of my program [Novice] abcd 1 2,625 May-22-2018, 06:02 AM
Last Post: buran
  Converting c++ class to python class panoss 12 11,820 Jul-23-2017, 01:16 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020