Fix OneHotEncoder handle_missing='value' to encode missing values as zeros (fixes #400) - #485
Open
ranafaraz wants to merge 3 commits into
Open
Conversation
Added a regression test to ensure missing values are encoded as zeros in dummy columns when using handle_missing='value'. This addresses issue scikit-learn-contrib#400.
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.
Summary
Fixes #400.
With
handle_missing='value'(the default), a missing value present in the training data was given its own dummy column instead of being encoded as0in every dummy column. This contradicts the documented behaviour ("'value'will encode a missing value as 0 in every dummy column").Before this PR the
Nonerow produced an extrac1_3column; after it, the missing row is0acrossc1_1/c1_2, matching the docs and the'ignore'strategy.Root cause
OneHotEncoder._fitmapshandle_missing='value'to the ordinal strategy'value'. When a NaN is present duringfit, the internalOrdinalEncoderassigns it a positive code, which then survives thevalues[values > 0]filter ingenerate_mappingand becomes its own dummy column. (When NaN appears only at transform time it was already handled correctly via thebase_df.loc[-2] = 0row.)Fix
Map the
'value'strategy to the ordinal'return_nan'path (NaN ->-2), exactly as'ignore'already does. The missing code is then dropped from the columns and encoded as all-zeros through the existingbase_df.loc[-2] = 0row. Other modes (indicator,return_nan,error,ignore) are unchanged.Tests
test_handle_missing_valuecovering the issue's reproduction (both the transformed result and the mapping).tests/test_one_hot.pypasses locally (17 passed, 12 subtests) andruff checkis clean. No existing test needed changing — the inverse-transform round-trips still hold.A note for maintainers
In the issue thread you mentioned the
valuevsignorenaming is a little ambiguous. This PR keeps both options and only makesvaluematch its documented behaviour (sovalueandignorenow coincide for missing values). Happy to instead update the docs, deprecate/rename, or take whatever direction you prefer.Prepared with AI assistance and verified locally.