xgb.plot_tree 字体大小 python

xgb.plot_tree font size python

我做了如下图

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

..... 我错过了 xgboost

的代码
xgb.plot_tree(clf, num_trees=2)

而且我想增加字体大小

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

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

但是字体大小是一样的 如何更改 xgb.plot_tree 中的字体大小?

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

希望这会有所帮助,我认为你应该先设置 matplotlib 参数。

我创建了这个辅助函数来导出高分辨率的 xgboost 树:

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)

您可以通过以下调用尝试一下。我更喜欢 'pdf' 版本,因为它提供了可以放大到无穷大的矢量图像。

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')