更改xgb.plot_tree()中的字体大小

问题描述 投票:8回答:2

如何更改xgb.plot_tree()图中的字体大小?

我作图如下:

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

# ...omitting code for training the xgboost model...

xgb.plot_tree(clf, num_trees=2)

enter image description here

并且我尝试增加字体大小:

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

或带有:

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

但是字体大小是相同的。如何在xgb.plot_tree()中更改字体大小?

python plot fonts xgboost
2个回答
12
投票
希望这会有所帮助,我认为您应该首先设置matplotlib参数。

5
投票
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')
© www.soinside.com 2019 - 2024. All rights reserved.