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] 

Dear Group,
If anyone of the learned members help me to understand the above code:
Apology for the indentation errors.

4 Years
Discussion Span
Last Post by rrashkin

There is obviously something missing: the creation of the list, obs. Given that, the method, add(), updates the values in prior and total for each element of obs. The method, discr(), looks like it's updating something like a grade point average for each element in obs. The method, classify(), looks like it should return the highest grade point average (if that's really what discr() returns) for each element in prior, only I don't think the "[1]" is right.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.