首页 > 解决方案 > 如何将 xgboost 的特征重要性图保存到 Jupyter 笔记本中的文件中

问题描述

我正在努力将 xgboost 功能重要性图保存到文件中。我在我的 jupyter 笔记本中创建了一个模型并绘制了功能的重要性-

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

它向我展示了特征重要性图,但我无法将其保存到文件中。我什至在 中寻找任何保存属性dir(xgboost.plot_importance(xgb_model)),但一无所获。有没有办法做到这一点?

标签: pythonjupyter-notebookxgboost

解决方案


根据文档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')

推荐阅读