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可视化动态绘制八种排序过程
前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...
随机推荐
- 关于Promise:你可能不知道的6件事
FROM ME : 文章介绍了6个Promise的知识点: 1.then() 返回一个 forked Promise(分叉的 Promise):返回的有两种情况: 2.回调函数应该传递结果:在 pro ...
- php中发送email
一.使用PHP内置的mail()函数 看了一下手册,就直接开始写代码了,如下 <?php $to = "test@163.com"; //收件人 $subject = &qu ...
- Raspberry Pi 配置笔记二
配置源 http://blog.chinaunix.net/uid-21658993-id-4702322.html deb http://ipv4.mirrors.ustc.edu.cn/raspb ...
- net-snmp-5.7.3配置编译安装
net-snmp-5.7.3配置编译安装 [TOC] 先看一下系统环境 o@o-pc:~/work/_snmp/net-snmp-5.7.3$ uname -a Linux o-pc 3.16.0-3 ...
- CPU时间戳获取
inline long long timt(){ long long p; int&a=*(((int*)&p)+1); __asm__ __volatile__("rdts ...
- Adapter
11-25 16:09:10.965 22791-22823/myapplication.com.myblue E/HAL: hw_get_module_by_class: lib loaded: / ...
- 网页百度地图API相关资料
百度地图API——网页URI接口.手机网页点击直接导航:js生成一个地图网页 或 直接跳转到百度导航界面 http://developer.baidu.com/map/index.php?title= ...
- IN和EXISTS的详解
从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<& ...
- SharePoint2010母版页想要的定制
查找<div id="s4-ribbonrow" class="s4-pr s4-ribbonrowhidetitle"用style="disp ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...