You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG Tree export functions display native categorical splits as <= -inf #35534
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.
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.
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:
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?
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?
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?
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?
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!
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.
Activity
Dishan18 commentedon Nov 26, 2026
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_kindtells numeric and categorical splits apart, and the categories going left can be recovered fromtree_._left_cat_bitset(forSPLIT_CATEGORICAL_HASHsplits from ExtraTree*, by evaluating the hash for each category code). Category labels can come from the fittedOrdinalEncoder(_categorical_encoder.categories_), falling back to the encoded integers when it isn't available (e.g. when a rawTreeis passed toexport_graphviz).questions before opening the PR:
color in {red, blue}/color not in {red, blue}(left set shown, as in the issue) fine forexport_text, andcolor in {red, blue}as the node label forplot_tree/export_graphviz?{a, b, c, ... (+40)}), or is this left for a follow-up?tree.applyfor both the bitset and the hash split kinds?lorentzenchr commentedon Nov 27, 2026
@adam2392 This might interest you.
lcrmorin commentedon Nov 27, 2026
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!
Dishan18 commentedon Nov 27, 2026
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.
lorentzenchr commentedon Nov 28, 2026
This is indeed a duplicate of the subpoint of #33965.