Shap适用于二进制文件,但不适用于多类(https://github.com/slundberg/shap)

时间:2019-01-15 12:13:23

标签: classification

问题是Shap不能针对多类LogisticRegresssion运行,而对于二进制类却可以运行。

我试图为二进制模型生成随机数据,并使用shap生成图形,并且它起作用了! 但是,当我生成3个类时,会出现错误消息:

Traceback (most recent call last):
  File "C:/Users/clint.f/.PyCharmCE2018.2/config/scratches/scratch.py", line 28, in <module>
    shap_values = explainer.shap_values(X)
  File "C:\Users\clint.f\AppData\Local\Continuum\anaconda3\lib\site-packages\shap\explainers\linear.py", line 212, in shap_values
    return np.array(X - self.mean) * self.coef
ValueError: operands could not be broadcast together with shapes (1000,3) (3,3) 

import pandas as pd
import numpy as np
import shap
from sklearn.linear_model import  LogisticRegression
import copy


shap.initjs()
X = pd.DataFrame(np.random.rand(1000,3), columns=["A","B","C"])
y = np.random.randint(0, 3, (1000,))
clf = LogisticRegression(class_weight="balanced", random_state=0, solver="saga", multi_class="multinomial")
clf.fit(X,y)

explainer = shap.LinearExplainer(clf, X, feature_dependence="independent")
shap_values = explainer.shap_values(X)

我希望ValueError得到解决。

0 个答案:

没有答案