Skip to content

BUG Tree export functions display native categorical splits as <= -inf #35534

In review

Description

@lorentzenchr

Describe the bug

export_text, plot_tree and export_graphviz do not know about the native
categorical splits added in 1.10 (#33354, #33972, #34832). A categorical split is
displayed as feature <= -inf, which looks like a split that sends no sample to the
left and gives no hint of which categories go to which child.

Users trying the new categorical_features parameter will likely plot their trees,
so this would be good to fix (or at least to document) before the 1.10 release.

Steps/Code to Reproduce

import pandas as pd
from sklearn.tree import DecisionTreeClassifier, export_text

X = pd.DataFrame({"color": pd.Categorical(["red", "green", "blue", "yellow"] * 5)})
y = X["color"].isin(["red", "blue"]).astype(int)

tree = DecisionTreeClassifier(categorical_features="from_dtype").fit(X, y)
print(export_text(tree, feature_names=["color"]))

Expected Results

The split shows the categories that go to each child, for instance:

|--- color in {green, yellow}
|   |--- class: 0
|--- color not in {green, yellow}
|   |--- class: 1

Actual Results

|--- color <= -inf
|   |--- class: 0
|--- color >  -inf
|   |--- class: 1

plot_tree and export_graphviz label the same node color <= -inf.

Possible cause

The export code in sklearn/tree/_export.py builds the label from
tree_.threshold, which is -inf for categorical nodes. It has no code path for
categorical splits.

Versions

scikit-learn 1.10.dev0 (main)

Activity

  1. added theissue type on Nov 26, 2026
  2. added
    Needs Triage
    on Nov 26, 2026
  3. Dishan18 commented on Nov 26, 2026

    @Dishan18

    I'd like to work on this once it's triaged.

    From a first look at sklearn/tree/_export.py, the fix can reuse what the tree already stores: tree_.split_kind tells numeric and categorical splits apart, and the categories going left can be recovered from tree_._left_cat_bitset (for SPLIT_CATEGORICAL_HASH splits from ExtraTree*, by evaluating the hash for each category code). Category labels can come from the fitted OrdinalEncoder (_categorical_encoder.categories_), falling back to the encoded integers when it isn't available (e.g. when a raw Tree is passed to export_graphviz).

    questions before opening the PR:

    1. Format: is color in {red, blue} / color not in {red, blue} (left set shown, as in the issue) fine for export_text, and color in {red, blue} as the node label for plot_tree / export_graphviz?
    2. Long category lists (up to 256 categories): should the label be truncated (e.g. {a, b, c, ... (+40)}), or is this left for a follow-up?
    3. Missing values: should the label also say which side NaN goes to, or should that stay out of scope like it is for numeric splits today?
    4. Is it OK to fix all three exporters in one PR, with tests that check the printed set matches tree.apply for both the bitset and the hash split kinds?
  4. added and removed
    Needs Triage
    on Nov 26, 2026
  5. lorentzenchr commented on Nov 27, 2026

    @lorentzenchr
    MemberAuthor

    @adam2392 This might interest you.

  6. lcrmorin commented on Nov 27, 2026

    @lcrmorin

    I’ve been working on this in #34809: all three exporters, both categorical split methods, original labels and missing/unknown routing, with tests against apply().

    The current scope is standalone trees; categorical forest sub-estimators raise a clear error, and display limits are left for later. Happy to coordinate and avoid duplicating work!

  7. Dishan18 commented on Nov 27, 2026

    @Dishan18

    Thanks @lcrmorin, I hadn't seen #34809. It came first, has already been through review, and covers more (missing/unknown routing, docs), so I'll close my PR #35536 in favour of it. Happy to help review #34809 if that's useful.

  8. lorentzenchr commented on Nov 28, 2026

    @lorentzenchr
    MemberAuthor

    This is indeed a duplicate of the subpoint of #33965.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions