nirs4all.api.methods_explain module
Optional nirs4all-methods attribution backend for general explain.
Selected with explainer_backend="methods". Methods owns every attribution
numeric (n4m.explain); this host adapter only chooses the explained
function, rows, background and feature groups, exports model-specific inputs
and records the result in the nirs4all.explanation.v1 envelope.
Model-agnostic methods (exact, permutation, kernel, LIME, occlusion) call the
same complete captured predictor as the default shap backend: raw input
columns in, original target units or class probabilities out. Linear and tree
SHAP explain the terminal estimator itself, so they apply in raw input space
only when the captured predictor has no preprocessing; otherwise
attribution_space="transformed_features" explains the terminal estimator
over the outputs of the fitted preprocessing chain. Every model-specific export
must reproduce the captured predictor before its attributions are returned.
- nirs4all.api.methods_explain.METHODS_EXPLAINER_TYPES = ('auto', 'permutation', 'exact', 'kernel', 'lime', 'occlusion', 'linear', 'tree')
explainer_typevalues accepted withexplainer_backend="methods".
- nirs4all.api.methods_explain.METHODS_OPTIONS = frozenset({'attribution_space', 'baseline', 'group_ranges', 'group_width', 'n_coalitions', 'n_permutations', 'seed'})
Options that only the Methods backend accepts.
- nirs4all.api.methods_explain.MODEL_AGNOSTIC_GROUP_WIDTH = 20
Default contiguous group width of model-agnostic methods on inputs wider than this width.
- class nirs4all.api.methods_explain.MethodsExplainSettings(explainer_type: str, baseline: str | None, group_width: int | None, group_ranges: tuple[tuple[int, int], ...] | None, n_permutations: int | None, n_coalitions: int | None, seed: int, attribution_space: str)[source]
Bases:
objectValidated Methods-backend options.
- Variables:
explainer_type (str) – Requested explainer (one of
METHODS_EXPLAINER_TYPES).baseline (str | None) – Explicit perturbation baseline, or
Nonefor the default.group_width (int | None) – Explicit fixed group width, or
None.group_ranges (tuple[tuple[int, int], ...] | None) – Explicit half-open feature-index ranges, or
None.n_permutations (int | None) – Explicit antithetic permutation pairs, or
None.n_coalitions (int | None) – Explicit sampled coalitions (
0= native auto), orNone.seed (int) – Seed of background subsampling and sampled explainers.
attribution_space (str) –
raw_inputortransformed_features.
- class nirs4all.api.methods_explain.MethodsExplanation(kind: str, values: ndarray, base_value: float | ndarray, labels: list[str], explained_names: list[str], explained_rows: ndarray, group_rows: ndarray, attribution_space: str, preprocessing: list[str], envelope: dict[str, Any])[source]
Bases:
objectAttributions of one selected output computed by the Methods backend.
- Variables:
kind (str) – Attribution kind actually computed (for example
tree_shap).values (numpy.ndarray) –
(rows, groups)attributions of the selected output.base_value (float | numpy.ndarray) – Expected value of the selected output; a per-row array when the baseline depends on the explained row.
explained_names (list[str]) – Names of the explained features.
explained_rows (numpy.ndarray) – Explained rows in the explained feature space.
group_rows (numpy.ndarray) – Per-group mean of
explained_rows(for figures).attribution_space (str) –
raw_inputortransformed_features.preprocessing (list[str]) – Class names of the fitted preprocessing steps applied before explanation in
transformed_featuresspace.envelope (dict[str, Any]) – The
nirs4all.explanation.v1envelope.
- nirs4all.api.methods_explain.NON_ADDITIVE_EXPLAINER_TYPES = frozenset({'lime', 'occlusion'})
Explainer types whose attributions do not sum to the prediction.
- nirs4all.api.methods_explain.explain_with_methods(*, function: Callable[[ndarray], Any], probabilities: bool, captured: Any, target_inverse: Any, X: ndarray, rows: Sequence[int], names: Sequence[str], axis: dict[str, Any], output_index: int, output_label: str | None, background_samples: int, settings: MethodsExplainSettings) MethodsExplanation[source]
Explain one captured predictor with the nirs4all-methods attribution kernels.
- Parameters:
function – Complete captured predictor (raw input rows to every output column).
probabilities – Whether
functionreturns class probabilities.captured – Captured model object (used for model-specific exports and
transformed_featuresspace).target_inverse – Artifact-level target inverse applied by
function.X – Every supplied raw input row; background rows are drawn from it.
rows – Indices in
Xof the explained rows.names – Raw input feature names.
axis – Recorded axis of the raw input features.
output_index – Selected output column.
output_label – Optional label of the selected output.
background_samples – Maximum background rows, subsampled without replacement with
settings.seed.settings – Validated Methods options.
- Returns:
The attributions of the selected output and the complete envelope.
- Raises:
ImportError – If the installed nirs4all-methods has no
n4m.explainbinding.ValueError – If the request cannot be explained faithfully.
- nirs4all.api.methods_explain.parse_methods_options(explainer_type: str, options: dict[str, Any]) MethodsExplainSettings[source]
Validate the Methods-backend options of one explain request.
- Parameters:
explainer_type – Requested
explainer_type.options – General explain options.
- Returns:
The validated settings.
- Raises:
ValueError – If an option is invalid or does not apply to the requested explainer.