Sagemaker 中 XGBoost 的功能重要性
Feature Importance for XGBoost in Sagemaker
我已经使用 Amazon Sagemaker 构建了一个 XGBoost 模型,但我找不到任何可以帮助我解释模型并验证它是否学习了正确依赖项的东西。
通常,我们可以通过 python API (https://xgboost.readthedocs.io/en/latest/python/python_api.html) I see nothing of that sort in the sagemaker api(https://sagemaker.readthedocs.io/en/stable/estimators.html) 中的 get_fscore() 函数查看 XGBoost 的特征重要性。
我知道我可以构建自己的模型,然后使用 sagemaker 进行部署,但我很好奇是否有人遇到过这个问题以及他们是如何克服它的。
谢谢。
SageMaker XGBoost 目前不提供从模型中检索特征重要性的接口。您可以编写一些代码来从 XGBoost 模型中获取特征重要性。您必须从 S3 中的模型中获取助推器对象工件,然后使用以下代码段
import pickle as pkl
import xgboost
booster = pkl.load(open(model_file, 'rb'))
booster.get_score()
booster.get_fscore()
请参考 XGBoost doc 以了解从 Booster 对象获取特征重要性的方法,例如 get_score()
或 get_fscore()
。
截至 2019 年 6 月 17 日,Sagemaker XGBoost 模型以名为 model.tar.gz
的存档形式存储在 S3 上。此存档由名为 xgboost-model
的单个腌制模型文件组成。
不下载直接从S3加载模型,可以使用如下代码:
import s3fs
import pickle
import tarfile
import xgboost
model_path = 's3://<bucket>/<path_to_model_dir>/xgboost-2019-06-16-09-56-39-854/output/model.tar.gz'
fs = s3fs.S3FileSystem()
with fs.open(model_path, 'rb') as f:
with tarfile.open(fileobj=f, mode='r') as tar_f:
with tar_f.extractfile('xgboost-model') as extracted_f:
xgbooster = pickle.load(extracted_f)
xgbooster.get_fscore()
尽管您可以像 rajesh 和 Lukas 建议的那样编写自定义脚本并使用 XGBoost 作为 运行 脚本的框架(参见 How to Use Amazon SageMaker XGBoost for how to use the "script mode"), SageMaker has recently launched SageMaker Debugger,它允许您从 XGBoost 中实际检索特征重要性时间.
Notebook demonstrates how to use SageMaker Debugger to retrieve feature importance.
我已经使用 Amazon Sagemaker 构建了一个 XGBoost 模型,但我找不到任何可以帮助我解释模型并验证它是否学习了正确依赖项的东西。
通常,我们可以通过 python API (https://xgboost.readthedocs.io/en/latest/python/python_api.html) I see nothing of that sort in the sagemaker api(https://sagemaker.readthedocs.io/en/stable/estimators.html) 中的 get_fscore() 函数查看 XGBoost 的特征重要性。
我知道我可以构建自己的模型,然后使用 sagemaker 进行部署,但我很好奇是否有人遇到过这个问题以及他们是如何克服它的。
谢谢。
SageMaker XGBoost 目前不提供从模型中检索特征重要性的接口。您可以编写一些代码来从 XGBoost 模型中获取特征重要性。您必须从 S3 中的模型中获取助推器对象工件,然后使用以下代码段
import pickle as pkl
import xgboost
booster = pkl.load(open(model_file, 'rb'))
booster.get_score()
booster.get_fscore()
请参考 XGBoost doc 以了解从 Booster 对象获取特征重要性的方法,例如 get_score()
或 get_fscore()
。
截至 2019 年 6 月 17 日,Sagemaker XGBoost 模型以名为 model.tar.gz
的存档形式存储在 S3 上。此存档由名为 xgboost-model
的单个腌制模型文件组成。
不下载直接从S3加载模型,可以使用如下代码:
import s3fs
import pickle
import tarfile
import xgboost
model_path = 's3://<bucket>/<path_to_model_dir>/xgboost-2019-06-16-09-56-39-854/output/model.tar.gz'
fs = s3fs.S3FileSystem()
with fs.open(model_path, 'rb') as f:
with tarfile.open(fileobj=f, mode='r') as tar_f:
with tar_f.extractfile('xgboost-model') as extracted_f:
xgbooster = pickle.load(extracted_f)
xgbooster.get_fscore()
尽管您可以像 rajesh 和 Lukas 建议的那样编写自定义脚本并使用 XGBoost 作为 运行 脚本的框架(参见 How to Use Amazon SageMaker XGBoost for how to use the "script mode"), SageMaker has recently launched SageMaker Debugger,它允许您从 XGBoost 中实际检索特征重要性时间.
Notebook demonstrates how to use SageMaker Debugger to retrieve feature importance.