我正在研究二进制分类,并试图用SHAP框架解释我的模型。
我使用的是logistic回归算法。我想用KernelExplainer和LinearExplainer来解释这个模型。
所以,我尝试了下面来自So here的代码
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:作为数据对象传递的未知类型:
如何使用logistic regression和SHAP LinearExplainer解释SHAP KernelExplainer模型?
发布于 2022-05-30 18:22:33
计算-从以下几个方面可以做到:
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不支持掩码,在本例中,loc或iloc都会返回相同的内容。
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的结果可以提供给瀑布“原样”。
https://stackoverflow.com/questions/72437807
复制相似问题