You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
drop must be None for OneHotEncoder to utilize handle_unknown = 'ignore' #18072
OneHotEncoder does not allow the parameter handle_unknown = 'ignore' and drop = 'if_binary' or drop = 'first' (or drop = anything other than None). It seems reasonable to want to drop the first category in a column since it is redundant, while also ignoring NaNs when transforming new data. Any thoughts here?
There are two feature requests here, it would be good to open an issues for each one. This way the discussions can be more focused and not jump between two issues.
Since this is titled after the second feature request, may you open another issue for the first feature request and edit this one?
In the above case, handle_unknown="ignore" allows to encode an unseen category "c" at transform time.
If we were dropping a column in this setting, we would get "a" and "c" with the same encoding.
Therefore, the only way be able to drop a column is to indicate the categorie beforehand:
I think having drop="if_binary" and handle_unknown="ignore" is still potentially valid as it's then easy way to get rid off collinear features on the training set for "close to binary categorical variables".
The rare test features will be mapped to 0 which can represent "a" or "b" arbitrarily but those values are very rare and we cannot use them to do meaningfully generalizeable predictions by definition because they are too rare (otherwise they would be present in the training set).
So I think we should allow this case (and drop="first" as well).
We could also introduce an handle_unknown="warn" that would do the same as handle_unknown="ignore" with a warning each time an unknown value is discovered at prediction time. This could be useful when doing interactive model development to get an idea of how frequent is this kind problem and maybe identify the cause of those unknown test time values more easily but this is another issue.
Hm I ran into this in dabl, I thought I opened an sklearn issue but apparently didn't?
My issue was in this PR dabl/dabl#265
basically I want to be robust to unknown categories (because it's within cross-validation and we might have long tails) and also want to drop="if_binary"
I agree with @ogrisel's assessment above that conflating the rare categories with the dropped one isn't too bad.
Also, probably the binary columns are those with long tail categories, not the binary ones.
I think not handling unknown values makes the drop feature much less useful.
I wouldn't worry about warn for now, in particular because #16018 will make all of this even more complicated ;)
I was primarily concerned with the unknown values being in columns that are not the binary column btw.
The simple option would be to inverse_transform to the dropped column and write that out in the doc.
We could warn during fitting if there is conflation happening. I think in the most common case (some variables binary, some variables long-tailed) there will be no conflation.
If we allow drop="first" together with ignore, any unknown values will be conflated with the baseline category. Maybe that should just be in the docstring for drop? If they are represented to the model as identical, I think it makes sense that the inverse_transform would also be identical, which helps with model debugging.
Activity
thomasjpfan commentedon Aug 3, 2020
There are two feature requests here, it would be good to open an issues for each one. This way the discussions can be more focused and not jump between two issues.
Since this is titled after the second feature request, may you open another issue for the first feature request and edit this one?
melgazar9 commentedon Aug 3, 2020
Hey @thomasjpfan thanks - I changed it so there are now two separate feature requests. You can find the first one here #18073
thomasjpfan commentedon Aug 7, 2020
"Unknown" used in the context of
handle_unknownmeans "categories that are not seen during training" and not NaNs.glemaitre commentedon Nov 9, 2020
Just rewriting my message because it was confusing:
In the above case,
handle_unknown="ignore"allows to encode an unseen category"c"at transform time.If we were dropping a column in this setting, we would get
"a"and"c"with the same encoding.Therefore, the only way be able to drop a column is to indicate the categorie beforehand:
glemaitre commentedon Nov 9, 2020
Be aware that
np.nanwould be seen as any other category:ogrisel commentedon Nov 9, 2020
I think having
drop="if_binary"andhandle_unknown="ignore"is still potentially valid as it's then easy way to get rid off collinear features on the training set for "close to binary categorical variables".The rare test features will be mapped to 0 which can represent "a" or "b" arbitrarily but those values are very rare and we cannot use them to do meaningfully generalizeable predictions by definition because they are too rare (otherwise they would be present in the training set).
So I think we should allow this case (and
drop="first"as well).We could also introduce an
handle_unknown="warn"that would do the same ashandle_unknown="ignore"with a warning each time an unknown value is discovered at prediction time. This could be useful when doing interactive model development to get an idea of how frequent is this kind problem and maybe identify the cause of those unknown test time values more easily but this is another issue.amueller commentedon Nov 17, 2020
Hm I ran into this in dabl, I thought I opened an sklearn issue but apparently didn't?
My issue was in this PR dabl/dabl#265
basically I want to be robust to unknown categories (because it's within cross-validation and we might have long tails) and also want to
drop="if_binary"I agree with @ogrisel's assessment above that conflating the rare categories with the dropped one isn't too bad.
Also, probably the binary columns are those with long tail categories, not the binary ones.
I think not handling unknown values makes the
dropfeature much less useful.I wouldn't worry about warn for now, in particular because #16018 will make all of this even more complicated ;)
glemaitre commentedon Nov 19, 2020
OK I see the point. So I assume that we should be able to make the change without any issue.
OneHotEncoderis becoming more permissive.glemaitre commentedon Nov 19, 2020
However, how do we handle the
inverse_transformin this case?amueller commentedon Dec 14, 2020
I was primarily concerned with the unknown values being in columns that are not the binary column btw.
The simple option would be to
inverse_transformto the dropped column and write that out in the doc.We could warn during fitting if there is conflation happening. I think in the most common case (some variables binary, some variables long-tailed) there will be no conflation.
If we allow
drop="first"together with ignore, any unknown values will be conflated with the baseline category. Maybe that should just be in the docstring fordrop? If they are represented to the model as identical, I think it makes sense that theinverse_transformwould also be identical, which helps with model debugging.