Understanding Code

subhabangalore at gmail.com subhabangalore at gmail.com
Tue Nov 13 04:05:37 EST 2012


Dear Group,
To improve my code writing I am trying to read good codes. Now, I have received a code,as given below,(apology for slight indentation errors) the code is running well. 
Now to comprehend the code, I am looking to understand it completely.

class Calculate:
      def __init__(self):
        self.prior = {}  
        self.total = {}  
        self.count = 0   
      def add(self, cls, obs):
        self.prior[cls] = self.prior.get(cls, 0) + 1
        for idx, val in enumerate(obs):
            key = cls, idx, val
            self.total[key] = self.total.get(key, 0) + 1
        self.count += 1
      def discr(self, cls, obs):
        result = self.prior[cls]/self.count
        for idx, val in enumerate(obs):
            freq = self.total.get((cls, idx, val), 0)
            result *= freq/self.prior[cls]
        return result
      def classify(self, obs):
        candidates = [(self.discr(c, obs), c) for c in self.prior]
        return max(candidates)[1]

I am not understanding many parts of it, I am understanding many parts of it also.

So I am looking for an exercise what are the things I should know to understand it, (please do not give answers I would get back with the answers in a week and would discuss even how to write better than this).

If any one of the expert members of the room kindly help me to do this.

Thanking You in Advance,
Regards,
Subhabrata. 



More information about the Python-list mailing list