【发布时间】:2021-09-14 21:31:24
【问题描述】:
我正在尝试为单行创建 shap 值以进行本地解释,但我一直收到此错误。我尝试了各种方法,但仍然无法修复它们。
到目前为止我做过的事情 -
创建了随机决策树模型 -
from sklearn.ensemble import ExtraTreesRegressor
extra_tree = ExtraTreesRegressor(random_state=42)
extra_tree.fit(X_train, y_train)
然后尝试计算shap值-
# create a explainer object
explainer = shap.Explainer(extra_tree)
explainer.expected_value
array([15981.25812347])
#calculate shap value for a single row
shap_values = explainer.shap_values(pd.DataFrame(X_train.iloc[9274]).T)
这给了我这个错误 -
Exception: Additivity check failed in TreeExplainer! Please ensure the data matrix you passed to the explainer is the same shape that the model was trained on. If your data shape is correct then please report this on GitHub. Consider retrying with the feature_perturbation='interventional' option. This check failed because for one of the samples the sum of the SHAP values was 25687017588058.968750, while the model output was 106205.580000. If this difference is acceptable you can set check_additivity=False to disable this check.
训练的形状和我传的单行列数一样
X_train.shape
(421570, 164)
(pd.DataFrame(X_train.iloc[9274]).T).shape
(1, 164)
我不认为,这应该会导致任何问题。但为了确保,我也尝试使用 reshape 方法带来正确的形状。
shap_values = explainer.shap_values(X_train.iloc[9274].values.reshape(1, -1))
X_train.iloc[9274].values.reshape(1, -1).shape
(1, 164)
这也不能解决问题。所以,我想也许我还需要匹配行数。所以我创建了一个小数据框并尝试对其进行测试。
train = pd.concat([X_train, y_train], axis="columns")
train_small = train.sample(n=500, random_state=42)
X_train_small = train_small.drop("Weekly_Sales", axis=1).copy()
y_train_small = train_small["Weekly_Sales"].copy()
# train a randomized decision tree model
from sklearn.ensemble import ExtraTreesRegressor
extra_tree_small = ExtraTreesRegressor(random_state=42)
extra_tree_small.fit(X_train_small, y_train_small)
# create a explainer object
explainer = shap.Explainer(extra_tree_small)
shap_values = explainer.shap_values(X_train_small)
# I also tried to add the y value like this
shap_values = explainer.shap_values(X_train_small, y_train_small)
但没有任何效果。
GitHub 上的一个人建议卸载和reinstall 来自 GitHub 的 shap 的最新版本 -
pip install git+https://github.com/slundberg/shap.git
也试过了,还是不行。
有人知道如何解决这个问题吗?
【问题讨论】:
-
为什么要调换行?为什么不直接通过
shap_values = explainer.shap_values(X_train.iloc[9274])
标签: python machine-learning shap