首页 > 解决方案 > KeyError: 1 在使用 SP_LIME 和 Lightgbm

问题描述

我正在使用SP_LIME解释基于Lightgbm模型的客户流失预测结果。使用 LIME explainer.explain_instance 可以正常工作。

当我在同一个数据集上尝试 SP_LIME 时,第一部分 sp_obj = submodular_pick.SubmodularPick(explainer, data_source[model_lgbm.feature_name()].values, prob, num_features=5,num_exps_desired=10) 也可以正常工作。我变成了以下结果:

Intercept -0.017232899377758105
Prediction_local [0.50051062]
Right: 0.9748700776391315
Intercept -0.016903088945780256
Prediction_local [0.28478504]
Right: 0.5419652445350156

在绘制代码后出现以下错误:

[exp.as_pyplot_figure(label=1) for exp in sp_obj.sp_explanations]

KeyError                                  Traceback (most recent call last)
<ipython-input-140-cf85e71256d4> in <module>
----> 1 [exp.as_pyplot_figure(label=1) for exp in sp_obj.sp_explanations]

<ipython-input-140-cf85e71256d4> in <listcomp>(.0)
----> 1 [exp.as_pyplot_figure(label=1) for exp in sp_obj.sp_explanations]

~\AppData\Local\Continuum\anaconda3\envs\use-case\lib\site-packages\lime\explanation.py in as_pyplot_figure(self, label, **kwargs)
    167         """
    168         import matplotlib.pyplot as plt
--> 169         exp = self.as_list(label=label, **kwargs)
    170         fig = plt.figure()
    171         vals = [x[1] for x in exp]

~\AppData\Local\Continuum\anaconda3\envs\use-case\lib\site-packages\lime\explanation.py in as_list(self, label, **kwargs)
    141         """
    142         label_to_use = label if self.mode == "classification" else self.dummy_label
--> 143         ans = self.domain_mapper.map_exp_ids(self.local_exp[label_to_use], **kwargs)
    144         ans = [(x[0], float(x[1])) for x in ans]
    145         return ans

KeyError: 1 ```

Any idea what is going on here?

标签: pythonlime

解决方案


将标签更改10[exp.as_pyplot_figure(label=0) for exp in sp_obj.sp_explanations]


推荐阅读