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