shapiq.explainer.tree.utils#

This module contains utility functions for dealing with trees or tree structures.

Functions

compute_empty_prediction(leaf_values, ...)

Compute the empty prediction of a tree model.

get_conditional_sample_weights(sample_count, ...)

Get the conditional sample weights for a tree at each decision node.

shapiq.explainer.tree.utils.compute_empty_prediction(leaf_values, leaf_sample_weights)[source]#

Compute the empty prediction of a tree model.

The empty prediction is the weighted average of the leaf node values.

Parameters:
  • leaf_values (ndarray[float]) – The values of the leaf nodes in the tree.

  • leaf_sample_weights (ndarray[float]) – The sample weights of the leaf nodes in the tree.

Return type:

float

Returns:

The empty prediction of the tree model.

shapiq.explainer.tree.utils.get_conditional_sample_weights(sample_count, parent_array)[source]#

Get the conditional sample weights for a tree at each decision node.

The conditional sample weights are the probabilities of going left or right at each decision node. The probabilities are computed by the number of instances going through each node divided by the number of instances going through the parent node. The conditional sample weights of the root node is 1.

Parameters:
  • sample_count (ndarray[int]) – The counts of the instances going through each node.

  • parent_array (ndarray[int]) – The parent array denoting the id of the parent node for each node in the tree. The parent of the root node is -1 or otherwise specified.

Return type:

ndarray[float]

Returns:

The conditional sample weights of the nodes.

Examples

>>> parent_array = np.asarray([-1, 0, 1, 1, 0, 4, 4])  # binary tree with depth 2
>>> sample_count = np.asarray([100, 70, 50, 20, 30, 15, 15])
>>> get_conditional_sample_weights(sample_count, parent_array)
>>> [1., 0.7, 0.71428571, 0.28571429, 0.3, 0.5, 0.5]