所用变量的声明如下:
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]
你的问题可能是:
在我看来,你列出了一个长度为1的列表:
然后尝试通过索引从中获取元素:
但外括号中的内容计算为字符串。字符串不能用于索引列表。
最后,这几乎肯定不是你想做的,但我认为这解释了错误。一般来说,我建议您避免像这样长而混乱的一行程序,并使用临时(但名称恰当)变量将其分解为各个组成部分。这将使您的代码更容易理解,因此它将更容易编写和开发。
相关问题 更多 >
编程相关推荐