shap.AdditiveExplainer

class shap.AdditiveExplainer(model: Any, masker: Any, link: Any = None, feature_names: list[str] | None = None, linearize_link: bool = True)

Computes SHAP values for generalized additive models.

This assumes that the model only has first-order effects. Extending this to second- and third-order effects is future work (if you apply this to those models right now you will get incorrect answers that fail additivity).

__init__(model: Any, masker: Any, link: Any = None, feature_names: list[str] | None = None, linearize_link: bool = True) None

Build an Additive explainer for the given model using the given masker object.

Parameters:
modelfunction

A callable python object that executes the model given a set of input data samples.

maskerfunction or numpy.array or pandas.DataFrame

A callable python object used to “mask” out hidden features of the form masker(mask, *fargs). It takes a single a binary mask and an input sample and returns a matrix of masked samples. These masked samples are evaluated using the model function and the outputs are then averaged. As a shortcut for the standard masking used by SHAP you can pass a background data matrix instead of a function and that matrix will be used for masking. To use a clustering game structure you can pass a shap.maskers.Tabular(data, hclustering="correlation") object, but note that this structure information has no effect on the explanations of additive models.

Methods

__init__(model, masker[, link, ...])

Build an Additive explainer for the given model using the given masker object.

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.

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.

Attributes

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, **kwargs: Any) dict[str, Any]

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

Load an Explainer from the given file stream.

Parameters:
in_fileThe file stream to load objects from.
save(out_file: Any, model_saver: str | Callable[..., Any] = '.save', masker_saver: str | Callable[..., Any] = '.save') None

Write the explainer to the given file stream.

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

Determines if this explainer can handle the given model.

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