@@ -101,9 +101,11 @@ def __init__(
101101 determined from the masker if possible.
102102
103103 partition_tree : dict, required
104- A dictionary defining the hierarchical grouping of features. Each key
105- represents a group name, and its value is either a list of feature names
106- or another dictionary defining subgroups, note all input features must be included in the leaf nodes.
104+ A dictionary defining a custom hierarchical grouping of features. This is for users who want to
105+ define partitions based on domain knowledge. For automatic binary clustering, please use the
106+ `PartitionExplainer`. Each key represents a group name, and its value is either a list of
107+ feature names or another dictionary defining subgroups. Note all input features must be included
108+ in the leaf nodes.
107109 For example:
108110 {
109111 "Demographics": ["Age", "Gender", "Education"],
@@ -143,6 +145,12 @@ def __init__(
143145 else :
144146 self ._reshaped_model = self .model
145147
148+ if partition_tree is None :
149+ raise ValueError (
150+ "A `partition_tree` must be provided to CoalitionExplainer. "
151+ "This explainer is for custom user-defined partitions. "
152+ "For automatic hierarchical clustering, please use `shap.PartitionExplainer`."
153+ )
146154 self .partition_tree = partition_tree
147155
148156 if not callable (self .masker .clustering ):
@@ -302,28 +310,31 @@ def _build_tree(d, root):
302310 _build_tree (value , node )
303311 elif isinstance (d , list ):
304312 for item in d :
305- node = Node (item )
306- root .child .append (node )
313+ if isinstance (item , dict ):
314+ _build_tree (item , root )
315+ else :
316+ node = Node (item )
317+ root .child .append (node )
307318 # get all the sibling permutations
308319 _generate_permutations (root )
309320
310321
311322def create_partition_hierarchy (
312323 linkage_matrix , columns
313324): # this is a helper to turn scipy linkage matrix to partition_tree dict
314- def build_hierarchy (node , linkage_matrix , columns ):
315- if node < len (columns ):
316- return {columns [node ]: columns [node ]}
317- else :
318- left_child = int (linkage_matrix [node - len (columns ), 0 ])
319- right_child = int (linkage_matrix [node - len (columns ), 1 ])
320- left_subtree = build_hierarchy (left_child , linkage_matrix , columns )
321- right_subtree = build_hierarchy (right_child , linkage_matrix , columns )
322- return {f"cluster_{ node } " : {** left_subtree , ** right_subtree }}
325+ """Converts a SciPy linkage matrix into a SHAP partition_tree dictionary."""
326+
327+ # Build a partition tree that `_build_tree` can parse
328+ # The simplest way to implement this is to always create a new group for each merge.
329+ def build_final_tree (i ):
330+ if i < len (columns ):
331+ return columns [i ]
332+ left = int (linkage_matrix [i - len (columns ), 0 ])
333+ right = int (linkage_matrix [i - len (columns ), 1 ])
334+ return {f"group_{ i } " : [build_final_tree (left ), build_final_tree (right )]}
323335
324336 root_node = len (linkage_matrix ) + len (columns ) - 1
325- hierarchy = build_hierarchy (root_node , linkage_matrix , columns )
326- return hierarchy [f"cluster_{ root_node } " ]
337+ return build_final_tree (root_node )
327338
328339
329340def _combine_masks (masks ):
0 commit comments