如何将 xgboost 的特征重要性图保存到 Jupyter Notebook 的文件中
How to save feature importance plot of xgboost to a file from Jupyter notebook
我正在努力将 xgboost 特征重要性图保存到文件中。我在我的 jupyter notebook 中创建了一个模型并绘制了特征的重要性-
xgb_model = xgboost.train(best_params, dtrain, num_round)
xgboost.plot_importance(xgb_model)
它向我展示了特征重要性图,但我无法将其保存到文件中。我什至在 dir(xgboost.plot_importance(xgb_model))
中寻找任何 save 属性,但一无所获。有什么办法吗?
从 documentation 你看到它是一个 matplotlib
输出。所以你应该可以调用matplotlib的savefig
。
如果要保存模型,请查看How to save & load xgboost model?。
根据 doc, xgboost.plot_importance(xgb_model)
returns 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')
我正在努力将 xgboost 特征重要性图保存到文件中。我在我的 jupyter notebook 中创建了一个模型并绘制了特征的重要性-
xgb_model = xgboost.train(best_params, dtrain, num_round)
xgboost.plot_importance(xgb_model)
它向我展示了特征重要性图,但我无法将其保存到文件中。我什至在 dir(xgboost.plot_importance(xgb_model))
中寻找任何 save 属性,但一无所获。有什么办法吗?
从 documentation 你看到它是一个 matplotlib
输出。所以你应该可以调用matplotlib的savefig
。
如果要保存模型,请查看How to save & load xgboost model?。
根据 doc, xgboost.plot_importance(xgb_model)
returns 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')