mojolearn.PermutationExplainer#
- class mojolearn.PermutationExplainer(model, data, *, random_state=None)#
Model-agnostic Permutation SHAP values.
Follows the
shappackage’sPermutationExplainer: 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 fromshap’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_probathe probabilities are explained, otherwisepredict.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
RandomStateobject, raisesValueError.
- Variables:
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)
Methods
get_params([deep])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.The GPU backend this estimator runs on.
- numeric_mode_used()#
The numeric mode this estimator runs in.
- Returns:
str –
'identical'or'fast', read from the compiled binary the estimator uses, not from the argument it was given.
- set_params(**params)#
Set constructor parameters. The estimator is rebuilt through its constructor, so every constructor check runs again and any fitted state is dropped, as a new setting invalidates it.
- vendor_used()#
The GPU backend this estimator runs on.
- Returns:
str –
'metal','cuda'or'hip', read from the compiled binary the estimator uses.
- shap_values(X, npermutations=10)#
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
dataraisesValueError.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).