Support mixed-type arrays in OneHotEncoder and OrdinalEncoder#34445
Open
ranykamel wants to merge 4 commits into
Open
Support mixed-type arrays in OneHotEncoder and OrdinalEncoder#34445ranykamel wants to merge 4 commits into
ranykamel wants to merge 4 commits into
Conversation
|
Thank you for opening your first pull request to scikit-learn! 🎉 To help get your contribution reviewed, please make sure that:
|
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
Fixes #11379.
What does this implement/fix? Explain your changes.
The current
OneHotEncoderandOrdinalEncoderimplementations does not support encoding arrays of mixed typing (e.g. ints and strings). PR #16713 partially addresses the issue by replacing an ambiguous comparison error with a more explicit message stating that encoders only support uniform typing. This PR adds an opt-in parameter,allow_mixed_types, to allow mixed type arrays to be encoded.The core of the bug centers on the built-in
sortedcall used to order categories, which fails on mixed types that cannot compare. To address this, the sort is wrapped by a helper,_sort_uniques, which applies a key to group values by type (e.g. strings are sorted with strings, numbers with numbers) while maintaining the original values and their types. This is conditioned on theallow_mixed_typesparameter being toggled True; if it is false, it reverts to current behavior.By default, encoders operate on current behavior, relying on the user to explictly allow mixed type encoding by toggling the parameter. Because a mix of types could be a sign that data was loaded incorrectly (e.g. column of strings with an erroneous numeric entry), the current error is raised by default to alert the user, and the error message now points the user to using
allow_mixed_types=Truein the case that the mix is intentional.First time contributor introduction
I am a Software Engineer and Masters student interested in learning about Machine Learning and Data Science. I've used sklearn frequently in my work and academic research. I chose this issue as I wanted to get my feet wet in open-source development and get a deeper understaning on one of the libraries I frequently use. I'm always learning and welcome any feedback or guidance!
AI usage disclosure
I used AI assistance for:
All outputs from AI assistance tools were reviewed and are understood by me. I take responsibility for the contents of this PR.
Any other comments?