mojolearn.PermutationExplainer#

class mojolearn.PermutationExplainer(model, data, *, random_state=None)[source]#

Bases: _AgnosticExplainer

Model-agnostic Permutation SHAP values.

Follows the shap package’s PermutationExplainer: each random permutation of the features adds them one by one (forward), then removes them (backward), and each feature’s marginal contribution is averaged over the background rows and the permutations. Every step except the model call runs on the device on GPU installs.

Differs from shap: random permutations come from mojolearn’s own seeded generator, so values differ from shap’s for the same seed.

Parameters:
  • model (estimator or callable) – A fitted mojolearn estimator, or a function mapping an (n, n_features) array to outputs. For a classifier with predict_proba the probabilities are explained, otherwise predict.

  • data (array of shape (n_background, n_features)) – Background rows that stand in for missing features.

  • random_state (int or None, default None) – Seed for the permutations: None (seed 0) or a nonnegative int. Anything else, including a RandomState object, raises ValueError.

Variables:
  • expected_value (float or array of shape (n_outputs,)) – The mean model output over data: a float for one output, else a float64 array.

  • n_outputs_ (int) – Number of model outputs explained.

  • n_features_in_ (int) – Number of features in data.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X @ np.arange(8, dtype=np.float32)
>>> model = mojolearn.Ridge().fit(X, y)
>>> explainer = mojolearn.PermutationExplainer(model, X[:50], random_state=0)
>>> explainer.shap_values(X[:10], npermutations=5).shape
(10, 8)
__init__(model, data, *, random_state=None)[source]#

Methods

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

shap_values(X[, npermutations])

Permutation SHAP values of the rows of X.

vendor_used()

The GPU backend this estimator runs on.

shap_values(X, npermutations=10)[source]#

Permutation SHAP values of the rows of X.

Parameters:
  • X (array of shape (n_samples, n_features)) – Rows to explain, converted to float32. A feature count different from data raises ValueError.

  • npermutations (int, default 10) – Number of random feature permutations per row; less than 1 raises ValueError.

Returns:

array, float64 – Shape (n_samples, n_features) for one output, else (n_samples, n_features, n_outputs).