juyter显示决策树图形_关于决策树可视化的treePlotter(学习笔记)
网上的版本好像好久都没更新了treePlotter是没有人用了么。今天学习的时候发现有些地方已经改了,我改的是在python 3.6 上的运行版本,需要导入matplotlib.pyplot
import matplotlib.pyplot as plt
# 定义决策树决策结果属性
descisionNode = dict(boxstyle=‘sawtooth‘, fc=‘0.8‘)
leafNode = dict(boxstyle=‘round4‘, fc=‘0.8‘)
arrow_args = dict(arrowstyle=‘
# myTree = {‘no surfacing‘: {0: ‘no‘, 1: {‘flippers‘: {0: ‘no‘, 1: ‘yes‘}}}}
def plotNode(nodeTxt, centerPt, parentPt, nodeType):
# nodeTxt为要显示的文本,centerNode为文本中心点, nodeType为箭头所在的点, parentPt为指向文本的点
createPlot.ax1.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.ax1为全局变量,绘制图像句柄
# # frameon表示是否绘制坐标轴矩形
# createPlot.ax1 = plt.subplot(111, frameon=False)
# plotNode(‘a decision node‘, (0.5, 0.1), (0.1, 0.5), descisionNode)
# plotNode(‘a leaf node‘, (0.8, 0.1), (0.3, 0.8), leafNode)
# plt.show()
# 这个是用来测试的
# -----------分割线-------------
# 获取树的叶子数和树的深度
def getNumLeafs(myTree):
numLeafs = 0
firstStr = list(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 numLeafs
def getTreeDepth(myTree):
maxDepth = 0
firstStr = list(myTree.keys())[0] # 这个是改的地方,原来myTree.keys()返回的是dict_keys类,不是列表,运行会报错。有好几个地方这样
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 = thisDepth
return maxDepth
# ---------分割线-------------
# 制图
def createPlot(inTree):
fig = plt.figure(1, facecolor=‘white‘)
fig.clf()
axprops = {‘xticks‘: None, ‘yticks‘: None}
createPlot.ax1 = plt.subplot(111, frameon=False)
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 plotTree(myTree, parentPt, nodeTxt):
numLeafs = getNumLeafs(myTree)
depth = getTreeDepth(myTree)
firstStr = list(myTree.keys())[0]
# cntrPt文本中心点, parentPt指向文本中心的点
cntrPt = (plotTree.xOff + (1.0 + float(numLeafs))/2.0/plotTree.totalW, plotTree.yOff)
plotMidText(cntrPt, parentPt, nodeTxt)
plotNode(firstStr, cntrPt, parentPt, descisionNode)
seconDict = myTree[firstStr]
plotTree.yOff = plotTree.yOff - 1.0/plotTree.totalD
for key in seconDict.keys():
if type(seconDict[key]).__name__ == ‘dict‘:
plotTree(seconDict[key], cntrPt, str(key))
else:
plotTree.xOff = plotTree.xOff + 1.0/plotTree.totalW
plotNode(seconDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)
plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
plotTree.yOff = plotTree.yOff + 1.0/plotTree.totalD
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)
# createPlot(myTree)
这个treePlotter导入了就可以把原来得到的决策树模型导入啦,而且要注意是以字典形式导入,所以保存和导入文件的时候最好用json。
发布5分钟之后,突然发现已经有人改过了,那就只算是个学习笔记吧 - -
juyter显示决策树图形_关于决策树可视化的treePlotter(学习笔记)相关推荐
- python可视化:matplotlib学习笔记
信息可视化是数据分析的一块重要内容.这是一个探索性的过程.比如说,可以帮助我们坚定离群值,或者必要的数据转换,又或者是构建一个理想的模型.对于其他的一些领域,也可以进行web可视化.Python有许多 ...
- 七月在线python数据分析_七月在线Python数据分析-入门学习笔记
Python是近年来比较热门的数据分析编程语言,个人本身做了几年的传统BI,一个偶然的机会从公众号上获得一元就可以学习Python课程的机会,于是抽时间在网上学习了,以下是入门课程的学习笔记,和大家分 ...
- python plt包_Python 数据可视化-Matplotlib包学习笔记(一)
Python 数据可视化 本文主要参照Matplotlib的官方教程进行整理,作为个人的学习笔记进行分享,欢迎相互讨论. Matplotlib包学习笔记(一) 本文主要简单介绍一下Matplotlib ...
- python 网页爬虫作业调度_第3次作业-MOOC学习笔记:Python网络爬虫与信息提取
1.注册中国大学MOOC 2.选择北京理工大学嵩天老师的<Python网络爬虫与信息提取>MOOC课程 3.学习完成第0周至第4周的课程内容,并完成各周作业. 4.提供图片或网站显示的学习 ...
- juyter显示决策树图形_决策树分析细分市场
一,前言 决策树就是利用图形来表述某项决策的本质,展现了各种选择和不确定因素之间的相互关系.决策树是一个利用像树一样的图形或决策模型的决策支持工具,包括随机事件结果,资源代价和实用性.它是一个算法显示 ...
- juyter显示决策树图形_在Jupyter Noteb中绘制交互式决策树
在Jupyter笔记本中使用d3js更新了可折叠图的答案 笔记本第一个单元格的开始%%html .node circle { cursor: pointer; stroke: #3182bd; str ...
- python绘制八角图形_国内数据可视化公司
数据是非常强大的.当然,如果你能真正理解它想告诉你的内容,那它的强大之处就更能体现出来了. 通过观察数字和统计数据的转换以获得清晰的结论并不是一件容易的事.必须用一个合乎逻辑的.易于理解的方式来呈现数 ...
- pycharm显示全部数据_【数据可视化】BI工程师岗位分析
BI工程师岗位分析(参考网址:https://www.5xiaobo.com/?id=694) 一.数据爬取 1. 数据爬取我使用的软件是pycharm.(像jupyter notebook其实更方便 ...
- 决策树(chap3)Machine Learning In Action学习笔记
优点:计算复杂度不高,输出结果易于理解,对中间值的缺失不敏感,可以处理不相关特征数据. 缺点:可能会产生过度匹配问题. 适用数据类型:数值型(必须离散化)和标称型. 决策树创建分支的伪代码函数crea ...
最新文章
- 利用bigpipe机制实现页面模块的异步渲染 chunked技术
- Django博客系统(忘记密码)
- ubuntu下解决Ruby安装后缺少openssl的问题
- windows terminal 笔记
- 图像滤波 Image Filtering
- boost::icl模块interval_map 的最简单示例是重叠计数器
- File类的构造方法
- python中os模块里有哪些类_python-os模块的常用方法
- 4 5区别 angular 和_Angular 常见问题解答
- Spring MVC 数据回显
- JQuery easyui (1) Draggable(拖动)组件
- 妙用javascript
- ffmpeg音频转换命令
- 难得有一篇文章来仔细梳理cowboy bebop的种种.
- 抽奖活动mysql表设计_抽奖项目的系统设计方案
- 本周最新文献速递20211219
- python-decouple简介
- 李清照(诗词介绍收藏)
- 京东面试官:给我说说你简历上的订单系统是如何设计的?尽量详细点~
- hbuilder边框代码是什么_HBuilder CSS 自定义代码块
热门文章
- 给html元素绑定单击和双击事件
- CMU Database Systems - Concurrency Control Theory
- 【MVC】使用FormCollection获取Form表单数据
- Numpy:通过算数运算取值、替换值
- Cause: java.sql.SQLException: Could not retrieve transation read-only status server
- 【计算机基础】 经常使用的排序算法的时间复杂度和空间复杂度
- Android实习结束后的阶段性总结
- ASP.NET 4.0 来了
- HTML5 canvas 模拟事件
- easyUI表单验证扩展