MCPcopy Create free account
hub / github.com/Jack-Cherish/Machine-Learning / getTreeDepth

Function getTreeDepth

Decision Tree/Decision Tree.py:226–235  ·  view source on GitHub ↗
(myTree)

Source from the content-addressed store, hash-verified

224 2017-07-24
225"""
226def getTreeDepth(myTree):
227 maxDepth = 0 #初始化决策树深度
228 firstStr = next(iter(myTree)) #python3中myTree.keys()返回的是dict_keys,不在是list,所以不能使用myTree.keys()[0]的方法获取结点属性,可以使用list(myTree.keys())[0]
229 secondDict = myTree[firstStr] #获取下一个字典
230 for key in secondDict.keys():
231 if type(secondDict[key]).__name__=='dict': #测试该结点是否为字典,如果不是字典,代表此结点为叶子结点
232 thisDepth = 1 + getTreeDepth(secondDict[key])
233 else: thisDepth = 1
234 if thisDepth > maxDepth: maxDepth = thisDepth #更新层数
235 return maxDepth
236
237"""
238函数说明:绘制结点

Callers 2

plotTreeFunction · 0.85
createPlotFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected