Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xgb.plot_tree font size python

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?

like image 478
Edward Avatar asked May 20 '16 07:05

Edward


People also ask

How to change font sizes on a Matplotlib plot?

How to Change Font Sizes on a Matplotlib Plot. Often you may want to change the font sizes of various elements on a Matplotlib plot. Fortunately this is easy to do using the following code: import matplotlib.pyplot as plt plt.rc('font', size=10) #controls default text size plt.rc('axes', titlesize=10) #fontsize of the title plt.rc('axes', ...

How to change the size of a plot in Python?

While making a plot it is important for us to optimize its size. Here are various ways to change the default plot size as per our required dimensions or resize a given plot. For changing height and width of a plot set_figheight and set_figwidth are used figsize () takes two parameters- width and height (in inches).

How to plot the output tree via Matplotlib in Python?

To plot the output tree via matplotlib, use xgboost.plot_tree (), specifying the ordinal number of the target tree. This function requires graphviz and matplotlib. When you use IPython, you can use the xgboost.to_graphviz () function, which converts the target tree to a graphviz instance.

How to set the font size of labels in Python?

These three methods are: 1 fontsize in plt.xticks/plt.yticks () 2 fontsize in ax.set_yticklabels/ax.set_xticklabels () 3 labelsize in ax.tick_params () More ...


2 Answers

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')
like image 143
Eftim Avatar answered Sep 17 '22 22:09

Eftim


%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.

like image 34
Raywho Avatar answered Sep 19 '22 22:09

Raywho