Samples.zip 示例压缩文件夹包含:
要重现问题,请执行以下步骤:
lin2 =joblib.load('model.pkl')加载线性回归模型x_test_2 = pd.read_csv('x_test.csv').drop(['Unnamed: 0'],axis=1)加载x_test_2explainer_test = shap.Explainer(lin2.predict, x_test_2)
shap_values_test = explainer_test(x_test_2)
Run Code Online (Sandbox Code Playgroud)
partial_dependence_plot查看错误信息:ValueError:x 和 y 不能大于二维,但具有形状 (2,) 和 (2, 1, 1)
sample_ind = 3
shap.partial_dependence_plot(
"new_personal_projection_delta",
lin.predict,
x_test, model_expected_value=True,
feature_expected_value=True, ice=False,
shap_values=shap_values_test[sample_ind:sample_ind+1,:]
)
Run Code Online (Sandbox Code Playgroud)
例外:waterfall_plot 需要模型输出的标量 base_values 作为第一个参数,但您已传递一个数组作为第一个参数!尝试 shap.waterfall_plot(explainer.base_values[0], value[0], X[0]) 或对于多输出模型尝试 shap.waterfall_plot(explainer.base_values[0], value[0][0], X[ 0])。
shap.plots.waterfall(shap_values_test[sample_ind], max_display=14)
partial_dependence_plot& shap.plots.waterfall?我最近发现了这个令人惊叹的 ML 可解释性库。我决定使用 sklearn 的玩具数据集构建一个简单的 xgboost 分类器,并绘制一个force_plot.
为了理解这个情节,图书馆说:
上面的解释显示了每个有助于将模型输出从基值(我们传递的训练数据集上的平均模型输出)推送到模型输出的功能。将预测推高的特征以红色显示,将预测推低的特征以蓝色显示(这些力图在我们的 Nature BME 论文中介绍)。
所以在我看来,base_value 应该与clf.predict(X_train).mean()0.637 相同。然而,从绘图来看,情况并非如此,数字实际上不在 [0,1] 之内。我尝试以不同的基础(10,e,2)进行记录,假设这将是某种单调变换......但仍然不走运。我怎样才能得到这个base_value?
!pip install shap
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingClassifier
import pandas as pd
import shap
X, y = load_breast_cancer(return_X_y=True)
X = pd.DataFrame(data=X)
y = pd.DataFrame(data=y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
clf = GradientBoostingClassifier(random_state=0)
clf.fit(X_train, y_train)
print(clf.predict(X_train).mean())
# load JS visualization code to notebook
shap.initjs()
explainer …Run Code Online (Sandbox Code Playgroud) 查看 shap 库,我遇到了这个问题,其中答案展示了瀑布图,简洁!看看这里和这里的一些官方示例,我注意到这些图也展示了这些功能的价值。
shap 包包含shap.waterfall_plot和shap.plots.waterfall,在 Iris 数据集上训练的随机森林上尝试两者都得到了相同的结果(请参阅下面的一个代码和图像示例)
for which_class in y.unique():
display(
shap.waterfall_plot(shap.Explanation(values=shap_values[int(which_class)][idx],
base_values=explainer.expected_value[int(which_class)],
feature_names=X_test.columns.tolist())
)
)
Run Code Online (Sandbox Code Playgroud)
其中idx表示我试图解释的测试集中的一个样本。该代码为其中一个类生成以下图:

如何让绘图也显示特征值?我没有看到任何可以传递给绘图方法的其他参数
任何帮助是极大的赞赏!