Repository navigation
FEA Categorical split support for DecisionTree*, and RandomForest* - #33354
Conversation
DecisionTree*, and RandomForest*
|
I got bored over the weekend, so here is this feature I've been dying to get into scikit-learn! Yay snow. |
|
@cakedev0 Your review would help a lot.
The HGBT uses bitsets, see |
|
Good point @lorentzenchr! I was hoping we can do that in a follow on PR to keep the diff relatively contained. I tried it in the previous PR but there's a lot of diff from moving things around essentially. I.e. item 4 :p |
|
I will for sure give it a review. I took a quick look already and it looks pretty nice at first sight 👍 you took the best of our preceding PRs I think ^^ (though I don't fully remember them 😅 ) I took a look at the possible TODOs, and here is my opinon
One thing that concerns me, and that we'll need to address before really publicizing the categorical support is the risk of over-fitting, see cakedev0#2 (comment). But I think it's fine to address it later, in another PR. Also, I'd appreciate that we merge #32119 first, it's been opened for a while and it will have some conflicts with this one... (nothing hard to solve, but still...). If have some boredom left, can you give it a review? 😉 |
|
Can you copy the adapted |
I think we should add an option for ordinal encoding by number of occurrences in the training set (independently of this PR), see: #32161. Feel free to say so there so that we can remove the "Needs decision: include features" label and unleash the flow of contributors ;) |
cakedev0
left a comment
There was a problem hiding this comment.
Here is a first pass, many small things, no big blockers. I'll let you clean up, add the split tests, and fix the CI before making a second path.
There was a problem hiding this comment.
LGTM, finally - after yeeeeeeeeears! Thanks @adam2392 for pushing it.
2 remarks:
- Now it's +1'478 -195 loc changed (at some point it was more than +1800).
- I expect the follow-up work for random forests and gradient boosting to be not that trivial. Some (private) API is needed to pass the cat encoding to the trees and skip validation there.
|
@thomasjpfan @cakedev0 @adam2392 If there is anything else you want to add, I leave it unmerged for a few days. |
Yes thank you and @cakedev0 and @thomasjpfan very much for the reviews, and ideas throughout this long process. And also @adrinjalali for the historical work!
Yep. I think RF is the logical next step, and then some fast-follow-up work outlined in #33965 . Somewhere there, I can consolidate the design to use ColumnTransformer to make the design at a high-level look very similar to HGBT. |
I'll ping @scikit-learn/core-devs actually since this is so "big" and "new". Tho if you have comments that require me to refactor things here... I kindly ask maybe that I can do it as a follow-up :p. |
cakedev0
left a comment
There was a problem hiding this comment.
LGTM, I think we can merge by the end of the week.
One minor catch from codex, but it can be fixed in another PR if you don't have time this week @adam2392.
The fact that the behavior is aligned with HGB is great (as we ultimately want to merge GB with HGB, at least publicly) and makes me confident this PR can be safely merged and that the API/decisions will remain stable.
About the subsequent work, I agree with @lorentzenchr about that:
I expect the follow-up work for random forests and gradient boosting to be not that trivial. Some (private) API is needed to pass the cat encoding to the trees and skip validation there.
I'd like to try implementing histograming in decision trees before doing this, as it will have the same kind of API need to be exposed in RF/GB. And doing both at the same time might help in finding a better design.
| assert r2 > 0.99 | ||
|
|
||
|
|
||
| def test_partial_dependence_recursion_decision_tree_categorical_target_feature(): |
There was a problem hiding this comment.
This test breaks / this feature is not implemented for categorical features that don't map to their encoding.
It's really not trivial to support categorical support in BaseDecisionTree._compute_partial_dependence_recursion, so let's just raise a not implemented error.
I think I looked into this, and seems hard. We want to store the data as uint type within the Cython code. WDYT? Maybe we can continue the discussion in #33965 |
|
Going to merge this tmr! |
|
Finally 🎉 🚀 🌲 @adam2392 Very minor note (more a remark on history): While not written as governance it is custom in scikit-learn that a reviewer merges. |
Ah didn't know that. But TIL. Thanks for the note! I got trigger happy |
Me too! |
Apologies for the exaggeration, obviously it took only a tad over 11 years since #4899 😜 No but seriously: thanks and congrats 🥳🤩 |
|
Well, someone with the endurance to finish it has to do the work. This feature is in good company alongside with
|
scikit-learn#33354) Co-authored-by: Adam Li <[email protected]> Co-authored-by: Arthur Lacote <[email protected]> Co-authored-by: Christian Lorentzen <[email protected]> Co-authored-by: Thomas J. Fan <[email protected]>
…-learn#35221) - Docstring incorrectly stated 'at most 255 unique categories'. The actual limit is 256 with splitter='best' (bitset capacity) and 16,777,216 with splitter='random'. The ordinal encoding added in scikit-learn#33354 means raw input values are no longer required to be in any specific range; only the *number* of unique categories is limited. - Error message referred to encoded value range [0, 255] which is an internal implementation detail, not a property of the user's data. Replaced with a clear, actionable message: 'Categorical feature N has K categories, but at most M are supported with splitter=...' Fixes scikit-learn#35221

Reference Issues/PRs
Supersedes #12866 #4899 and #7068 , #3346
Supersedes #29437 because the diff was too complicated relative to main.
Took some inspiration from: cakedev0#2, so I would say we should say this is co-authored with @cakedev0. Instead of only using a uint64_t to encode the bitset, I opt for
uint64_t*, which future proofs our ability to handle much larger cardinality categories, which will be useful for the categorical support in ExtraTrees*.What does this implement/fix? Explain your changes.
Implements splitting rules when categorical data is given in a decision tree classifier/regressor. For now, limits it to use the breiman shortcut to demonstrate the value and the main implementation changes. Downstream features (outlined in the next section) can be more simply added after this PR.
ParentInfo,SplitRecord,Node) ->_utils.pxdAlso ran this benchmark, which compares one-hot-encoding (OHE), ordinal (ord), and native-categorical support (cat); I had to group-encode rare categories to make this all fit within the constraint of "256" max categories:
There are two results worth looking at. The first is using default parameters where
max_features = 'sqrt'. I shuffled up the categories after ordinal encoding.The second is where we fix

max_features=None, so it samples all features:(Interestingly, if you order the categories by their occurrence, then the ordinal approach does quite well. This probably opens up a door of heuristics that we can later on add for categorical support. there is no 1 right-answer for everything, so I think it's totally reasonable).
Here, OHE is abysmally slow, and performance is essentially equivalent with native categorical support being on average the best. Surprisingly the ordinal strategy is quite good. It could be due to the fact that maybe I'm cutting categorical support down by group-encoding rare categories, which could be informative. It has ~10+x deeper trees, and almost 3-5x longer fittime.
Open TODO In a downstream PR
goes_leftoperation to work at inference time tooAny other comments?
May be related to #24967