4

我已经使用 Amazon Sagemaker 构建了一个 XGBoost 模型,但是我找不到任何可以帮助我解释模型并验证它是否学习了正确的依赖关系的东西。

通常,我们可以通过 python API ( https://xgboost.readthedocs.io/en/latest/python/python_api.html )中的 get_fscore() 函数看到 XGBoost 的功能重要性我在 sagemaker api 中看不到这种类型(https://sagemaker.readthedocs.io/en/stable/estimators.html)。

我知道我可以构建自己的模型,然后使用 sagemaker 进行部署,但我很好奇是否有人遇到过这个问题以及他们是如何克服它的。

谢谢。

4

3 回答 3

5

截至 2019 年 6 月 17 日,Sagemaker XGBoost 模型存储在 S3 上,名为model.tar.gz. 该档案由单个腌制模型文件组成,名为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()
于 2019-06-17T10:47:39.190 回答
3

SageMaker XGBoost 目前不提供从模型中检索特征重要性的接口。您可以编写一些代码来从 XGBoost 模型中获取特征重要性。您必须从 S3 中的模型中获取助推器对象工件,然后使用以下代码段

import pickle as pkl
import xgboost
booster = pkl.load(open(model_file, 'rb'))
booster.get_score()
booster.get_fscore()

有关从 Booster 对象(例如或)获取特征重要性的方法,请参阅XGBoost 文档get_score()get_fscore()

于 2019-04-11T16:54:55.857 回答
3

尽管您可以像 rajesh 和 Lukas 建议的那样编写自定义脚本并使用 XGBoost 作为框架来运行脚本(请参阅如何使用 Amazon SageMaker XGBoost了解如何使用“脚本模式”),SageMaker 最近推出了SageMaker Debugger,它允许您可以实时从 XGBoost 中检索特征重要性。

Notebook 演示了如何使用 SageMaker Debugger 检索功能重要性

于 2020-02-03T23:29:24.037 回答