如何从Jupyter笔记本将XGBoost的特征重要性图保存到文件

6

我在尝试将xgboost特征重要性图保存到文件中遇到了困难。我已经在jupyter notebook中创建了一个模型并绘制了特征的重要性-

xgb_model = xgboost.train(best_params, dtrain, num_round)
xgboost.plot_importance(xgb_model)

它展示了我特征重要性图,但是我不能将其保存到文件中。我甚至在dir(xgboost.plot_importance(xgb_model))中寻找任何save属性,但没有得到任何东西。有没有什么方法可以做到这一点?

2个回答

10
根据文档xgboost.plot_importance(xgb_model)返回matplotlib Axes
因此,您只需
ax = xgboost.plot_importance(xgb_model)
ax.figure.savefig('the-path-you-want-to-save.png')

如果您的图像失去了左右边距,可以使用tight_layout来设置。

ax = xgboost.plot_importance(xgb_model)
ax.figure.tight_layout()
ax.figure.savefig('the-path-you-want-to-save.png')

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接