MNT: Simplify unknown handling in encoders/_encode.py - #34452
Merged
Conversation
_encode.py
Contributor
Author
|
Marking this as ready and cc @thomasjpfan, it seems you have a good knowledge of encoders :) |
cakedev0
marked this pull request as ready for review
July 10, 2026 08:05
thomasjpfan
reviewed
Jul 15, 2026
Contributor
Author
Rechecked after recent iterations, it's still the case 👍 |
This was referenced Jul 22, 2026
Member
This is great @cakedev0! May I ask how did you measure that? |
Contributor
Author
|
With benchmark code from #34394 (but maybe a bit different, I have a local version so it's kept around from branch to branch 😅 ) |
ogrisel
approved these changes
Aug 5, 2026
ogrisel
left a comment
Member
There was a problem hiding this comment.
LGTM besides the following comment suggestions.
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
Member
|
@cakedev0 Nice PR! |
prady0t
pushed a commit
to prady0t/scikit-learn
that referenced
this pull request
Sep 2, 2026
…#34452) Co-authored-by: Olivier Grisel <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
When I was working on #34393 I started to refactor a bit some things in the encoders. I isolated the refactor in this PR.
What does this implement/fix? Explain your changes.
In
sklearn/utils/_encode.py:_check_unknownwhich had a fairly complex logic in favor of the slightly more powerful_encode(encodes unknown as -1)._encode_labels: a simple . This avoids having a multi purposes with error messages aboutywhen it's also used for features...Thanks to those changes, I can simplify
_BaseEncoder._transform: no need to replace unknow values by a valid category before calling encode (this was a fairly dirty trick IMO).Those changes are also helpful for #34393
AI usage: no
In my experience, AI is terrible at this kind of refactor.
Any other comments?
This also makes
transformslightly faster for both the numeric and object path, somethings like 10-20%. I don't think it's worth publishing detailed benchmark results. But I can if needed.