'''
Created on Oct 14, 2010
@author: Peter Harrington
'''
import matplotlib.pyplot as plt
#from trees import *
decisionNode = dict(boxstyle="sawtooth", fc="0.8")
#print(decisionNode)
leafNode = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")
def getNumLeafs(myTree):
#print(myTree)
numLeafs = 0
firstStr = list(myTree.keys())[0]
#print(myTree)
#print(myTree.keys())
#print(firstStr)
#{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}
secondDict = myTree[firstStr]
#print(secondDict)
for key in secondDict.keys():
#print(secondDict[key])验证树的节点值是不是还是字典,如果是字典,继续要递归,直到得到叶子节点的个数。
#即每个节点Yes和No都被判断到了。总共有3层。
#print(key)
if type(secondDict[key]).__name__=='dict':#test to see if the nodes are dictonaires, if not they are leaf nodes
#print(type(secondDict[key]).__name__)
numLeafs += getNumLeafs(secondDict[key])
【机器学习笔记-决策树-3】利用matplotlib绘制决策树图形
最新推荐文章于 2025-05-29 12:49:12 发布