【问题标题】:eli5 show_prediction not showing probabilityeli5 show_prediction 未显示概率
【发布时间】:2019-05-15 23:03:56
【问题描述】:

我正在使用 eli5 包中的 show_prediction 函数来了解我的 XGBoost 分类器是如何得出预测的。出于某种原因,我似乎得到的是回归分数,而不是我的模型的概率。

以下是一个完全可重现的公共数据集示例。

from sklearn.datasets import load_breast_cancer
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from eli5 import show_prediction

# Load dataset
data = load_breast_cancer()

# Organize our data
label_names = data['target_names']
labels = data['target']
feature_names = data['feature_names']
features = data['data']


# Split the data
train, test, train_labels, test_labels = train_test_split(
    features,
    labels,
    test_size=0.33,
    random_state=42
)

# Define the model
xgb_model = XGBClassifier(
    n_jobs=16,
    eval_metric='auc'
)

# Train the model
xgb_model.fit(
    train,
    train_labels
)

show_prediction(xgb_model.get_booster(), test[0], show_feature_values=True, feature_names=feature_names)

这给了我以下结果。注意3.7的分数,这绝对不是概率。

虽然官方 eli5 documentation 正确显示了概率。

失踪概率似乎与我使用xgb_model.get_booster()有关。看起来官方文档没有使用它并按原样传递模型,但是当我这样做时,我得到TypeError: 'str' object is not callable,所以这似乎不是一个选项。

我还担心 eli5 没有通过遍历 xgboost 树来解释预测。看来我得到的“分数”实际上只是所有特征贡献的总和,就像我所期望的那样,如果 eli5 实际上没有遍历树而是拟合线性模型。真的吗?我怎样才能让 eli5 遍历树?

【问题讨论】:

    标签: python machine-learning xgboost


    【解决方案1】:

    解决了我自己的问题。根据this Github Issue eli5 仅支持旧版本的 XGBoost (

    发布问题的解决方案:

    import eli5
    from xgboost import XGBClassifier, XGBRegressor
    
    def _check_booster_args(xgb, is_regression=None):
        # type: (Any, bool) -> Tuple[Booster, bool]
        if isinstance(xgb, eli5.xgboost.Booster): # patch (from "xgb, Booster")
            booster = xgb
        else:
            booster = xgb.get_booster() # patch (from "xgb.booster()" where `booster` is now a string)
            _is_regression = isinstance(xgb, XGBRegressor)
            if is_regression is not None and is_regression != _is_regression:
                raise ValueError(
                    'Inconsistent is_regression={} passed. '
                    'You don\'t have to pass it when using scikit-learn API'
                    .format(is_regression))
            is_regression = _is_regression
        return booster, is_regression
    
    eli5.xgboost._check_booster_args = _check_booster_args
    

    然后将我的问题代码 sn-p 的最后一行替换为:

    show_prediction(xgb_model, test[0], show_feature_values=True, feature_names=feature_names)
    

    解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2021-02-18
      • 2022-01-06
      • 1970-01-01
      • 2017-06-07
      • 2014-03-05
      • 1970-01-01
      • 2021-07-15
      • 2019-05-23
      • 1970-01-01
      相关资源
      最近更新 更多