python - SHAP - 具有多个维度的实例

标签 python machine-learning shap

我对 SHAP 很陌生,我想尝试一下,但遇到了一些困难。

该模型已经过训练并且似乎表现良好。然后我使用训练数据来测试 SHAP。看起来像这样:

   var_Braeburn  var_Cripps Pink  var_Dazzle  var_Fuji  var_Granny Smith  \
0             1                0           0         0                 0   
1             0                1           0         0                 0   
2             0                1           0         0                 0   
3             0                1           0         0                 0   
4             0                1           0         0                 0   

   var_Other Variety  var_Royal Gala (Tenroy)  root_CG202  root_M793  \
0                  0                        0           0          0   
1                  0                        0           1          0   
2                  0                        0           1          0   
3                  0                        0           0          0   
4                  0                        0           0          0   

   root_MM106  ...  frt_BioRich Organic Compost_single  \
0           1  ...                                   0   
1           0  ...                                   0   
2           0  ...                                   0   
3           1  ...                                   0   
4           1  ...                                   0   

   frt_Biomin Boron_single  frt_Biomin Zinc_single  \
0                        0                       1   
1                        0                       0   
2                        0                       0   
3                        0                       0   
4                        0                       0   

   frt_Fertco Brimstone90 sulphur_single  frt_Fertco Guano _single  \
0                                      0                         0   
1                                      0                         0   
2                                      0                         0   
3                                      0                         0   
4                                      0                         0   

   frt_Gro Mn_multiple  frt_Gro Mn_single  frt_Organic Mag Super_multiple  \
0                    0                  0                               0   
1                    1                  0                               1   
2                    1                  0                               1   
3                    1                  0                               1   
4                    1                  0                               1   

   frt_Organic Mag Super_single  frt_Other Fertiliser  
0                             0                     0  
1                             0                     0  
2                             0                     0  
3                             0                     0  
4                             0                     0 

然后我执行explainer = shap.Explainer(model)shap_values =explainer(X_train)

运行没有错误,shap_values 给了我这个:

.values =
array([[[ 0.00775555, -0.00775555],
        [-0.03221035,  0.03221035],
        [-0.0027203 ,  0.0027203 ],
        ...,
        [ 0.00259787, -0.00259787],
        [-0.00459262,  0.00459262],
        [-0.0303394 ,  0.0303394 ]],

       [[-0.00068313,  0.00068313],
        [-0.03006355,  0.03006355],
        [-0.00245706,  0.00245706],
        ...,
        [-0.00418809,  0.00418809],
        [-0.00088372,  0.00088372],
        [-0.00030019,  0.00030019]],

       [[-0.00068313,  0.00068313],
        [-0.03006355,  0.03006355],
        [-0.00245706,  0.00245706],
        ...,
        [-0.00418809,  0.00418809],
        [-0.00088372,  0.00088372],
        [-0.00030019,  0.00030019]],

       ...,

但是,当我运行 shap.plots.beeswarm(shap_values) 时,出现以下错误:

ValueError:蜂群图不支持对具有多个维度的实例进行绘图解释!

我在这里做错了什么?

最佳答案

试试这个:

from shap import Explainer
from shap.plots import beeswarm
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer

X, y = load_breast_cancer(return_X_y=True, as_frame = True)
model = RandomForestClassifier().fit(X, y)

explainer = Explainer(model)
sv = explainer(X)

然后,由于 RF 有点特殊,因此仅检索第 1 类的 shap 值:

beeswarm(sv[:,:,1])

enter image description here

关于python - SHAP - 具有多个维度的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76083485/

相关文章:

python - 如何使用 ctypes 从 Python3 访问 C 中的某些常量值

hadoop - 如何使用Stanford NER CFR训练大型数据集

python - “精确”对象没有属性 'shap_values'

deep-learning - 对 PyTorch RNN/LSTM 使用 SHAP 值

python - 如何使用 Celery 从 Python 代码动态添加/停止 worker

python - 如何在搜索查询中加入文档

python - 使用 Map 并行化 for 循环并使用 pyspark 在 Spark 中减少

tensorflow - ValueError : Input 0 is incompatible with layer model: expected shape=(None, 14999, 7), 找到形状=(None, 7)

c++ - 如何在 OpenCV 和 C++ 中为图像分类配置 CvSVM

python-3.x - 如何将绘图(由 shap_values 生成)保存到 png?