使用的变量的声明如下:
self.features = {} #dictionary defined for storing the features and the values
self.featureNameList = [] #list to store the names and values of the features
self.featureCounts = collections.defaultdict(lambda: 1) #the counts of the features and labels
self.featureVectors = [] #
self.labelCounts = collections.defaultdict(lambda: 0)
def Classify(self): #featureVector is a simple list like the ones that we use to train
probabilityPerLabel = {}
for label in self.labelCounts.keys():
Prob = 0
for featureValue in self.featureVectors:
#print self.labelCounts[label]
Prob+=self.featureCounts[[label][self.featureNameList[self.featureVectors.index(featureValue)]][featureValue]]/self.labelCounts[label]
# Prob+= self.featureCounts(label, self.featureNameList[self.featureVectors.index(featureValue)], featureValue)/self.labelCounts[label]
probabilityPerLabel[label] = (self.labelCounts[label]/sum(self.labelCounts.values())) * (Prob)
print probabilityPerLabel
return max(probabilityPerLabel, key = lambda classLabel: probabilityPerLabel[classLabel])
该错误是在该行产生的:
Prob+=self.featureCounts[[label][self.featureNameList[self.featureVectors.index(featureValue)]][featureValue]]/self.labelCounts[label]