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可视化的更多相关文章

  1. 笔记︱决策树族——梯度提升树(GBDT)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 本笔记来源于CDA DSC,L2-R语言课程所 ...

  2. RF, GBDT, XGB区别

    GBDT与XGB区别 1. 传统GBDT以CART作为基分类器,xgboost还支持线性分类器(gblinear),这个时候xgboost相当于带L1和L2正则化项的逻辑斯蒂回归(分类问题)或者线性回 ...

  3. 决策树(中)-集成学习、RF、AdaBoost、Boost Tree、GBDT

    参考资料(要是对于本文的理解不够透彻,必须将以下博客认知阅读): 1. https://zhuanlan.zhihu.com/p/86263786 2.https://blog.csdn.net/li ...

  4. 机器学习入门:极度舒适的GBDT原理拆解

    机器学习入门:极度舒适的GBDT拆解 本文旨用小例子+可视化的方式拆解GBDT原理中的每个步骤,使大家可以彻底理解GBDT Boosting→Gradient Boosting Boosting是集成 ...

  5. 机器学习系列:LightGBM 可视化调参

    大家好,在100天搞定机器学习|Day63 彻底掌握 LightGBM一文中,我介绍了LightGBM 的模型原理和一个极简实例.最近我发现Huggingface与Streamlit好像更配,所以就开 ...

  6. iOS可视化动态绘制连通图

    上篇博客<iOS可视化动态绘制八种排序过程>可视化了一下一些排序的过程,本篇博客就来聊聊图的东西.在之前的博客中详细的讲过图的相关内容,比如<图的物理存储结构与深搜.广搜>.当 ...

  7. 发布:.NET开发人员必备的可视化调试工具(你值的拥有)

    1:如何使用 1:点击下载:.NET可视化调试工具 (更新于2016-12-29 19:11:00) (终于彻底兼容了部分VS环境下无法使用的问题) 2:解压RAR后执行:CYQ.VisualierS ...

  8. Webstorm+Webpack+echarts构建个性化定制的数据可视化图表&&两个echarts详细教程(柱状图,南丁格尔图)

    Webstorm+Webpack+echarts   ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ...

  9. iOS可视化动态绘制八种排序过程

    前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...

随机推荐

  1. css弹性布局

    1.弹性布局是什么 在移动端一种方便的布局方式,打破了之前用浮动,定位的布局,更加灵活. 2.弹性布局的格式 包含父元素和子元素,有对应的属性应用在父元素和子元素达到布局的目的 3.父元素的属性 要开 ...

  2. php DI实现实例:

    <?php //DI 主要运用IoC用于解决 依赖文件共享(无需每一个依赖都手动注册) //管理应用程序中的『全局』对象(包括实例化.处理依赖关系). //可以延时加载对象(仅用到时才创建对象) ...

  3. Tomcat配置文件server.xml详解

    <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOW ...

  4. Linux查看硬盘型号

    Linux查看硬盘型号 -- :: 分类: 服务器与存储 请先确定服务器是否有配 RAID. 如果有RAID,请通过对应的RAID管理(监控)工具查看,例如LSI的MegaCli: # /opt/Me ...

  5. iOS 中 为UIView添加背景图片

    创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...

  6. SSM 集成的两个配置文件

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> < ...

  7. WPF 中更新界面信息

    1.Dispatcher.BeginInvoke int ii = 0; new Thread(new ParameterizedThreadStart((i) => { while (true ...

  8. zookeeper部署及集群测试

    zookeeper部署及集群测试 环境 三台测试机 操作系统: centos7 ; hostname: c1 ; ip: 192.168.1.80 操作系统: centos7 ; hostname: ...

  9. centos6.5 tomcat开机启动

    可参考:centos6.5 nginx开机启动 /etc/init.d/下添加tomcatd文件,内容如下: #!/bin/sh # # chkconfig: - # # Licensed to th ...

  10. XP共享连接数限制