Repository navigation
Keep the fitted column count in OneHotEncoder.transform - #1093
Open
vishalhabib99 wants to merge 1 commit into
Open
vishalhabib99 wants to merge 1 commit into
vishalhabib99 wants to merge 1 commit into
Conversation
OneHotEncoder._transform_col returned None for a column whose rows in the given data held no non-zero label, so transform skipped that column. On new data where a column only takes its base or infrequent values, the output had fewer columns than at fit time and every later column's dummies shifted left. Return an all-zero block of the fitted width instead, and keep None only for columns that have no dummy at all (label_max of 0 or NaN).
|
|
atishay-kasliwal
left a comment
There was a problem hiding this comment.
Fitted OneHotEncoder(min_obs=2) on three columns and transformed new data where one column holds only its base value. Master returned 2 columns instead of the fitted 4, so the other dummies shifted left; here it's 4, and each row matches its encoding when transformed together with the training data. Unseen or infrequent values and normal rows give the same output as before.
This branch has not been deployed
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.
Proposed changes
OneHotEncoder._transform_colreturnedNonewhen a column had no non-zero label in the data being transformed, andtransformthen skipped that column. That is fine at fit time, where every column with dummies has at least one non-zero label, but on new data a column can hold only its base (or infrequent) values. The column then contributes nothing, the output has fewer columns than the fitted encoder produces, and every later column's dummies shift left into the wrong positions.Reproduction on current master (089a882):
Before:
Column
b's dummy lands in the slot that belongs toa.After:
The fix returns an all-zero sparse block of the fitted width (
label_max) for such a column.Noneis still returned when the column has no dummy at all (label_maxof 0, or NaN when no value reachesmin_obs), sofit_transformoutput andload_databehavior are unchanged.Types of changes
Checklist
Further comments
Added
test_OneHotEncoder_transform_keeps_fitted_columnstotests/test_features.py. It fails on master (assert (5, 1) == (5, 2)) and passes with the fix.pytest tests/test_features.py tests/test_match.py(match usesload_data): 18 passed.black --checkandflake8are clean on the changed files.