Skip to content

drop must be None for OneHotEncoder to utilize handle_unknown = 'ignore' #18072

Closed

Description

@melgazar9

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?

Activity

  1. thomasjpfan commented on Aug 3, 2020

    @thomasjpfan
    Member

    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?

  2. melgazar9 commented on Aug 3, 2020

    @melgazar9
    Author

    Hey @thomasjpfan thanks - I changed it so there are now two separate feature requests. You can find the first one here #18073

  3. thomasjpfan commented on Aug 7, 2020

    @thomasjpfan
    Member

    "Unknown" used in the context of handle_unknown means "categories that are not seen during training" and not NaNs.

  4. glemaitre commented on Nov 9, 2020

    @glemaitre
    Member

    Just rewriting my message because it was confusing:

    from sklearn.preprocessing import OneHotEncoder
    
    X_train = [["a"], ["b"]]
    X_test = [["a"], ["b"], ["c"]]
    ohe = OneHotEncoder(handle_unknown="ignore", sparse=False).fit(X_train)
    print(ohe.transform(X_test))
    [[1. 0.]
     [0. 1.]
     [0. 0.]]
    

    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:

    ohe = OneHotEncoder(
        categories=[["a", "b", "c"]], drop="first", sparse=False).fit(X_train)
    print(ohe.transform(X_test))
    [[0. 0.]
     [1. 0.]
     [0. 1.]]
    
  5. glemaitre commented on Nov 9, 2020

    @glemaitre
    Member

    Be aware that np.nan would be seen as any other category:

    import numpy as np
    from sklearn.preprocessing import OneHotEncoder
    
    X_train = [["a"], ["b"], [np.nan]]
    X_test = [["a"], ["b"], [np.nan], ["c"]]
    ohe = OneHotEncoder(
        handle_unknown="ignore", sparse=False).fit(X_train)
    print(ohe.transform(X_test))
    [[1. 0. 0.]
     [0. 1. 0.]
     [0. 0. 1.]
     [0. 0. 0.]]
    
  6. ogrisel commented on Nov 9, 2020

    @ogrisel
    Member

    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.

  7. amueller commented on Nov 17, 2020

    @amueller
    Member

    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 ;)

  8. glemaitre commented on Nov 19, 2020

    @glemaitre
    Member

    OK I see the point. So I assume that we should be able to make the change without any issue. OneHotEncoder is becoming more permissive.

  9. glemaitre commented on Nov 19, 2020

    @glemaitre
    Member

    However, how do we handle the inverse_transform in this case?

  10. amueller commented on Dec 14, 2020

    @amueller
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions