explainer.pfi#

This module gathers PFI Explanation Methods

Classes

IncrementalPFI(model_function, ...[, ...])

Incremental PFI Explainer

class explainer.pfi.IncrementalPFI(model_function, loss_function, feature_names, storage=None, imputer=None, n_inner_samples=1, smoothing_alpha=0.001, dynamic_setting=True)[source]#

Bases: BaseIncrementalFeatureImportance

Incremental PFI Explainer

Computes PFI importance values incrementally by applying exponential smoothing. For each input instance tuple x_i, y_i one update of the explanation procedure is performed.

Parameters:
  • model_function (Callable[[Any], Any]) – The Model function to be explained (e.g. model.predict_one (river), model.predict_proba (sklearn)).

  • loss_function (Union[Metric, Callable[[Any, Dict], float]]) –

    The loss function for which the importance values are calculated. This can either be a callable function or a predefined river.metric.base.Metric.<br> - river.metric.base.Metric: Any Metric implemented in river (e.g.

    river.metrics.CrossEntropy() for classification or river.metrics.MSE() for regression).<br>

    • callable function: The loss_function needs to follow the signature of

      loss_function(y_true, y_pred) and handle the output dimensions of the model function. Smaller values are interpreted as being better if not overriden with loss_bigger_is_better=True. y_pred is passed as a dict.

  • feature_names (Sequence[Union[str, int, float]]) – List of feature names to be explained for the model.

  • smoothing_alpha (float, optional) – The smoothing parameter for the exponential smoothing of the importance values. Should be in the interval between ]0,1]. Defaults to 0.001.

  • storage (BaseStorage, optional) – Optional incremental data storage Mechanism. Defaults to GeometricReservoirStorage(size=100) for dynamic modelling settings (dynamic_setting=True) and UniformReservoirStorage(size=100) in static modelling settings (dynamic_setting=False).

  • imputer (BaseImputer, optional) – Incremental imputing strategy to be used. Defaults to MarginalImputer(sampling_strategy=’joint’).

  • n_inner_samples (int) – Number of model evaluation per feature and explanation step (observation). Defaults to 1.

  • dynamic_setting (bool) – Flag to indicate if the modelling setting is dynamic True (changing model, and adaptive explanation) or a static modelling setting False (all observations contribute equally to the final importance). Defaults to True.

n_inner_samples#

The number of inner_samples used for removing features.

Type:

int

explain_one(x_i, y_i, n_inner_samples=None, update_storage=True)[source]#

Explain one observation (x_i, y_i).

Parameters:
  • x_i (dict) – The input features of the current observation as a dict of feature names to feature values.

  • y_i (Any) – Target label of the current observation.

  • n_inner_samples (int, optional) – Number of model evaluation per feature for the current explanation step (observation). Defaults to None.

  • update_storage (bool) – Flag if the underlying incremental data storage mechanism is to be updated with the new observation (True) or not (False). Defaults to True.

Returns:

The current PFI feature importance scores.

Return type:

(dict)