Skip to content

Instantly share code, notes, and snippets.

@chiral
Created December 18, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiral/d030811834a235c7dd74 to your computer and use it in GitHub Desktop.
Save chiral/d030811834a235c7dd74 to your computer and use it in GitHub Desktop.
pretty print for xgboost gbtree booster dump
import sys
import re
import unicodedata
depth={}
yn={}
f1=None
f=open(sys.argv[1])
while True:
line=f.readline().strip()
if not line: break
line = line.decode('sjis',"ignore")
line = unicodedata.normalize('NFKC',unicode(line))
m=re.match("booster\[([0-9]+)\]",line)
if m:
tree_id=int(m.group(1))+1
depth={}
yn={}
if f1: f1.close()
f1 = open("tree"+str(tree_id)+".txt","w")
else:
m=re.match("([0-9]+):",line)
if m:
id=m.group(1)
if id not in depth:
depth[id]=''
yn[id]=False
f1.write((depth[id]+line+"\n").encode("sjis"))
m=re.match("[0-9]+:.* yes=([0-9]+),no=([0-9]+)",line)
if m:
yes=m.group(1)
no=m.group(2)
depth[yes]=depth[no]=depth[id]+('| ' if yn[id] else ' ')
yn[yes]=True
yn[no]=False
f.close()
if f1: f1.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment