mojolearn.KernelExplainer#
- class mojolearn.KernelExplainer(model, data, *, link='identity', random_state=None)#
Model-agnostic Kernel SHAP values.
Follows the
shappackage’sKernelExplainer: 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 fromdata, so explainingnrows makesn * n_coalitions * n_backgroundmodel evaluations.Differs from
shap: random draws come from mojolearn’s own seeded generator, so values differ fromshap’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_probathe probabilities are explained, otherwisepredict.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
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.RandomForestRegressor().fit(X, y) >>> explainer = mojolearn.KernelExplainer(model, X[:50], random_state=0) >>> explainer.shap_values(X[:10]).shape (10, 8)
Methods
get_params([deep])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.The GPU backend this estimator runs on.
- shap_values(X, nsamples='auto', l1_reg='auto')#
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
dataraisesValueError.nsamples (int or “auto”, default “auto”) – Number of coalitions per row.
"auto"is2 * n_features + 2048. With 30 features or fewer it is capped at2**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).
- 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.