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


Messages In This Thread
Class construction in python - by Alberto - Jul-18-2017, 01:13 PM
RE: Class construction in python - by ichabod801 - Jul-18-2017, 02:25 PM
RE: Class construction in python - by Alberto - Jul-18-2017, 02:39 PM
RE: Class construction in python - by ichabod801 - Jul-18-2017, 03:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Total electric construction cost project SecondSandwich94 2 2,287 Jul-21-2021, 09:37 PM
Last Post: deanhystad
  I'm blocked in the construction of my program [Novice] abcd 1 2,698 May-22-2018, 06:02 AM
Last Post: buran
  Converting c++ class to python class panoss 12 12,137 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