shap.explainers.Partition

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

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_row(*row_args, max_evals, …[, …])

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

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

Load an Explainer from the given file stream.

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

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

save(out_file[, model_saver, masker_saver])

Write the explainer to the given file stream.

supports_model_with_masker(model, masker)

Determines if this explainer can handle the given model.

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

Explains a single row and returns the tuple (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)

Load an Explainer from the given file stream.

Parameters
in_fileThe file stream to load objects from.
owen(fm, f00, f11, max_evals, output_indexes, fixed_context, batch_size, silent)

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

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

Write the explainer to the given file stream.

static supports_model_with_masker(model, masker)

Determines if this explainer can handle the given model.

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