From 08a77b5c011d58a515d5311d56cd3422fa5e6218 Mon Sep 17 00:00:00 2001 From: Jefferyjia <18818263299@163.com> Date: Tue, 1 Dec 2026 05:01:04 +0800 Subject: [PATCH] Fix RegressionTree diagram labels truncating fractional E[Y] values In _graph_from_tree, both the leaf and split branches cast tree_.value to int. For classifiers this matches the "# Obs" label, but for regression tree_.value holds the per-node mean of the target, so map(int) truncated it toward zero: leaves predicting -0.5/1.5 were labeled "E[Y]: 0" / "E[Y]: 1" in explain_global/explain_local diagrams, while predictions themselves were correct. Branch the cast on _is_classifier: classifiers keep the integer cast (byte-for-byte identical display), regression trees cast to float so the E[Y] labels show the actual node means. Adds regression tests asserting the exact E[Y] labels for RegressionTree and the unchanged integer "# Obs" formatting for ClassificationTree. Fixes #711 Signed-off-by: Jefferyjia <18818263299@163.com> --- .../interpret/glassbox/_decisiontree.py | 11 +++--- .../tests/glassbox/test_decisiontree.py | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/python/interpret-core/interpret/glassbox/_decisiontree.py b/python/interpret-core/interpret/glassbox/_decisiontree.py index 434eff751..0422ffb45 100644 --- a/python/interpret-core/interpret/glassbox/_decisiontree.py +++ b/python/interpret-core/interpret/glassbox/_decisiontree.py @@ -503,14 +503,15 @@ def recur(i, depth=0): feature = int(tree_.feature[i]) threshold = float(tree_.threshold[i]) + if _is_classifier(self): + value = [list(map(int, x)) for x in tree_.value[i].tolist()] + else: + value = [list(map(float, x)) for x in tree_.value[i].tolist()] if feature == _tree.TREE_UNDEFINED: feature = None threshold = None - value = [list(map(int, x)) for x in tree_.value[i].tolist()] - else: - value = [list(map(int, x)) for x in tree_.value[i].tolist()] - if feature_names is not None: - feature = feature_names[feature] + elif feature_names is not None: + feature = feature_names[feature] counter["node"] += 1 node_id = str(counter["node"]) diff --git a/python/interpret-core/tests/glassbox/test_decisiontree.py b/python/interpret-core/tests/glassbox/test_decisiontree.py index 3c0c7ec31..98b21c537 100644 --- a/python/interpret-core/tests/glassbox/test_decisiontree.py +++ b/python/interpret-core/tests/glassbox/test_decisiontree.py @@ -75,6 +75,43 @@ def test_dt(): assert global_viz is not None +def test_regression_tree_labels(): + # Issue #711: regression tree diagram labels must show the actual node + # means (E[Y]) instead of integer-truncated values. + X = np.array([[0.0], [1.0]]) + y = np.array([-0.5, 1.5]) + our_dt = RegressionTree(max_depth=1) + + our_dt.fit(X, y) + + assert np.allclose(our_dt.predict(X), y) + + expected = ["E[Y]: 0.5", "E[Y]: -0.5", "E[Y]: 1.5"] + nodes = our_dt.explain_global().data(None)["nodes"] + labels = [node["data"]["label"].splitlines()[-1] for node in nodes] + assert labels == expected + + nodes = our_dt.explain_local(X).data(0)["nodes"] + labels = [node["data"]["label"].splitlines()[-1] for node in nodes] + assert labels == expected + + +def test_classification_tree_labels(): + # The classifier diagram keeps integer "# Obs" labels (issue #711 only + # changes the regression path). + X = np.array([[0.0], [1.0]]) + our_dt = ClassificationTree(max_depth=1) + + our_dt.fit(X, [0, 1]) + + nodes = our_dt.explain_global().data(None)["nodes"] + for node in nodes: + label = node["data"]["label"].splitlines()[-1] + assert label.startswith("# Obs: ") + values = label[len("# Obs: ") :].split(", ") + assert all("." not in value for value in values) + + @pytest.fixture def skip_sklearn() -> set: """Test which we do not adhere to."""