def prune(tree, mingain):
# If the branches aren't leaves, then prune them
if tree.tb.results == None:
prune(tree.tb, mingain)
if tree.fb.results == None:
prune(tree.fb, mingain)
# If both the subbranches are now leaves, see if they
# should merged
if tree.tb.results != None and tree.fb.results != None:
# Build a combined dataset
tb, fb = [], []
for v, c in tree.tb.results.items():
tb += [[v]] * c
for v, c in tree.fb.results.items():
fb += [[v]] * c
# Test the reduction in entropy
delta = entropy(tb + fb) - (entropy(tb) + entropy(fb) / 2)
if delta < mingain:
# Merge the branches
tree.tb, tree.fb = None, None
tree.results = uniquecounts(tb + fb)
decision_tree_manual_classifier.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录