【发布时间】:2021-12-19 09:56:07
【问题描述】:
我最近发现了this amazing library for ML interpretability。我决定使用来自 sklearn 的 toy dataset 构建一个简单的 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 = shap.TreeExplainer(clf)
shap_values = explainer.shap_values(X_train)
# visualize the first prediction's explanation (use matplotlib=True to avoid Javascript)
shap.force_plot(explainer.expected_value, shap_values[0,:], X_train.iloc[0,:])
【问题讨论】:
标签: python machine-learning scikit-learn shap