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. Android学习笔记(二)——探究一个活动

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件,主要用于和用户进行交互.一 ...

  2. NOIP“对偶”题:还教室

    先说一下思路: 方差可以经过恒等变形变成 x12 + x22 + ... + xn2 + 2a(x1 + x2 + ... + xn) + na2 所以维护平方和.连续和即可 平均数我就不再推了…… ...

  3. 汉诺塔(河内塔)算法 ----C语言递归实现

    汉诺塔:汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子, 在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘.大梵天命令婆罗门把圆盘从下面开始按大小顺 ...

  4. 关于在android 4.2.2 上运行runlmbench

    在剑锋的基础上加一些自己笔记,让自己更懂一些这个流程. 参考:http://www.cnblogs.com/zengjfgit/p/5731655.html runlmbench 是一款在linux ...

  5. Ubuntu+Apache+PHP+Mysql环境搭建

    一.操作系统Ubuntu 14.04 64位,虚拟机服务器 二.Apache 1.安装Apache,安装命令:sudo apt-get install apache2 2.环境配置: 1)配置文件:路 ...

  6. 2.7---判断链表是否是回文(CC150)

    注意,如果用的方法是翻转整个链表,那么链表都被改变了.就无法做了. 此外注意fast.next.next!= null;不然也会报错.要保证后面的比前面的少. 答案: public static bo ...

  7. 【云计算】qcow2虚拟磁盘映像转化为vmdk

    Software QEMU emulator version 1.7.0 Introduction Since we use VmWare in my company, I started to pl ...

  8. VS中计算程序运行时间

    VS中计算程序运行的时间   http://bbs.csdn.net/topics/39068881 有时候在设计程序完了之后需要计算程序运行的时间. 这时候可以使用Windows的库函数 GetIi ...

  9. IOS - NSURLSession

    NSURLSession是iOS7中新的网络接口,它与咱们熟悉的NSURLConnection是并列的.在程序在前台时,NSURLSession与NSURLConnection可以互为替代工作.注意, ...

  10. 如何在MyEclipse中通过hibernate使用jtds驱动连接数据库,并向数据库添加数据的方法

    最近学习了下如何在MyEclipse中通过hibernate使用jtds驱动连接数据库,并向数据库添加数据的方法,虽然MyEclipse中自带了连接数据库的方法,我也尝试了下其他方法,如有不当之处请指 ...