• 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 = '%sl%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 Usage: %s ARGS %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

       

  • 相关阅读:
    【转】Web Service单元测试工具实例介绍之SoapUI
    【节选】刘积仁:怎样才是真正的创业者
    xadmin引入django-import-export导入功能
    django使用xadmin
    Mac Docker安装Redis4.0
    JMeter+Maven+CSV数据驱动
    Selenium+TestNG+CSV数据驱动
    JMeter压测时报“内存不足”故障的9个简单解决方案
    requests+unittest+ddt+xlrd+pymysql+BeautifulReport数据驱动
    Mac Docker安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/rocketfan/p/4006490.html
Copyright © 2020-2023  润新知