11

I make a picture as bellow

import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams

..... i miss code for xgboost

xgb.plot_tree(clf, num_trees=2)

enter image description here

And i want to increase font size

font = {'size'   : 22}
plt.rc('font', **font)

or

plt.rcParams.update({'font.size': 32})

but font size is the same how to change font size in xgb.plot_tree?

Edward
  • 4,443
  • 16
  • 46
  • 81

2 Answers2

16
%matplotlib inline
from xgboost import plot_tree
from matplotlib.pylab import rcParams

##set up the parameters
rcParams['figure.figsize'] = 80,50

plot_tree(finalmodel, num_trees=X)

hope this will help, I think you should set up the matplotlib parameters first.

Raywho
  • 176
  • 2
  • 3
14

I created this helper function to export xgboost trees in high resolution:

def plot_tree(xgb_model, filename, rankdir='UT'):
    """
    Plot the tree in high resolution
    :param xgb_model: xgboost trained model
    :param filename: the pdf file where this is saved
    :param rankdir: direction of the tree: default Top-Down (UT), accepts:'LR' for left-to-right tree
    :return:
    """
    import xgboost as xgb
    import os
    gvz = xgb.to_graphviz(xgb_model, num_trees=xgb_model.best_iteration, rankdir=rankdir)
    _, file_extension = os.path.splitext(filename)
    format = file_extension.strip('.').lower()
    data = gvz.pipe(format=format)
    full_filename = filename
    with open(full_filename, 'wb') as f:
        f.write(data)

You can give it a try with the following calls. I prefer the 'pdf' version as it provides vector images in which you can zoom in to infinity.

plot_tree(xgb_model, 'xgboost_test_tree.pdf')
plot_tree(xgb_model, 'xgboost_test_tree.png')
plot_tree(xgb_model, 'xgboost_test_tree_LR.pdf', 'LR')
plot_tree(xgb_model, 'xgboost_test_tree_LR.png', 'LR')
Eftim
  • 151
  • 1
  • 3
  • 1
    Nice code, would make a good enhance contribution if you submit it to xgboost package. Except the `num_trees=xgb_model.best_iteration` seems wrong; `best_iteration` is not the number of a particular tree, but of the optimal no, of training rounds, per [this](https://stackoverflow.com/questions/43534219/xgboost-what-is-the-difference-among-bst-best-score-bst-best-iteration-and-bst). You might as well just default num_trees to 0, for want of no proper ability to figure out the 'best' individual tree. – smci Mar 02 '20 at 10:21
  • This is really a great answer. Thank you so much – freddy888 Mar 14 '20 at 18:12