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

Ank*_*eth 3 python xgboost jupyter-notebook

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

xgb_model = xgboost.train(best_params, dtrain, num_round)
xgboost.plot_importance(xgb_model)
Run Code Online (Sandbox Code Playgroud)

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

And*_* Li 5

根据文档xgboost.plot_importance(xgb_model)返回matplotlib Axes

因此,你可以

ax = xgboost.plot_importance(xgb_model)
ax.figure.savefig('the-path-you-want-to-save.png')
Run Code Online (Sandbox Code Playgroud)

另外,如果您丢失了图形的左右边距,您可以设置 tight_layout

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