使用的变量声明如下:
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]
答案 0 :(得分:2)
您的问题可能是:
[label][self.featureNameList[self.featureVectors.index(featureValue)]
我看起来是你正在制作长度为1的列表:
[label]
然后你试图通过索引来获取一个元素:
[self.featureNameList[self.featureVectors.index(featureValue)]
但是外括号内的东西会计算为字符串。并且字符串不能用于索引列表。
最终,这几乎可以肯定不你想要做什么,但我认为它解释了错误。一般来说,我建议你避免使用那些长期混乱的1-liners,并使用临时(但恰当命名)变量将其分解为组成部分。这将使您的代码更易于理解,因此编写和开发将更容易。