决策树ID3算法

TypeError: 'dict_keys' object does not support indexing

来源:python常见错误集合

9.TypeError: ‘dict_keys’ object does not support indexing

这个问题是python版本的问题

#如果使用的是python2
firstStr = myTree.keys()[0]
#LZ使用的是python3
firstSides = list(myTree.keys())
firstStr = firstSides[0]12345

这是看决策树代码出现的问题,python3如果运行

firstStr = myTree.keys()[0]1

就会报这个错误,解决办法就是先转换成list,再把需要的索引提取出来。
NameError: name 'trainLabel' is not defined
    def labelCount(self, dataset):
        labelCount = {}
        for i in dataset:
            if trainLabel[i] in labelCount.keys():
                labelCount[trainLabel[i]] += 1
            else:
                labelCount[trainLabel[i]] = 1

        return labelCount

# 改为
    def labelCount(self, dataset):
        labelCount = {}
        for i in dataset:
            if self.trainLabel[i] in labelCount.keys():
                labelCount[self.trainLabel[i]] += 1
            else:
                labelCount[self.trainLabel[i]] = 1

        return labelCount

 

你可能感兴趣的:(决策树ID3算法)