在学习python构建决策树时,使用GitHub中的代码遇到一个问题:字典中keyerror错误。
我们都知道如果关键字不在字典中,就会出现会出现keyerror 错误。
github代码地址:
https://github.com/RRdmlearning/Decision-Tree
首先来看看代码:
def dotgraph(decisionTree):
global dcHeadings
dcNodes = defaultdict(list)
"""Plots the obtained decision tree. """
def toString(iSplit, decisionTree, bBranch, szParent="null", indent=''):
if decisionTree.results != None: # leaf node
lsY = []
for szX, n in decisionTree.results.items():
lsY.append('%s:%d' % (szX, n))
dcY = {"name": "%s" % ', '.join(lsY), "parent": szParent}
dcSummary = decisionTree.summary
dcNodes[iSplit].append(['leaf', dcY['name'], szParent, bBranch, dcSummary['impurity'],