使用 KernelExplainer 和 LinearExplainer 的 SHAP 线性模型瀑布

SHAP Linear model waterfall with KernelExplainer and LinearExplainer

我正在研究二进制分类并尝试使用 SHAP 框架解释我的模型。

我正在使用逻辑回归算法。我想用 KernelExplainerLinearExplainer.

来解释这个模型

所以,我尝试了 SO

中的以下代码
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_breast_cancer
from shap import TreeExplainer, Explanation
from shap.plots import waterfall

X, y = load_breast_cancer(return_X_y=True, as_frame=True)

idx = 9
model = LogisticRegression().fit(X, y)
background = shap.maskers.Independent(X, max_samples=100)
explainer = KernelExplainer(model,background)
sv = explainer(X.iloc[[5]])   # pass the row of interest as df
exp = Explanation(
    sv.values[:, :, 1],         # class to explain
    sv.base_values[:, 1],
    data=X.iloc[[idx]].values,  # pass the row of interest as df
    feature_names=X.columns,
)
waterfall(exp[0])  

         

这引发了如下所示的错误

AssertionError: Unknown type passed as data object: <class 'shap.maskers._tabular.Independent'>

如何使用 SHAP KernelExplainer 和 SHAP LinearExplainer 解释 logistic regression 模型?

Calculation-wise 将执行以下操作:

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer

from shap import LinearExplainer, KernelExplainer, Explanation
from shap.plots import waterfall
from shap.maskers import Independent

X, y = load_breast_cancer(return_X_y=True, as_frame=True)

idx = 9
model = LogisticRegression().fit(X, y)

explainer = KernelExplainer(model.predict, X)
sv = explainer.shap_values(X.loc[[5]])   # pass the row of interest as df

exp = Explanation(sv,explainer.expected_value, data=X.loc[[idx]].values, feature_names=X.columns)
waterfall(exp[0])

注意:KernelExplainer 不支持掩码,在这种情况下 lociloc 将 return 相同。

background = Independent(X, max_samples=100)
explainer = LinearExplainer(model,background)
sv = explainer(X.loc[[5]])   # pass the row of interest by index
waterfall(sv[0])

这里注意,LinearExplainer的结果可以提供给瀑布“as-is”