Skip to content

FIX Show categorical splits as category sets in tree exporters - #35536

Closed
Dishan18 wants to merge 2 commits into
scikit-learn:mainfrom
Dishan18:fix-export-categorical-splits
Closed

Dishan18 wants to merge 2 commits into
scikit-learn:mainfrom
Dishan18:fix-export-categorical-splits

Conversation

@Dishan18

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #35534.

What does this implement/fix? Explain your changes.

export_text, export_graphviz and plot_tree built split labels from tree_.threshold, which is -inf for native categorical splits, so these nodes were shown as feature <= -inf.

This PR uses tree_.split_kind to detect categorical splits and decodes the categories sent to the left child:

  • SPLIT_CATEGORICAL_BITSET: from tree_._left_cat_bitset;
  • SPLIT_CATEGORICAL_HASH (ExtraTree*): by evaluating the routing hash for each category code with the stored seed.

Category names come from the fitted encoder (_categorical_encoder.categories_). When it is not available (e.g. a raw Tree passed to export_graphviz), the encoded integers are shown.

Output for the example in the issue:

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

plot_tree and export_graphviz label the node color in {green, yellow}.

Tests check, for all three exporters and for both split kinds, that the printed set matches the routing given by tree.apply.

Introduce yourself

Hi, I'm Dishan, a final-year engineering student in AIML. I've used scikit-learn a lot in my ML coursework and projects, so I wanted to start contributing back. This is my first contribution to scikit-learn, I'm happy to adjust the approach based on your feedback.

AI usage disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Research and understanding

Any other comments?

Open questions for reviewers:

  1. Is the format above OK?
  2. Should long category lists (up to 256) be truncated?
  3. Should the label say which side missing values go to? Numeric splits don't show this today, so I left it out.

export_text, export_graphviz and plot_tree built split labels from
tree_.threshold, which is -inf for native categorical splits, so these
nodes were shown as "feature <= -inf".

Decode the categories sent to the left child from the node bitset (or
the hash seed for ExtraTree splits) and display them by name, e.g.
"color in {red, blue}".

Fixes scikit-learn#35534
@github-actions

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@Dishan18

Copy link
Copy Markdown
Author

Closing as a duplicate of #34809, which came first and covers the same fix more completely. Thanks!

@Dishan18 Dishan18 closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant