gbdt可视化
gbdt的最大优点,和决策树一样,高度可解释,最喜欢的分类模型:)

#!/usr/bin/env python
#coding=gbk
# ==============================================================================
# \file print-fastreank-tree.py
# \author chenghuige
# \date 2014-10-04 00:34:59.825146
# \Description
# ==============================================================================
import sys,os
from gflags import *
from gezi import *
from libmelt import *
from BinaryTree import *
from TreeWriter import *
DEFINE_string('model', './model/model.json', '')
DEFINE_string('feature', '', '')
DEFINE_integer('tree', 0, '-1 means print all trees')
DEFINE_boolean('use_invisable_node', False, '')
DEFINE_string('outfile', 'tree.png', '')
def get_tree_(node_idx, fe, tree, fnames, is_inpath):
node = Node()
node.attr = {'color' : ".7 .3 1.0", 'style' : 'filled'}
node.leftEdgeAttr = {'color' : 'blue', 'penwidth' : '2.5', 'label' : '<='}
node.rightEdgeAttr = {'color' : 'green', 'penwidth' : '2.5', 'label' : '>'}
if is_inpath:
node.attr['color'] = '#40e0d0'
if node_idx < 0:
node.attr['shape'] = 'box'
node.attr['label'] = str(tree.leafValue[-node_idx - 1])
if is_inpath:
print node.attr['label']
return node
name = fnames[tree.splitFeature[node_idx]]
label = '%s\l%f <= %f?\l[%f]'%(name, fe[tree.splitFeature[node_idx]], tree.threshold[node_idx], tree.previousLeafValue[node_idx])
node.attr['label'] = label
if is_inpath:
l = fe[tree.splitFeature[node_idx]] <= tree.threshold[node_idx]
r = 1 - l
if l:
node.leftEdgeAttr['color'] = 'black'
else:
node.rightEdgeAttr['color'] = 'black'
else:
l = r = 0
node.left = get_tree_(tree.lteChild[node_idx], fe, tree, fnames, l)
node.right = get_tree_(tree.gtChild[node_idx], fe, tree, fnames, r)
return node
def get_tree(model, fe, index):
tree = model.trees[index]
fnames = model.Predictor.featureNames
btree = BinaryTree()
node_idx = 0
btree.root = get_tree_(node_idx, fe, tree, fnames, 1)
return btree
def main(argv):
try:
argv = FLAGS(argv) # parse flags
except gflags.FlagsError, e:
print '%s\nUsage: %s ARGS\n%s' % (e, sys.argv[0], FLAGS)
sys.exit(1)
model = jsonfile2obj(FLAGS.model)
fe = Vector(FLAGS.feature)
tree = get_tree(model, fe, FLAGS.tree)
writer = TreeWriter(tree)
if FLAGS.use_invisable_node:
writer.use_invisable_node = True
writer.Write(FLAGS.outfile)
if __name__ == "__main__":
main(sys.argv)
#!/usr/bin/env python
#coding=gbk
# ==============================================================================
# \file TreeWriter.py
# \author chenghuige
# \date 2014-10-02 20:32:25.744069
# \Description
# ==============================================================================
import sys
from BinaryTree import *
import pygraphviz as pgv
'''
treeWriter with func wite can write a binary tree to tree.png or user spcified
file
'''
class TreeWriter():
def __init__(self, tree):
self.num = 1 #mark each visible node as its key
self.num2 = -1 #makk each invisible node as its key
self.tree = tree
self.use_invisable_node = False
def Write(self, outfile = 'tree.png'):
def writeHelp(root, A):
if not root:
return
p = str(self.num)
self.num += 1
A.add_node(p, **root.attr)
q = None
r = None
if root.left:
q = writeHelp(root.left, A)
A.add_edge(p, q, **root.leftEdgeAttr)
if root.right:
r = writeHelp(root.right, A)
A.add_edge(p, r, **root.rightEdgeAttr)
if not self.use_invisable_node:
return p
if q or r:
if not q:
q = str(self.num2)
self.num2 -= 1
A.add_node(q, style = 'invis')
A.add_edge(p, q, style = 'invis')
if not r:
r = str(self.num2)
self.num2 -= 1
A.add_node(r, style = 'invis')
A.add_edge(p, r, style = 'invis')
l = str(self.num2)
self.num2 -= 1
A.add_node(l, style = 'invis')
A.add_edge(p, l, style = 'invis')
B = A.add_subgraph([q, l, r], rank = 'same')
B.add_edge(q, l, style = 'invis')
B.add_edge(l, r, style = 'invis')
return p #return key root node
self.A = pgv.AGraph(directed=True,strict=True)
writeHelp(self.tree.root, self.A)
self.A.graph_attr['epsilon']='0.001'
#self.A.layout(prog='dot')
#print self.A.string() # print dot file to standard output
self.A.layout('dot') # layout with dot
self.A.draw(outfile) # write to file
if __name__ == '__main__':
tree = BinaryTree()
tree.CreateTree(-1)
tree.InorderTravel()
writer = TreeWriter(tree)
if len(sys.argv) > 1:
outfile = sys.argv[1]
writer.Write(outfile) #write result to outfile
else:
writer.Write() #write result to tree.png
gbdt可视化的更多相关文章
- 笔记︱决策树族——梯度提升树(GBDT)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记来源于CDA DSC,L2-R语言课程所 ...
- RF, GBDT, XGB区别
GBDT与XGB区别 1. 传统GBDT以CART作为基分类器,xgboost还支持线性分类器(gblinear),这个时候xgboost相当于带L1和L2正则化项的逻辑斯蒂回归(分类问题)或者线性回 ...
- 决策树(中)-集成学习、RF、AdaBoost、Boost Tree、GBDT
参考资料(要是对于本文的理解不够透彻,必须将以下博客认知阅读): 1. https://zhuanlan.zhihu.com/p/86263786 2.https://blog.csdn.net/li ...
- 机器学习入门:极度舒适的GBDT原理拆解
机器学习入门:极度舒适的GBDT拆解 本文旨用小例子+可视化的方式拆解GBDT原理中的每个步骤,使大家可以彻底理解GBDT Boosting→Gradient Boosting Boosting是集成 ...
- 机器学习系列:LightGBM 可视化调参
大家好,在100天搞定机器学习|Day63 彻底掌握 LightGBM一文中,我介绍了LightGBM 的模型原理和一个极简实例.最近我发现Huggingface与Streamlit好像更配,所以就开 ...
- iOS可视化动态绘制连通图
上篇博客<iOS可视化动态绘制八种排序过程>可视化了一下一些排序的过程,本篇博客就来聊聊图的东西.在之前的博客中详细的讲过图的相关内容,比如<图的物理存储结构与深搜.广搜>.当 ...
- 发布:.NET开发人员必备的可视化调试工具(你值的拥有)
1:如何使用 1:点击下载:.NET可视化调试工具 (更新于2016-12-29 19:11:00) (终于彻底兼容了部分VS环境下无法使用的问题) 2:解压RAR后执行:CYQ.VisualierS ...
- Webstorm+Webpack+echarts构建个性化定制的数据可视化图表&&两个echarts详细教程(柱状图,南丁格尔图)
Webstorm+Webpack+echarts ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ...
- iOS可视化动态绘制八种排序过程
前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...
随机推荐
- am335x 电容屏驱动添加。
参考:http://www.cnblogs.com/helloworldtoyou/p/5530422.html 上面可以下载驱动. 解压后驱动有如下目录: 我们要选择的是: eGTouchARM/e ...
- 我的Vim配置(自动补全/树形文件浏览)
配置文件的下载路径在这里 http://files.cnblogs.com/files/oloroso/vim.configure.xz.gz 这实际上是一个 xz 格式的文件,添加的 gz 文件后 ...
- Java WebClient 总结
private WebClient getAWebClient() { WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); ...
- 【leetcode】N-Queens
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- C#之数据分页
方法一:临时datatable 创建临时表,临时变量 DataTable dt = null; //临时表 ; //总分页数 ; //当前页数 ; //每页的数量 加载数据到临时表,该方法测试放到了窗 ...
- 如何使用shell脚本快速排序和去重文件数据
前面写过一篇通过shell脚本去重10G数据的文章,见<用几条shell命令快速去重10G数据>.然而今天又碰到另外一个业务,业务复杂度比上次的单纯去重要复杂很多.找了很久没有找到相应的办 ...
- cxTreeList 控件说明
树.cxTreeList 属性: Align:布局,靠左,靠右,居中等 AlignWithMargins:带边框的布局 Anchors:停靠 (akTop上,akBottom下,akLeft左,akR ...
- Django~queries
API queries create, retrieve, update and delete
- nyoj756_重建二叉树_先序遍历
重建二叉树 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数 ...
- zsh 通信脚本
server #!/bin/zsh #zsh TCP server script zmodload zsh/net/tcp #listening port ztcp -l #This is a fil ...