决策树-构造注解树
来源:互联网 发布:蒙泰打印机端口设置 编辑:程序博客网 时间:2024/06/14 06:34
注解树:将树中的信息添加到决策树图中。
Note:绘制图形的x轴有效范围是0.0到1.0,y轴有效范围也是0.0到1.0。
def getNumLeafs(myTree): #获取叶节点树 numLeafs = 0 # firstStr = myTree.keys()[0] #mytree的第一个特征值 python2写法 first = list(myTree.keys()) firstStr = first[0] secondDict = myTree[firstStr] #mytree经过第一个特征值分类后的字典 for key in secondDict.keys(): if type(secondDict[key]).__name__=='dict': #判断是否分类过的数据为字典 numLeafs += getNumLeafs(secondDict[key]) #子节点数量+1 递归,判断一共多少个子节点 else: numLeafs += 1 return numLeafsdef getTreeDepth(myTree): #获取树深度 maxDepth = 0 # firstStr = myTree.keys()[0] #mytree的第一个特征值 python2写法 first = list(myTree.keys()) firstStr = first[0] secondDict = myTree[firstStr] # mytree经过第一个特征值分类后的字典 for key in secondDict.keys(): if type(secondDict[key]).__name__ == 'dict': # 判断是否分类过的数据为字典 thisDepth = 1 + getTreeDepth(secondDict[key]) #数的深度+1 else: thisDepth = 1 if thisDepth > maxDepth: maxDepth = thisDepth return maxDepth #最大深度 #二叉树的深度是指有多少层, 而二元决策树的深度是指经过多少层计算#测试的树数据def retrieveTree(i): listOfTrees = [{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1:'yes'}}}}, {'no surfacing': {0: 'no', 1: {'flippers': {0: {'head': {0: 'no', 1:'yes'}}, 1: 'no'}}}}] return listOfTrees[i]def plotMidText(cntrPt, parentPt, txtString): #在坐标点cntrPt和parentPt连接线上的中点,显示文本txtString xMid = (parentPt[0]-cntrPt[0])/2.0 + cntrPt[0] #x坐标 yMid = (parentPt[1]-cntrPt[1])/2.0 + cntrPt[1] #y坐标 createPlot.ax1.text(xMid, yMid, txtString) #在(xMid, yMid)处显示txtStringdef plotTree(myTree, parentPt, nodeTxt): numLeafs = getNumLeafs(myTree) #获得当前树叶节点个数 depth = getTreeDepth(myTree) #获得当前树深度 # firstStr = myTree.keys()[0] #mytree的第一个特征值 python2写法 first = list(myTree.keys()) firstStr = first[0] #第一个分类的特征值,即根节点 cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff) # plotTree.xOff和plotTree.yOff是用来追踪已经绘制的节点位置,plotTree.totalW为这个数的宽度,叶节点数 plotMidText(cntrPt, parentPt, nodeTxt) #显示节点 plotNode(firstStr, cntrPt, parentPt, decisionNode) #firstStr为需要显示的文本,cntrPt为文本的中心点, # parentPt为箭头指向文本的起始点,decisionNode为文本属性 secondDict = myTree[firstStr] #子树 plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD #totalD是这个数的深度,深度移下一层 for key in secondDict.keys(): if type(secondDict[key]).__name__=="dict": plotTree(secondDict[key], cntrPt, str(key)) #递归显示子树 else: plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW #x坐标平移一个单位 plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode) #画叶节点 plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key)) #显示箭头文本 plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD #下移一层深度def createPlot(inTree): fig = plt.figure(1, facecolor = 'white') #创建一个画布,背景为白色 fig.clf() #画布清空 axprops = dict(xticks=[], yticks=[]) #定义横纵坐标轴,无内容 #ax1是函数createPlot的一个属性,这个可以在函数里面定义也可以在函数定义后加入也可以 # createPlot.ax1 = plt.subplot(111, frameon = False, **axprops) #frameon表示是否绘制坐标轴矩形,无坐标轴 createPlot.ax1 = plt.subplot(111, frameon = False) #frameon表示是否绘制坐标轴矩形 plotTree.totalW = float(getNumLeafs(inTree)) #树的宽度 plotTree.totalD = float(getTreeDepth(inTree)) #树的深度 plotTree.xOff = -0.5/plotTree.totalW #x轴起始值,之前一开始定义了1.0+,有一个偏差 plotTree.yOff = 1.0 plotTree(inTree, (0.5, 1.0), '') plt.show()myTree = retrieveTree(0)createPlot(myTree)
此树叶节点为3,深度为2。
阅读全文
0 0
- 决策树-构造注解树
- py2.7 : 《机器学习实战》 决策树 12.5:构造注解树
- Python构造决策树
- 第三章 决策树 3.1决策树构造
- 决策树的python代码注解
- 用Python从零开始构造决策树
- ID3 决策树算法伪代码及注解
- 《机器学习实战》第三章:决策树(2)树的构造
- 数据挖掘3---决策树的构造
- 决策树学习 之 概念与构造算法
- 机器学习实战python版决策树以及Matplotlib注解绘制决策树
- 机器学习实战python版决策树以及Matplotlib注解绘制决策树
- 第三章 决策树 3.2 使用 Matplotlib 注解绘制树形图
- KD树与决策树
- 决策树vs行为树
- 决策树中熵和信息增益的计算,构造决策树 ID3
- 机器学习(一):决策树算法及使用python构造一个决策树
- 决策树
- 算法模板——线段树之Lazy标记
- 如何统一jdk版本
- ubuntu 安装qt5.9.3 for arm-linux am335x qt开发2
- Java web过滤器验证登录防止未登录进入界面
- VMware_centos 无法自动获取IP地址
- 决策树-构造注解树
- JNI从ByteBuffer获取数据,返回byte[]
- 《ApacheFlume教程》第八章 Flum中HDFS如何启动
- Java多线程使用场景
- 用flex实现弹性布局
- 对象引用与对象的区别
- 扫雷
- 日志系统架构,后台服务架构图
- ntp本地服务器搭建