nirs4all.api.explain_export module

Export fitted host estimators to the Methods model-specific explain inputs.

The SDK owns its Python models; Methods owns attribution numerics. This module only converts fitted scikit-learn, XGBoost and LightGBM tree ensembles to the flat path-dependent TreeSHAP layout of n4m.explain.TreeEnsemble and recovers the affine coefficients of linear estimators. Callers must verify that the exported model reproduces the explained predictor before reporting attributions.

XGBoost and LightGBM are optional: a model can only be an instance of their classes when the package is already imported, so no import is attempted here.

class nirs4all.api.explain_export.FlatTreeEnsemble(tree_offsets: ndarray, feature: ndarray, threshold: ndarray, left: ndarray, right: ndarray, cover: ndarray, value: ndarray, n_outputs: int, decision_type: int = 0, default_left: ndarray | None = None, base_offset: ndarray | None = None, tree_scale: float = 1.0, input_dtype: type = <class 'numpy.float64'>)[source]

Bases: object

Flat tree ensemble in the n4m.explain.TreeEnsemble layout.

Variables:
  • tree_offsets (numpy.ndarray) – n_trees + 1 node offsets; tree t owns nodes [tree_offsets[t], tree_offsets[t + 1]).

  • feature (numpy.ndarray) – Split feature per node, -1 for leaves.

  • threshold (numpy.ndarray) – Split threshold per node (ignored for leaves).

  • left (numpy.ndarray) – Local left child index per node, -1 for leaves.

  • right (numpy.ndarray) – Local right child index per node, -1 for leaves.

  • cover (numpy.ndarray) – Training weight reaching each node.

  • value (numpy.ndarray) – Leaf values, n_nodes * n_outputs row-major.

  • n_outputs (int) – Output columns of every leaf.

  • decision_type (int) – 0 for x <= threshold going left, 1 for x < threshold.

  • default_left (numpy.ndarray | None) – Optional per-node missing-value direction.

  • base_offset (numpy.ndarray | None) – Optional per-output constant added to the ensemble output.

  • tree_scale (float) – Factor applied to the summed leaf values.

  • input_dtype (type) – Precision in which the estimator compares its inputs; rows must be rounded to it before explanation.

base_offset: ndarray | None = None
cover: ndarray
decision_type: int = 0
default_left: ndarray | None = None
ensemble_fields() → dict[str, Any][source]

Return the keyword arguments of n4m.explain.TreeEnsemble.

feature: ndarray
input_dtype

alias of float64

left: ndarray
n_outputs: int
right: ndarray
threshold: ndarray
tree_offsets: ndarray
tree_scale: float = 1.0
value: ndarray
nirs4all.api.explain_export.affine_coefficients(model: Any, Z: ndarray) → tuple[ndarray, ndarray][source]

Recover verified affine coefficients of a fitted linear estimator.

The coefficient orientation is resolved from coef_ against the estimator’s own predictions, which covers the transposed PLSRegression.coef_ layouts of different scikit-learn releases. The intercept is recovered from the predictions rather than intercept_ because estimators such as PLSRegression center inputs before applying coef_.

Parameters:
  • model – Fitted estimator exposing coef_ and predict.

  • Z – Reference rows (at least two distinct rows) in the estimator’s input space.

Returns:

(coefficients, intercept) with shapes (n_features, n_outputs) and (n_outputs,) such that model.predict(Z) == Z @ coefficients + intercept.

Raises:

ValueError – If the estimator exposes no coefficients reproducing its predictions.

nirs4all.api.explain_export.export_tree_ensemble(model: Any, *, probabilities: bool) → FlatTreeEnsemble[source]

Export a fitted tree model to the flat path-dependent TreeSHAP layout.

Supported regressors: scikit-learn DecisionTreeRegressor, ExtraTreeRegressor, RandomForestRegressor, ExtraTreesRegressor, GradientBoostingRegressor (constant init), XGBoost gbtree and LightGBM models with identity-link regression objectives. Supported probability outputs: scikit-learn single-output decision-tree, random-forest and extra-trees classifiers (predict_proba columns).

Parameters:
  • model – Fitted terminal estimator.

  • probabilities – Whether the explained output is predict_proba.

Returns:

The flat ensemble whose output equals the model prediction.

Raises:

ValueError – If the model is not a supported tree ensemble.

nirs4all.api.explain_export.is_tree_ensemble(model: Any, *, probabilities: bool) → bool[source]

Return whether export_tree_ensemble() recognizes the model type.