eli5 show_prediction 未显示概率

eli5 show_prediction not showing probability

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

下面是一个使用 public 数据集的完全可重现的示例。

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 树来解释预测。看来我得到的 "score" 实际上只是所有特征贡献的总和,就像我期望的那样,如果 eli5 实际上不是遍历树而是拟合线性模型。真的吗?我怎样才能让eli5也遍历树?

解决了我自己的问题。根据 this Github Issue,eli5 仅支持旧版本的 XGBoost (<=0.6)。我使用的是 XGBoost 0.80 版和 eli5 0.8 版。

发布问题的解决方案:

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

然后将我问题的代码片段的最后一行替换为:

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

解决了我的问题。