shap.PartitionExplainer

class shap.PartitionExplainer(model, masker, *, output_names=None, link=CPUDispatcher(<function identity>), linearize_link=True, feature_names=None, **call_args)

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

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

PartitionExplainer 具有两个特别好的特性

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

  2. PartitionExplainer 始终将一组相关特征的信用分配给该特征集,如果将其视为一个组。这意味着,如果提供给 PartitionExplainer 的层次聚类将相关特征组合在一起,那么特征相关性在某种意义上被“考虑在内”,即分配给一组紧密依赖特征的总信用不取决于它们在解释的扰动过程中打破其相关结构时的行为方式。

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

__init__(model, masker, *, output_names=None, link=CPUDispatcher(<function identity>), linearize_link=True, feature_names=None, **call_args)

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

参数:
modelfunction

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

maskerfunction 或 numpy.array 或 pandas.DataFrame 或 tokenizer

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

partition_treeNone 或 function 或 numpy.array

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

示例

请参阅 Partition explainer 示例

方法

__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, ...])

从给定的文件流加载 Explainer。

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

计算基于排序递归的嵌套递归 Owen 值集。

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

计算基于排序递归的嵌套递归 Owen 值集。

save(out_file[, model_saver, masker_saver])

将 explainer 写入给定的文件流。

supports_model_with_masker(model, masker)

确定此 explainer 是否可以处理给定的模型。

explain_row(*row_args, max_evals, main_effects, error_bounds, batch_size, outputs, silent, fixed_context='auto')

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

classmethod load(in_file, model_loader=<bound method Model.load of <class 'shap.models._model.Model'>>, masker_loader=<bound method Serializable.load of <class 'shap.maskers._masker.Masker'>>, instantiate=True)

从给定的文件流加载 Explainer。

参数:
in_file要从中加载对象的文件流。
owen(fm, f00, f11, max_evals, output_indexes, fixed_context, batch_size, silent)

计算基于排序递归的嵌套递归 Owen 值集。

owen3(fm, f00, f11, max_evals, output_indexes, fixed_context, batch_size, silent)

计算基于排序递归的嵌套递归 Owen 值集。

save(out_file, model_saver='.save', masker_saver='.save')

将 explainer 写入给定的文件流。

static supports_model_with_masker(model, masker)

确定此 explainer 是否可以处理给定的模型。

这是一个抽象静态方法,旨在由每个子类实现。