Sagemaker 中 XGBoost 的功能重要性

dar*_*sam 4 xgboost amazon-sagemaker

我已经使用 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 部署它,但我很好奇是否有人遇到过这个问题以及他们是如何克服它的。

谢谢。

Luk*_*bel 5

截至 2019 年 6 月 17 日,Sagemaker XGBoost 模型以名为model.tar.gz. 该存档由名为 .zip 的单个腌制模型文件组成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()
Run Code Online (Sandbox Code Playgroud)


raj*_*raj 4

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

import pickle as pkl
import xgboost
booster = pkl.load(open(model_file, 'rb'))
booster.get_score()
booster.get_fscore()
Run Code Online (Sandbox Code Playgroud)

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