屏蔽/烧蚀Shap包中的非设置成员特征

2024-05-16 06:37:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我想可能有一个简单的答案。我试图使用Shap包返回ML数据集的特征子集(联合)的值。这是Shapley意义上的“值”,意味着存在所有特征的预测与存在连接(非屏蔽)特征的预测之间的差异。我已经在源代码中摸索了一段时间,并认为这可能是通过MaskedModel类实现的,但最好通过一个示例来说明:

import shap
import sklearn
import numpy as np
import pandas as pd

# Load the Boston housing dataset
X,y = shap.datasets.boston()

# Create a tree model
tree_model = sklearn.ensemble.RandomForestRegressor(
    n_jobs=10, n_estimators=50,random_state=0
).fit(X, y)

# Create a masked model
masked_model = shap.utils.MaskedModel(model=tree_model, masker=X, link=lambda x: x)

# Create a 1d array boolean mask
bool_mask = np.array(
    [False,  True,  True, False, False, False, False, False, False, False, False, False, False]
)

# Call the masked model - expected (well... hoped-for) output are Shapley values for a coalition of just the 2nd and 3rd features
masked_model(bool_mask)

这实际上返回TypeError: 'DataFrame' object is not callable,这很公平,它不是-所以我尝试了一下

masked_model.masker = lambda x: X.loc[:, x]
masked_model(bool_mask)

这就产生了ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 13,一个我到目前为止还没有弄清楚如何克服的错误

如果有人能在这里把我引向正确的方向(包括但不限于纠正我最初的任何假设),我将不胜感激


Tags: theimportfalsetreeformodelcreatemask