shap.PartitionExplainer

class shap.PartitionExplainer(model: ~typing.Any, masker: ~typing.Any, *, output_names: list[str] | None = None, link: ~collections.abc.Callable[[...], ~typing.Any] = CPUDispatcher(<function identity>), linearize_link: bool = True, feature_names: list[str] | None = None, **call_args: ~typing.Any)

使用 Partition SHAP 方法解释任何函数的输出。

Partition SHAP 通过特征层次结构递归地计算 Shapley 值,该层次结构定义了特征联盟并产生博弈论中的 Owen 值。

PartitionExplainer 具有两个特别好的特性

  1. PartitionExplainer 与模型无关,但当使用平衡分区树时,仅具有二次精确运行时(以输入特征的数量表示)。这与 KernelExplainer 或 SamplingExplainer 的指数精确运行时形成对比。

  2. PartitionExplainer 总是将一组相关特征所获得的信用分配给它们作为一个组时所应有的信用。这意味着如果提供给 PartitionExplainer 的层次聚类将相关特征分组在一起,那么特征相关性是“被考虑在内”的,因为分配给一组紧密相关特征的总信用不取决于它们在解释的扰动过程中相关结构被破坏时的行为方式。

请注意,对于线性模型,PartitionExplainer 返回的 Owen 值与标准非层次 Shapley 值相同。

__init__(model: ~typing.Any, masker: ~typing.Any, *, output_names: list[str] | None = None, link: ~collections.abc.Callable[[...], ~typing.Any] = CPUDispatcher(<function identity>), linearize_link: bool = True, feature_names: list[str] | None = None, **call_args: ~typing.Any) None

为给定模型和掩码器构建 PartitionExplainer。

参数:
model函数

用户提供的函数,它接受一个样本矩阵(# 样本 x # 特征)并计算这些样本的模型输出。

masker函数或 numpy.array 或 pandas.DataFrame 或分词器

用于“掩盖”隐藏特征的函数,形式为 masker(mask, x)。它接受单个输入样本和二进制掩码,并返回一个掩码样本矩阵。然后将这些掩码样本使用模型函数进行评估并求平均值。作为 SHAP 使用的标准掩码的快捷方式,您可以传递一个背景数据矩阵而不是函数,该矩阵将用于掩码。shap 中提供了特定领域的掩码函数,例如用于图像的 shap.maskers.Image 和用于文本的 shap.maskers.Text。

partition_treeNone 或函数或 numpy.array

由遵循 scipy.cluster.hierarchy 所用格式的矩阵表示的输入特征的层次聚类(请参阅 notebooks_html/partition_explainer 目录中的示例)。如果这是一个函数,那么该函数在给定单个输入示例时会生成一个聚类矩阵。如果您正在使用标准 SHAP 掩码器对象,则可以传递 masker.clustering 以使用该掩码器内置的特征聚类,或者如果 partition_tree 为 None,则默认使用 masker.clustering。

示例

请参阅 分区解释器示例

方法

__init__(model, masker, *[, output_names, ...])

为给定模型和掩码器构建 PartitionExplainer。

explain_row(*row_args, max_evals, ...[, ...])

解释单行并返回元组 (row_values, row_expected_values, row_mask_shapes)。

load(in_file[, model_loader, masker_loader, ...])

从给定的文件流加载一个解释器。

owen(fm, f00, f11, max_evals, ...)

根据排序递归计算一组嵌套的递归 Owen 值。

owen3(fm, f00, f11, max_evals, ...)

根据排序递归计算一组嵌套的递归 Owen 值。

save(out_file[, model_saver, masker_saver])

将解释器写入给定的文件流。

supports_model_with_masker(model, masker)

判断此解释器是否能处理给定的模型。

属性

input_shape

expected_value

values

dvalues

last_eval_count

model

masker

output_names

feature_names

link

linearize_link

explain_row(*row_args: Any, max_evals: int | Literal['auto'], main_effects: bool, error_bounds: bool, outputs: Any, silent: bool, batch_size: int | Literal['auto'] = 'auto', fixed_context: Literal[0, 1, 'auto'] | None = 'auto', **kwargs: Any) dict[str, Any]

解释单行并返回元组 (row_values, row_expected_values, row_mask_shapes)。

classmethod load(in_file: Any, model_loader: Callable[..., Any] | None = None, masker_loader: Callable[..., Any] | None = None, instantiate: bool = True) Explainer | dict[str, Any]

从给定的文件流加载一个解释器。

参数:
in_file用于加载对象的文件流。
owen(fm: MaskedModel, f00: ndarray[Any, dtype[Any]], f11: ndarray[Any, dtype[Any]], max_evals: int, output_indexes: Any, fixed_context: Literal[0, 1] | None, batch_size: int | Literal['auto'], silent: bool) tuple[Any, ndarray[Any, dtype[Any]]]

根据排序递归计算一组嵌套的递归 Owen 值。

owen3(fm: MaskedModel, f00: ndarray[Any, dtype[Any]], f11: ndarray[Any, dtype[Any]], max_evals: int, output_indexes: Any, fixed_context: Literal[0, 1] | None, batch_size: int | Literal['auto'], silent: bool) tuple[Any, ndarray[Any, dtype[Any]]]

根据排序递归计算一组嵌套的递归 Owen 值。

save(out_file: Any, model_saver: str | Callable[..., Any] = '.save', masker_saver: str | Callable[..., Any] = '.save') None

将解释器写入给定的文件流。

static supports_model_with_masker(model: Any, masker: Any) bool

判断此解释器是否能处理给定的模型。

这是一个抽象的静态方法,需要由每个子类实现。