python - 如何将 xgboost 的特征重要性图保存到 Jupyter 笔记本中的文件中
问题描述
我正在努力将 xgboost 功能重要性图保存到文件中。我在我的 jupyter 笔记本中创建了一个模型并绘制了功能的重要性-
xgb_model = xgboost.train(best_params, dtrain, num_round)
xgboost.plot_importance(xgb_model)
它向我展示了特征重要性图,但我无法将其保存到文件中。我什至在 中寻找任何保存属性dir(xgboost.plot_importance(xgb_model))
,但一无所获。有没有办法做到这一点?
解决方案
根据文档,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')
推荐阅读
- graphql - Apollo GraphQL 教程:“GraphQLError:无法在类型 \"LaunchConnection\" 上查询字段 \"id\"。”
- drake - 使用 Python 序列化 Drake 对象
- python - Python 3.6 将字典转换为排序列表
- c# - WinDBG Preview - 从其他 Visual Studio 项目加载源代码
- python - 如何知道 GEKKO 花了多长时间解决我的模型?
- ios - 加载类时 UITableViewCell UIView 颜色不会立即更改
- azure - Update-AzureRmVmss、Update-AzureRmVmssInstance 和 Update-AzureRmVmssVM 有什么区别?
- azure-devops - Azure Devops - 用户故事描述/接受标准字段中的 Markdown 支持?
- oracle - Oracle APEX - 默认主键值“t1000”
- c++ - 等待 MPI-IO 在 MPI_File_open 上同步的进程旋转