shap.PartitionExplainer

class shap.PartitionExplainer(model, masker, *, partition_tree=None, output_names=None, link=CPUDispatcher(<function identity>))
__init__(model, masker, *, partition_tree=None, output_names=None, link=CPUDispatcher(<function identity>))

Uses the Partition SHAP method to explain the output of any function.

Partition SHAP computes Shapley values recursively through a hierarchy of features, this hierarchy defines feature coalitions and results in the Owen values from game theory. The PartitionExplainer has two particularly nice properties: 1) PartitionExplainer is model-agnostic but when using a balanced partition tree only has quadradic exact runtime (in term of the number of input features). This is in contrast to the exponential exact runtime of KernalExplainer or SamplingExplainer. 2) PartitionExplainer always assigns to groups of correlated features the credit that set of features would have had if treated as a group. This means if the hierarchical clustering given to PartitionExplainer groups correlated features together, then feature correlations are “accounted for” … in the sense that the total credit assigned to a group of tightly dependent features does net depend on how they behave if their correlation structure was broken during the explanation’s perterbation process. Note that for linear models the Owen values that PartitionExplainer returns are the same as the standard non-hierarchical Shapley values.

Parameters
modelfunction

User supplied function that takes a matrix of samples (# samples x # features) and computes the output of the model for those samples.

maskerfunction or numpy.array or pandas.DataFrame or tokenizer

The function used to “mask” out hidden features of the form masker(mask, x). It takes a single input sample and a binary mask and returns a matrix of masked samples. These masked samples will then be evaluated using the model function and the outputs averaged. As a shortcut for the standard masking using by SHAP you can pass a background data matrix instead of a function and that matrix will be used for masking. Domain specific masking functions are available in shap such as shap.maksers.Image for images and shap.maskers.Text for text.

partition_treeNone or function or numpy.array

A hierarchical clustering of the input features represented by a matrix that follows the format used by scipy.cluster.hierarchy (see the notebooks_html/partition_explainer directory an example). If this is a function then the function produces a clustering matrix when given a single input example. If you are using a standard SHAP masker object then you can pass masker.clustering to use that masker’s built-in clustering of the features, or if partition_tree is None then masker.clustering will be used by default.

Examples

See Partition Explainer Examples

Methods

__init__(model, masker, *[, partition_tree, …])

Uses the Partition SHAP method to explain the output of any function.

explain(x, hierarchical, npartitions, …)

explain_row(*row_args, max_evals, …)

Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes).

hierarchical_shap_values(x[, npartitions, …])

owen(fm, f00, f11, npartitions, …)

Compute a nested set of recursive Owen values based on an ordering recursion.

owen3(x, npartitions, output_indexes, …)

Compute a nested set of recursive Owen values based on an ordering recursion.

owen4(x, npartitions, output_indexes, …)

Compute a nested set of recursive Owen values based on an ordered recursion of paried permutations.

shap_values(x[, npartitions, …])

single_context_owen(x, npartitions, …)

Compute a nested set of recursive Owen values.

supports_model(model)

Determines if this explainer can handle the given model.

explain_row(*row_args, max_evals, main_effects, error_bounds, batch_size, outputs, silent)

Explains a single row and returns the tuple (row_values, row_expected_values, row_mask_shapes).

owen(fm, f00, f11, npartitions, output_indexes, fixed_context, batch_size, silent)

Compute a nested set of recursive Owen values based on an ordering recursion.

owen3(x, npartitions, output_indexes, hierarchical, context, batch_size, silent)

Compute a nested set of recursive Owen values based on an ordering recursion.

owen4(x, npartitions, output_indexes, interaction_tolerance, context, batch_size, silent)

Compute a nested set of recursive Owen values based on an ordered recursion of paried permutations.

TODO: This is not done yet, but when it is done it should outperform our current solution of just using the PermutationExplainer for tabular data.

single_context_owen(x, npartitions, output_indexes, interaction_tolerance, context, silent)

Compute a nested set of recursive Owen values.

static supports_model(model)

Determines if this explainer can handle the given model.

This is an abstract static method meant to be implemented by each subclass.