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."""