mojolearn.KernelExplainer#

class mojolearn.KernelExplainer(model, data, *, link='identity', random_state=None)[source]#

Bases: _AgnosticExplainer

Model-agnostic Kernel SHAP values.

Follows the shap package’s KernelExplainer: every coalition size small enough is enumerated in full, the rest are sampled with Kernel SHAP weights, and the values come from the efficiency-constrained weighted least squares. Every step except the model call runs on the device on GPU installs. The model is called on synthetic rows built from data, so explaining n rows makes n * n_coalitions * n_background model evaluations.

Differs from shap: random draws come from mojolearn’s own seeded generator, so values differ from shap’s for the same seed; sampled coalitions come in complementary pairs and are not de-duplicated (as in cuML); there is no l1 feature selection; every feature is treated as varying.

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.

  • link ({“identity”, “logit”}, default “identity”) – Applied to the model output before it is explained, by name; a link object or any other name raises NotImplementedError.

  • random_state (int or None, default None) – Seed for the coalition sampling: 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, through link: 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.RandomForestRegressor().fit(X, y)
>>> explainer = mojolearn.KernelExplainer(model, X[:50], random_state=0)
>>> explainer.shap_values(X[:10]).shape
(10, 8)
__init__(model, data, *, link='identity', 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[, nsamples, l1_reg])

Kernel SHAP values of the rows of X.

vendor_used()

The GPU backend this estimator runs on.

shap_values(X, nsamples='auto', l1_reg='auto')[source]#

Kernel 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.

  • nsamples (int or “auto”, default “auto”) – Number of coalitions per row. "auto" is 2 * n_features + 2048. With 30 features or fewer it is capped at 2**n_features - 2.

  • l1_reg ({“auto”, False, 0}, default “auto”) – Accepted for compatibility; no l1 feature selection is done, and any other value raises NotImplementedError.

Returns:

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