使用的變量的聲明如下:類型錯誤:列表索引必須是整數,而不是str的
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]
哪條線產生錯誤? – mgilson
Prob + = self.featureCounts [[label] [self.featureNameList [self.featureVectors.index(featureValue)]] [featureValue]]/self.labelCounts [標籤] – Soham