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. Oracle 10G select工作原理

    数据库查询语句内部执行过程 select * from  table 步骤 分析阶段(parse) 1.共享池库高速缓存有没有该语句.如果有直接返回结果. 2.语法分析sql语句是否正确进行下一步分析 ...

  2. split() 注意事项.

    split(): 当函数默认为空,它会把所有空格(空格符.制表符.换行符)当作分隔符. 但是当函数参数默认为其他,将会对 空格符.制表符.换行符 当作元素进行分割. a = 'hello world! ...

  3. cf555b

    题意:按顺序给出多个互不相交的区间(表示一些小岛),和一些可以连接区间的桥,每个桥有固定的长度.区间和桥的数量都是2*10^5. 两个相邻的小岛之间的桥的长度必须小于等于最远点距离,大于等于最近点距离 ...

  4. 在linux环境编译boost

    1.在boost官网:http://www.boost.org/下载相应版本的boost 2.解压boost到相应目录,在boost跟目录下有b2可执行程序,可以通过输入命令“/b2 --help”, ...

  5. MongoDB 副本集管理(不定时更新)

    简介: 前面介绍完了副本集的搭建.用户的管理.参数和日常操作的说明,那副本集搭建好该如何管理呢?现在来说明下副本集的日常查看和管理. 说明: 1)查看命令行参数:db.serverCmdLineOpt ...

  6. NoSQL之【MongoDB】学习(三):配置文件说明

    摘要: 继上一篇NoSQL之[MongoDB]学习(一):安装说明 之后,知道了如何安装和启动MongoDB,现在对启动时指定的配置文件(mongodb.conf)进行说明,详情请见官方. 启动Mon ...

  7. ASM:《X86汇编语言-从实模式到保护模式》第七章应用例:用adc命令计算1到1000的累加

    在16位的处理器上,做加法的指令是add,但是他每次只能做8位或者16位的加法,除此之外,还有一个带进位的加法指令adc(Add With Carry),他的指令格式和add一样,目的操作数可以是8位 ...

  8. 【leetcode】Flatten Binary Tree to Linked List (middle)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  9. LeetCode 217 Contains Duplicate

    Problem: Given an array of integers, find if the array contains any duplicates. Your function should ...

  10. 无限轮播的N+2 策略

    N张照片把contentsSize设置为N+2个图片的宽度,例子如下,两端填充如图,当处于一端时,且即将进入循环状态的时候,如第二张图,从状态1滑动到状态2,在滑动结束的时候,将当前的位置直接转到状态 ...