Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MNT: Simplify unknown handling in encoders/_encode.py - #34452

Merged
ogrisel merged 16 commits into
scikit-learn:mainfrom
cakedev0:refactor/encode
Aug 5, 2026
Merged

MNT: Simplify unknown handling in encoders/_encode.py#34452
ogrisel merged 16 commits into
scikit-learn:mainfrom
cakedev0:refactor/encode

Conversation

@cakedev0

@cakedev0 cakedev0 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

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:

  • remove _check_unknown which had a fairly complex logic in favor of the slightly more powerful _encode (encodes unknown as -1).
  • add _encode_labels: a simple . This avoids having a multi purposes with error messages about y when 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 transform slightly 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.

@cakedev0 cakedev0 changed the title MNT: Simplify encoding MNT: Simplify unknown handling in encoders/`_encode.py Jul 10, 2026
@cakedev0 cakedev0 changed the title MNT: Simplify unknown handling in encoders/`_encode.py MNT: Simplify unknown handling in encoders/_encode.py Jul 10, 2026
@cakedev0

Copy link
Copy Markdown
Contributor Author

Marking this as ready

and cc @thomasjpfan, it seems you have a good knowledge of encoders :)

@cakedev0
cakedev0 marked this pull request as ready for review July 10, 2026 08:05
Comment thread sklearn/utils/_encode.py Outdated
Comment thread sklearn/utils/tests/test_encode.py
Comment thread sklearn/preprocessing/_encoders.py Outdated
@cakedev0

Copy link
Copy Markdown
Contributor Author

This also makes transform slightly faster for both the numeric and object path, somethings like 10-20%

Rechecked after recent iterations, it's still the case 👍

@cakedev0 cakedev0 added this to Labs Jul 27, 2026
@cakedev0 cakedev0 moved this to PR waiting for reviews in Labs Jul 27, 2026
@cakedev0
cakedev0 requested a review from thomasjpfan July 28, 2026 14:41
@DeaMariaLeon

Copy link
Copy Markdown
Member

This also makes transform slightly faster for both the numeric and object path, somethings like 10-20%.

This is great @cakedev0! May I ask how did you measure that?

@cakedev0

Copy link
Copy Markdown
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 😅 )

@thomasjpfan thomasjpfan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ogrisel ogrisel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM besides the following comment suggestions.

Comment thread sklearn/utils/_encode.py
Comment thread sklearn/utils/tests/test_encode.py
Comment thread sklearn/utils/_encode.py Outdated
cakedev0 and others added 3 commits August 5, 2026 16:57
Co-authored-by: Olivier Grisel <[email protected]>
Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel ogrisel added the CUDA CI label Aug 5, 2026
@github-actions github-actions Bot removed the CUDA CI label Aug 5, 2026
@ogrisel
ogrisel enabled auto-merge (squash) August 5, 2026 15:00
@ogrisel
ogrisel merged commit 1074736 into scikit-learn:main Aug 5, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from PR waiting for reviews to Done in Labs Aug 5, 2026
@lorentzenchr

Copy link
Copy Markdown
Member

@cakedev0 Nice PR!

prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks
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.

5 participants