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:
objectFlat tree ensemble in the
n4m.explain.TreeEnsemblelayout.- Variables:
tree_offsets (numpy.ndarray) –
n_trees + 1node offsets; treetowns nodes[tree_offsets[t], tree_offsets[t + 1]).feature (numpy.ndarray) – Split feature per node,
-1for leaves.threshold (numpy.ndarray) – Split threshold per node (ignored for leaves).
left (numpy.ndarray) – Local left child index per node,
-1for leaves.right (numpy.ndarray) – Local right child index per node,
-1for leaves.cover (numpy.ndarray) – Training weight reaching each node.
value (numpy.ndarray) – Leaf values,
n_nodes * n_outputsrow-major.n_outputs (int) – Output columns of every leaf.
decision_type (int) –
0forx <= thresholdgoing left,1forx < 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.
- input_dtype
alias of
float64
- 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 transposedPLSRegression.coef_layouts of different scikit-learn releases. The intercept is recovered from the predictions rather thanintercept_because estimators such asPLSRegressioncenter inputs before applyingcoef_.- Parameters:
model – Fitted estimator exposing
coef_andpredict.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 thatmodel.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), XGBoostgbtreeand LightGBM models with identity-link regression objectives. Supported probability outputs: scikit-learn single-output decision-tree, random-forest and extra-trees classifiers (predict_probacolumns).- 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.