总目录:Python数据分析整理

本文数据以及大部分代码来自《机器学习实战》

机器学习实战

决策树绘制

  • treePlotter
  • 测试与使用

treePlotter

东西太多了,懒得看咋实现的了,直接把书上的代码搬过来,修改了几个可能版本问题引起的bug,加了句保存图片的代码,直接拿来用了。

treePlotter.py

'''
Created on Oct 14, 2010@author: Peter Harrington
'''
import matplotlib.pyplot as pltdecisionNode = dict(boxstyle="sawtooth", fc="0.8")
leafNode = dict(boxstyle="round4", fc="0.8")
arrow_args = dict(arrowstyle="<-")def getNumLeafs(myTree):numLeafs = 0firstStr = list(myTree.keys())[0]secondDict = myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__=='dict':#test to see if the nodes are dictonaires, if not they are leaf nodesnumLeafs += getNumLeafs(secondDict[key])else:   numLeafs +=1return numLeafsdef getTreeDepth(myTree):maxDepth = 0firstStr = list(myTree.keys())[0]secondDict = myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__=='dict':#test to see if the nodes are dictonaires, if not they are leaf nodesthisDepth = 1 + getTreeDepth(secondDict[key])else:   thisDepth = 1if thisDepth > maxDepth: maxDepth = thisDepthreturn maxDepthdef plotNode(nodeTxt, centerPt, parentPt, nodeType):createPlot.ax1.annotate(nodeTxt, xy=parentPt,  xycoords='axes fraction',xytext=centerPt, textcoords='axes fraction',va="center", ha="center", bbox=nodeType, arrowprops=arrow_args )def plotMidText(cntrPt, parentPt, txtString):xMid = (parentPt[0]-cntrPt[0])/2.0 + cntrPt[0]yMid = (parentPt[1]-cntrPt[1])/2.0 + cntrPt[1]createPlot.ax1.text(xMid, yMid, txtString, va="center", ha="center", rotation=30)def plotTree(myTree, parentPt, nodeTxt):#if the first key tells you what feat was split onnumLeafs = getNumLeafs(myTree)  #this determines the x width of this treedepth = getTreeDepth(myTree)firstStr = list(myTree.keys())[0]     #the text label for this node should be thiscntrPt = (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':#test to see if the nodes are dictonaires, if not they are leaf nodes   plotTree(secondDict[key],cntrPt,str(key))        #recursionelse:   #it's a leaf node print the leaf nodeplotTree.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.totalD
#if you do get a dictonary you know it's a tree, and the first element will be another dictdef createPlot(inTree, name):fig = plt.figure(1, facecolor='white')fig.clf()axprops = dict(xticks=[], yticks=[])createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)    #no ticks#createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses 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.savefig('13数据分布情况')plt.savefig(str(name))plt.show()

测试与使用

import treePlottera = {'no_surfacing': {'L1': {'flippers': {'R0': 'no', 'R1': 'yes'}}, 'L0': {'flippers': {'R0': 'why', 'R1': 'no'}}, 'L2': 'yes'}}
treePlotter.createPlot(a, 'test3')

这个dict就是上面一篇文章的结果
https://blog.csdn.net/weixin_44255182/article/details/108748933

{'no_surfacing': {'L1': {'flippers': {'R0': 'no','R1': 'yes'}},'L0': {'flippers': {'R0': 'why','R1': 'no'}},'L2': 'yes'}
}

test3是图片的名字,可以自己命名。

利用python实现决策树图片绘制相关推荐

  1. 【适合Python语言小白的股价图】利用Python中的matplotlib绘制股价图(非k线图)

    [适合Python语言小白的股价图]利用Python中的matplotlib绘制股价图(非k线图) 代码小白,最近做一家公司的股价复盘用到了matplotlib,在此做个小小的记录.代码的逻辑比较笨, ...

  2. 用python画玫瑰花教程-利用Python的turtle库绘制玫瑰教程

    用Python的turtle库绘图是很简单的,闲来无事就画了一个玫瑰花,下面奉上源码.... 源码: ''' Created on Nov 18, 2017 @author: QiZhao ''' i ...

  3. 教你一招利用Python快速去除图片水印

    大家好,我是IT界搬运工. 相信大家都有在网上下载好图片但是有水印的烦恼,那么问题就来了:看到心爱的图片想要"占为己有".怎么把图片上的水印去除呢?今天我就来教你一招利用Pytho ...

  4. 霍兰德人格分析:利用Python第三方库matplotlib绘制雷达图

    美国约翰霍普金斯大学霍兰德教授认为,个人职业兴趣特性与职业之间应有一种内在的对应关系.根据兴趣的不同,人格可分为研究型(I).艺术型(A).社会型(S).企业型(E).传统型(C).现实型(R)六个维 ...

  5. python绘制简单城市剪影图_利用Python的folium包绘制城市道路图的实现示例

    写在前面 很长一段时间内,我都在研究在线地图的开发者文档,百度地图和高德地图的开发者中心提供了丰富的在线地图服务,虽然有一定的权限限制,但不得不说,还是给我的科研工作提供了特别方便的工具,在博客前面我 ...

  6. 利用Python的turtle库绘制四叶草

    利用Python的turtle库绘制四叶草 turtle库的基本使用 turtle库属于Python的标准库,即可以直接用import导入,无需额外安装下载. 这个博主写的蛮好的,我就不献丑了嘿嘿嘿( ...

  7. 利用Python脚本给图片批量添加文字水印

    引言:本人从小白自学python,为了测试基础学习效果,增加一定的促进,想通过参加全国计算机等级考试二级python来检验基础学习情况.在学习过程中,会将该过程编写的python小程序题目在此发表,希 ...

  8. 《延禧攻略》演员大起底——利用Python+bs+pyecharts分析绘制词云和玫瑰图

    最近大火的电视剧<延禧攻略>已宣告大结局,除了剧情走心,演员的那些头花啊头饰啊,戏服也都精致无比.本文利用Python抓取互联网上演员的部分数据,分析演员的姓名.星座.身高.体重和籍贯等信 ...

  9. python显示gif图片_利用Python制作GIF图片

    #Python制作GIF图片 import imageio def create_gif(image_list, gif_name, duration = 0.1): ''' :param image ...

  10. 【Python】 将图片绘制到Excel表格中

    首先,可以发现Excel表格可以给每一个格子分别填充颜色(RGB) 而且,通过调整格子的长宽,可以调整为方形格子(相当于像素格子) 那么便可以开始大胆的创作[疑似某国际手势] 好好填一张图看看效果: ...

最新文章

  1. Python多进程编程
  2. 神策数据荣获 2017 年度商业影响力大数据领域新锐企业 TOP 10
  3. 一建机电实务教材电子版_20年一建其实并不难,官方出版:复习题集(精修),速做速提90分...
  4. 【转载】 Asp.net Mvc 3当然也不会例
  5. css中如何实现帧布局_?如何在Python中加入多个数据帧?
  6. [算法笔记]分块算法从入门到TLE
  7. 商汤科技回应“IPO推迟”:“被”IPO,还“被”推迟了
  8. 数据可视化工具的特点有哪些
  9. springboot健康饮食管理系统
  10. 小D课堂-nexus
  11. pe分区助手读不到Linux硬盘,解决WINPE下不显示硬盘的方法
  12. Postman安装与入门简单教程
  13. 自定义bt服务器,[教程]Aria2自动更新BT Tracker服务器列表的方法
  14. 投影仪芯片0.33和0.47DMD哪个好?当贝F3 Air实测体验分享
  15. 美团笔试题解2022-3-12号
  16. 百万富翁 混淆电路
  17. Android裁剪系统
  18. 手写个Tomcat雏型
  19. 转:使用Python写一个m3u8多线程下载器
  20. Linux Centos8 安装Minio开机启动并Nginx代理访问

热门文章

  1. 优维产品最佳实践:如何设计流水线?
  2. java计算机毕业设计旅游网站vue源码+系统+数据库+lw文档+mybatis+运行部署
  3. 「津津乐道播客」#345 厂长来了:我想用这家酒馆,安放对精酿的虔诚初心(下)...
  4. 三大电信运营商携号转网数据_省通信管理局会同三大运营商详解携号转网服务...
  5. 临时抱佛脚你得会吧,掌握这套Java面试题,轻松应对面试官,让您体验把完爆的感觉,重拾面试信心。直接反客为主。
  6. 《误差理论》——粗大误差
  7. 知识图谱-事件抽取综述
  8. Belmond宣布开展战略选择审议,以提升股东价值
  9. JavaScript实现二级、多级(N级)联动下拉列表框更新版(续)- 四级联动的演示
  10. Oxygen版本eclipse在JSP疯狂报错解决方案