Skip to content

Instantly share code, notes, and snippets.

@kvorion
Created December 7, 2010 05:16
Show Gist options
  • Save kvorion/731492 to your computer and use it in GitHub Desktop.
Save kvorion/731492 to your computer and use it in GitHub Desktop.
classify method
def Classify(self, featureVector): #featureVector is a simple list like the ones that we use to train
probabilityPerLabel = {} #store the final probability for each class label
for label in self.labelCounts:
logProb = 0
for featureValue in featureVector:
logProb += math.log(self.featureCounts[(label, self.featureNameList[featureVector.index(featureValue)], featureValue)]/self.labelCounts[label])
probabilityPerLabel[label] = (self.labelCounts[label]/sum(self.labelCounts.values())) * math.exp(logProb)
print probabilityPerLabel
return max(probabilityPerLabel, key = lambda classLabel: probabilityPerLabel[classLabel])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment