shapiq.tree.TreeModel¶
- class shapiq.tree.TreeModel(children_left, children_right, children_missing, features, thresholds, values, node_sample_weight, empty_prediction=None, leaf_mask=None, n_features_in_tree=None, max_feature_id=None, feature_ids=None, root_node_id=None, n_nodes=None, nodes=None, decision_type=None, feature_map_original_internal=None, feature_map_internal_original=None, original_output_type='raw', intercepts=None, coeffs=None, cat_values=None, cat_start=None, cat_size=None, input_precision='float64')[source]¶
Bases:
objectInternal representation of a single tree used by the shapiq tree explainers.
Each library-specific converter (scikit-learn, XGBoost, LightGBM, CatBoost) targets this common format so that the downstream algorithms (QuadratureTreeSHAP, TreeSHAP-IQ, LinearTreeSHAP, InterventionalTreeSHAPIQ) only need to understand one node-array layout.
Constructor arguments that fall back to a computed default when
Noneis passed are documented on__init__(). The attributes below describe what is available on a fully initialized instance.- Variables:
children_left – The left children of each node in a tree. Leaf nodes are
-1.children_right – The right children of each node in a tree. Leaf nodes are
-1.children_missing – The child each node routes missing-value samples to. Used together with
children_leftto derivechildren_left_defaultduring__init__.children_left_default – Boolean mask.
Trueat indexiif missing-value samples at nodeiare routed tochildren_left[i]. Derived fromchildren_missing.features – The feature indices of the decision nodes in a tree. Leaf nodes are
-2.thresholds – The thresholds of the decision nodes in a tree. Leaf nodes are
np.nan.values – The leaf-node values, flattened to a 1-D array. Non-leaf nodes are set to
0.node_sample_weight – The sample weights of the nodes in a tree.
cat_values – Concatenated (per-node sorted) category sets of all categorical decision nodes in CSR layout. A categorical node routes
int(x[feature]) in setto the left child and everything else to the right child (NaN goes to the missing child). Empty for trees without categorical splits.cat_start – Per-node offset of the node’s category set inside
cat_values.cat_size – Per-node length of the node’s category set.
0marks a numeric or leaf node.is_categorical – Boolean mask of categorical decision nodes (
cat_size > 0).has_categorical – Whether the tree contains any categorical decision node.
empty_prediction – The empty prediction of the tree model (weighted mean of leaf values).
leaf_mask – The boolean mask of the leaf nodes in a tree.
n_features_in_tree – The number of distinct features actually used by decision nodes.
max_feature_id – The maximum feature index used by any decision node (or
0if none).feature_ids – The set of feature indices used by decision nodes.
root_node_id – The root node id of the tree model. Defaults to
0.n_nodes – The number of nodes in the tree model.
decision_type – The split comparison used by
goes_left(). Either"<="(default) or"<".nodes – The node ids of the tree model as
np.arange(n_nodes).feature_map_original_internal – Mapping of feature indices from the original feature indices (as in the model) to the internal feature indices (as in the tree model).
feature_map_internal_original – Mapping of feature indices from the internal feature indices (as in the tree model) to the original feature indices (as in the model).
original_output_type – The original output type of the tree model. Defaults to
"raw". Currently not used by downstream algorithms.intercepts – Per-leaf intercept terms for linear-leaf tree models. Currently unused.
coeffs – Per-leaf coefficient vectors for linear-leaf tree models. Currently unused.
- Parameters:
node_sample_weight (ndarray[tuple[Any, ...], dtype[floating]])
empty_prediction (float)
n_features_in_tree (int)
max_feature_id (int)
root_node_id (int)
n_nodes (int)
decision_type (Literal['<=', '<'])
original_output_type (Literal['raw', 'probability'])
input_precision (Literal['float64', 'float32'])
Initialize the
TreeModel.All numpy-array arguments must share a common node ordering. Arguments listed as
None-able fall back to a value computed from the mandatory arrays.- Parameters:
children_left (
ndarray[tuple[Any,...],dtype[int_]]) – Left-child node ids;-1denotes a leaf.children_right (
ndarray[tuple[Any,...],dtype[int_]]) – Right-child node ids;-1denotes a leaf.children_missing (
ndarray[tuple[Any,...],dtype[int_]]) – Node id to which missing-value samples are routed.features (
ndarray[tuple[Any,...],dtype[int_]]) – Decision-node feature indices. Leaf positions are sanitized to-2.thresholds (
ndarray[tuple[Any,...],dtype[floating]]) – Decision-node thresholds. Leaf positions are sanitized tonp.nan.values (
ndarray[tuple[Any,...],dtype[floating]]) – Leaf-node values. Higher-dim arrays are flattened to 1-D; non-leaf positions are forced to0.node_sample_weight (
ndarray[tuple[Any,...],dtype[floating]]) – Per-node sample weights.NaNat leaves is replaced with1.empty_prediction (
float|None) – Pre-computed empty prediction.Nonetriggerscompute_empty_prediction().leaf_mask (
ndarray[tuple[Any,...],dtype[bool]] |None) – Boolean mask of leaf nodes.Nonederives it fromchildren_left == -1.n_features_in_tree (
int|None) – Number of distinct features used by decision nodes.Nonederives it from the unique values infeatures(excluding-2).max_feature_id (
int|None) – Largest feature index used.Nonederives it fromfeatures.feature_ids (
set[int] |None) – Set of feature indices used by decision nodes.Nonederives it fromfeatures.root_node_id (
int|None) – Root node id.Nonedefaults to0.n_nodes (
int|None) – Number of nodes.Nonederives it fromlen(children_left).nodes (
ndarray[tuple[Any,...],dtype[int_]] |None) – Node-id array.Nonedefaults tonp.arange(n_nodes).decision_type (
Optional[Literal['<=','<']]) – Split comparison used bygoes_left()("<="or"<").Nonedefaults to"<=".feature_map_original_internal (
dict[int,int] |None) – Mapping from original to internal feature indices.Nonedefaults to the identity mapping onfeature_ids.feature_map_internal_original (
dict[int,int] |None) – Mapping from internal to original feature indices.Nonedefaults to the identity mapping onfeature_ids.original_output_type (
Literal['raw','probability']) – Currently unused; accepted for forward compatibility.intercepts (
ndarray[tuple[Any,...],dtype[floating]] |None) – Currently unused; accepted for forward compatibility with linear-leaf trees.coeffs (
ndarray[tuple[Any,...],dtype[floating]] |None) – Currently unused; accepted for forward compatibility with linear-leaf trees.cat_values (
ndarray[tuple[Any,...],dtype[int_]] |None) – Concatenated category sets of categorical decision nodes (CSR layout, see the class docstring). Must be provided together withcat_startandcat_size.None(default) marks a tree without categorical splits.cat_start (
ndarray[tuple[Any,...],dtype[int_]] |None) – Per-node offsets intocat_values.cat_size (
ndarray[tuple[Any,...],dtype[int_]] |None) – Per-node category-set lengths (0= numeric node).input_precision (
Literal['float64','float32']) – Precision in which the source library compares inputs against thresholds at prediction time:"float32"for XGBoost and CatBoost (inputs are cast before routing, seecast_input()),"float64"otherwise. Defaults to"float64".
- cast_input(x)[source]¶
Round
xthe way the source library rounds prediction inputs.XGBoost and CatBoost cast inputs to float32 before comparing them against their (float32) split thresholds; routing the original float64 values can reach a different leaf than the model’s own prediction. The float32 embedding into float64 is exact and order-preserving, so the round trip below makes every downstream float64 comparison bit-identical to the source library’s float32 comparison. LightGBM compares in float64 (no cast), and sklearn’s thresholds are exact midpoints of adjacent float32 values, which makes its internal float32 input cast unobservable — both keep
"float64".
- compute_empty_prediction()[source]¶
Compute the empty prediction of the tree model.
The method computes the empty prediction of the tree model by taking the weighted average of the leaf node values. The method modifies the tree model in place.
- Return type:
- goes_left(node_id, value)[source]¶
Route a feature value through the split at
node_id.Handles both node kinds: numeric nodes compare against
thresholds[node_id]usingdecision_type; categorical nodes routeint(value)in the node’s category set to the left child and everything else (including unknown categories) to the right child. NaN values route to the missing child for both node kinds.
- predict(X)[source]¶
Predicts the output of multiple instances.
- reduce_feature_complexity()[source]¶
Reduces the feature complexity of the tree model.
The method reduces the feature complexity of the tree model by removing unused features and reindexing the feature indices of the decision nodes in the tree. The method modifies the tree model in place. To see the original feature mappings after the reduction, use the
feature_map_original_internalandfeature_map_internal_originalattributes.For example, consider a tree model with the following feature indices:
[0, 1, 8]
The method will remove the unused feature indices and reindex the feature indices of the decision nodes in the tree to the following:
[0, 1, 2]
Feature
'8'is ‘renamed’ to'2'such that in the internal representation a one-hot vector (and matrices) of length3suffices to represent the feature indices.- Return type: