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 有两个特别好的特性: - PartitionExplainer 与模型无关,但在使用平衡分区树时,其精确运行时间仅为二次方级别(相对于输入特征的数量)。这与 KernelExplainer 或 SamplingExplainer 的指数级精确运行时间形成对比。 
- PartitionExplainer 总是将一组相关特征作为一个整体来分配其应有的贡献。这意味着,如果提供给 PartitionExplainer 的层次聚类将相关特征分组在一起,那么特征相关性就被“考虑”进去了。也就是说,分配给一组紧密依赖的特征的总贡献,不会因为在解释的扰动过程中其相关性结构被破坏而改变。 
 - 请注意,对于线性模型,PartitionExplainer 返回的 Owen 值与标准的非分层 Shapley 值相同。 - __init__(model, masker, *, output_names=None, link=CPUDispatcher(<function identity>), linearize_link=True, feature_names=None, **call_args)
- 为给定的模型和 masker 构建一个 PartitionExplainer。 - 参数:
- modelfunction
- 用户提供的函数,该函数接收一个样本矩阵(# 样本数 x # 特征数)并计算这些样本的模型输出。 
- maskerfunction or numpy.array or pandas.DataFrame or tokenizer
- 用于“遮盖”隐藏特征的函数,形式为 masker(mask, x)。它接收一个输入样本和二元掩码,并返回一个被遮盖的样本矩阵。然后,这些被遮盖的样本将使用模型函数进行评估,并对输出进行平均。作为 SHAP 使用的标准遮盖方法的快捷方式,您可以传递一个背景数据矩阵而不是一个函数,该矩阵将用于遮盖。SHAP 中提供了特定领域的遮盖函数,例如用于图像的 shap.maksers.Image 和用于文本的 shap.maskers.Text。 
- partition_treeNone or function or numpy.array
- 输入特征的层次聚类,由一个遵循 scipy.cluster.hierarchy 所用格式的矩阵表示(示例请参见 notebooks_html/partition_explainer 目录)。如果这是一个函数,则该函数在接收单个输入样本时会生成一个聚类矩阵。如果您正在使用标准的 SHAP masker 对象,您可以传递 masker.clustering 来使用该 masker 内置的特征聚类;或者,如果 partition_tree 为 None,则默认使用 masker.clustering。 
 
 - 示例 
 - 方法 - __init__(model, masker, *[, output_names, ...])- 为给定的模型和 masker 构建一个 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)- 判断此解释器是否能处理给定的模型。 - 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=None, masker_loader=None, instantiate=True)
- 从给定的文件流加载一个解释器。 - 参数:
- 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')
- 将解释器写入给定的文件流。 
 - static supports_model_with_masker(model, masker)
- 判断此解释器是否能处理给定的模型。 - 这是一个抽象的静态方法,需要由每个子类实现。