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)[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 (TreeSHAP-IQ, LinearTreeSHAP, InterventionalTreeSHAP) 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.
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
decision_function(). 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:
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 (
str|None) – Split comparison used bydecision_function()("<="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 (
str) – 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.
- 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:
- decision_function(value, threshold, *, left_default)[source]¶
Decision function for split nodes.
The function compares the input value to the threshold using the specified decision type. If the value is NaN, the function returns the left_default.
- Parameters:
- Return type:
- Returns:
A boolean indicating whether to go left (True) or right (False) at the split node.
- 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: