决策树算法学习笔记(三)-预测隐形眼镜类型
#coding=utf-8 import matplotlib.pyplot as plt #定义文本框和箭头格式 decisionNode=dict(boxstyle="sawtooth",fc="0.8") leafNode = dict(boxstyle="round4",fc="0.8") arrow_args=dict(arrowstyle="<-")def getNumLeafs(myTree):numLeafs=0 firstStr=myTree.keys()[0]secondDict=myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__=='dict':numLeafs+=getNumLeafs(secondDict[key])else:numLeafs+=1 return numLeafsdef getTreeDepth(myTree):maxDepth=0 firstStr=myTree.keys()[0]secondDict = myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__=='dict':thisDepth=1+getTreeDepth(secondDict[key])else:thisDepth=1 if thisDepth>maxDepth:maxDepth=thisDepthreturn maxDepthdef plotMidText(cntrPt,parentPt,txtString):xMid=(parentPt[0]-cntrPt[0])/2.0+cntrPt[0]yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]createPlot.axl.text(xMid,yMid,txtString)def plotTree(myTree,parentPt,nodeTxt):numLeafs=getNumLeafs(myTree)depth=getTreeDepth(myTree)firstStr=myTree.keys()[0]cntrPt=(plotTree.xOff+(1.0+float(numLeafs))/2.0/plotTree.totalW,plotTree.yOff)plotMidText(cntrPt,parentPt,nodeTxt)plotNode(firstStr,cntrPt,parentPt,decisionNode)secondDict=myTree[firstStr]plotTree.yOff=plotTree.yOff-1.0/plotTree.totalDfor key in secondDict.keys():if type(secondDict[key]).__name__=='dict':plotTree(secondDict[key],cntrPt,str(key))else:plotTree.xOff=plotTree.xOff+1.0/plotTree.totalWplotNode(secondDict[key],(plotTree.xOff,plotTree.yOff),cntrPt,leafNode)plotMidText((plotTree.xOff,plotTree.yOff),cntrPt,str(key))plotTree.yOff=plotTree.yOff+1.0/plotTree.totalDdef createPlot(inTree):fig=plt.figure(1,facecolor='white')fig.clf()axprops=dict(xticks=[],yticks=[])createPlot.axl=plt.subplot(111,frameon=False,**axprops)plotTree.totalW=float(getNumLeafs(inTree))plotTree.totalD=float(getTreeDepth(inTree))plotTree.xOff=-0.5/plotTree.totalW;plotTree.yOff=1.0 plotTree(inTree,(0.5,1.0),'')plt.show()def plotNode(nodeTxt,centerPt,parentPt,nodeType):createPlot.axl.annotate(nodeTxt,xy=parentPt,xycoords='axes fraction',xytext=centerPt,textcoords='axes fraction',\va="center",ha="center",bbox=nodeType,arrowprops=arrow_args)# def createPlot(): # fig=plt.figure(1,facecolor='white') # fig.clf() # createPlot.axl=plt.subplot(111,frameon=False) # plotNode('决策节点',(0.5,0.1),(0.1,0.5),decisionNode) # plotNode('叶节点', (0.8, 0.1), (0.3, 0.8), leafNode) # plt.show() 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] # myTree=retrieveTree(0) # myTree['no surfacing'][3]='maybe' # print myTree # createPlot(myTree) # print retrieveTree(0) # print getNumLeafs(myTree=retrieveTree(0)) # print getTreeDepth(myTree=retrieveTree(0))
def fileReading(filename):fr=open(filename)dataSet=fr.readlines()lenses=[inst.strip().split('\t') for inst in dataSet]lensesLabels=['age','prescript','astigmatic','tearRate']return lenses,lensesLabelsdataSet,labels=fileReading('lenses.txt') lensesTree=createTree(dataSet,labels) print lensesTree treePlotter.createPlot(lensesTree)
决策树算法学习笔记(三)-预测隐形眼镜类型相关推荐
- Python3:《机器学习实战》之决策树算法(3)预测隐形眼镜类型
Python3:<机器学习实战>之决策树算法(3)预测隐形眼镜类型 转载请注明作者和出处:http://blog.csdn.net/u011475210 代码地址:https://gith ...
- Python3:《机器学习实战》之决策算法(3)预测隐形眼镜类型
Python3:<机器学习实战>之决策树算法(3)预测隐形眼镜类型 转载请注明作者和出处:http://blog.csdn.net/u011475210 代码地址:https://gith ...
- 决策树算法学习笔记(提升篇)
声明:本文虽有部分自己理解成分,但是大部分摘自以下链接. 决策树(decision tree)(三)--连续值处理 决策树参数讲解+实例 数据挖掘十大算法 C4.5算法的改进: 用信息增益率来选择属性 ...
- java日期类型_Java 学习笔记 (三) Java 日期类型
以下内容摘自: https://www.cnblogs.com/crazylqy/p/4172324.html import java.sql.Timestamp; import java.text ...
- python3.5《机器学习实战》学习笔记(五):决策树算法实战之预测隐形眼镜类型
转载请注明作者和出处:http://blog.csdn.net/u013829973 系统版本:window 7 (64bit) 文章出现的所有代码在我的GitHub:https://github.c ...
- 《机器学习实战》学习笔记:绘制树形图使用决策树预测隐形眼镜类型
上一节实现了决策树,但只是使用包含树结构信息的嵌套字典来实现,其表示形式较难理解,显然,绘制直观的二叉树图是十分必要的.Python没有提供自带的绘制树工具,需要自己编写函数,结合Matplotlib ...
- 徒手写代码之《机器学习实战》-----决策树算法(2)(使用决策树预测隐形眼镜类型)
使用决策树预测隐形眼镜类型 说明: 将数据集文件 'lenses.txt' 放在当前文件夹 from math import log import operator 熵的定义 "" ...
- ID3构造决策树预测隐形眼镜类型(代码笔记)
决策树可以从数据集合中提取出一系列规则,从而分类样本.它的优势是理解数据蕴含信息. 思想:利用信息增益(information gain)[度量数据集信息的方式-香农熵(entropy)]计算得出最好 ...
- 机器学习——决策树实践(预测隐形眼镜类型)
前言 之前把<机器学习实战>这本书的分类部分学完了,想自己动手实践一下,所以从前面的章节开始,慢慢熟悉代码. 今天在学习决策树的时候,发现书中并没有直接给出预测隐形眼镜类型的代码,于是想借 ...
最新文章
- sdut 2805(最小生成树)
- P3项目轶事之一票否决的新员工培训
- windows服务器连接linux nfs,windows设置通过NFS连接到Linux共享数据
- java flex 上传文件_使用Flex和java servlet上传文件
- Jquery validate验证表单只验证第一个input元素
- php72w redis,docker php7安装php-redis
- JavaFX UI控件教程(十二)之List View
- 获取要素集中字段的唯一值
- typedef 定义数组类型_Java基础之数组
- leetcode 394.字符串解码 dfs
- [ZPG TEST 115] 字符串【归类思想】
- Day14 字符编码
- SharePoint 2010学习资源
- VS2017_Reshaper
- HTML元素居中的三种方法
- android app安装apk,Android App更新安装APK
- matlab波形叠加,matlab程序两列波相向传播叠加波形图和动画.doc
- 一名爱折腾人士的Apps for iPhone分享
- 人脸识别:特征脸(Eigenface)
- python 习题练习一
热门文章
- 十大远程控制软件(远程控制软件推荐)
- 小作品--linux下设计MATLAB GUI 实现美图秀秀的功能
- 教你把多段视频合并成一个长视频,并添加转场特效
- OC Apple IAP 自动续费订阅 重复订阅问题
- 中南大学校长是计算机专业的吗,张尧学接棒黄伯云 中南大学来了个计算机校长...
- Processing - 练习(3)飞碟追踪 - (渐进、延迟效果)
- 【数据下载】使用DownThemAll批量下载网站上的文件
- AutoCAD2020.1.2官方下载地址
- 内外盘期货市场的介绍(二)
- 房地产楼盘“认购-交房-整改”业务特色签署场景,客户、承建商在线签