Skip to content

Add "other" / min_frequency option to OneHotEncoder #12153

Closed

Description

@amueller

The OneHotEncoder should have an option to summarize categories that are not frequent - or we should have another transformer to do that beforehand.
Generally having a maximum number of categories or having a minimum frequency per category would make sense as thresholds. This is similar to what we're doing in CountVectorizer but I think common enough for categorical variables that we should explicitly make it easy to do.

Activity

  1. datajanko commented on Sep 25, 2018

    @datajanko
    Contributor

    Actually, I once implemented something along those lines and would like to work on that.
    I also build a transformer that was able to perform a linear regression on groups. For this purpose it seems to be to much overhead to one hot-encoder those variables. There are also cases where one would like to perform the OneHotEncoding as late as possible in a pipeline.

    So I'd vote for a separate transformer that simply is called in OneHotEncoder if necessary

  2. jnothman commented on Sep 26, 2018

    @jnothman
    Member
  3. datajanko commented on Sep 26, 2018

    @datajanko
    Contributor

    I'll work on this and try to provide a PR as soon as possible.

    I just read the _encoders.py source code and it seems that also the OrdinalEncoder would profit directly. As both OneHot- and OrdinalEncoder inherit from _BaseEncoder where we call _encode (which could can numpy's unique which can return counts), I'd start tackling the issue from there.

    Would that make sense?

  4. jnothman commented on Sep 26, 2018

    @jnothman
    Member
  5. datajanko commented on Oct 2, 2018

    @datajanko
    Contributor

    Just a quick update:

    I'm still working on this, but I feel that a WIP Pull request is still to early.

    In introduced a function _group_values(values, min_freq=0, groups=None), which calls for dtype not object

    def _group_values_numpy(values, min_freq=0, group=None):
        if min_freq and group:
            raise ValueError
        if min_freq:
            uniques, counts = np.unique(values, return_counts=True)
            mask = (counts/len(values) < min_freq)
            group = uniques[mask]
        if group is not None:
            try:
                values[np.isin(values, group)] = group[0]
            except IndexError:
                pass
            return values, group
        else:
            return values, None
    

    essentially, this function could be plugged in in front of e.g. the _encodecalls in BaseEncoder.
    This leads to a question in the _fitfunction, which only calls _encode if categories are inferred automatically. I`d assume, that we don't want to apply a frequency threshold if we provide the categories manually, right?

    Also, it would be nice to set the 'other' label as desired (currently the first element of the grouped items is set as the representative element, if one such element is found).

    So what is the minimal quality a WIP-PR should have? (I have some tests for _group_values btw)
    Please apologize my slowness. Feel free to provide any comment, as I'm here to learn.

  6. jorisvandenbossche commented on Oct 4, 2018

    @jorisvandenbossche
    Member

    I think we should have more discussion about what we want exactly as behaviour.

    On the PR (#12264 (comment)) I raised the question about "combining all non-frequent categories in one separate category" vs "putting all non-frequent categories into an existing (first) category", which seems a quite important difference from a usage point of view.

    Also, if we would consider multiple ways or multiple options, I think it might become too much for putting in the OneHotEncoder. Eg if you want the option to either specify minimum frequency as maximum number of categories, the option how to name the new category (or in which existing category to put it, see first question), or the option to replace them with missing value, etc ... it might give some "keyword noise" to put all of that in the Ordinal/OneHotEncoder, and it might warrant a separate transformer class.
    (but which has clear downside of not being able to share the factorization step)

  7. amueller commented on Oct 4, 2018

    @amueller
    MemberAuthor

    I think putting them into a new category is more common in "ML". But we could also just have a parameter that is others_col='name_of_category' and if that level already exists we add to it, if not, it's created?

  8. amueller commented on Oct 4, 2018

    @amueller
    MemberAuthor

    We could put this code in a different class but then add a convenience call into OneHotEncoder? That might make it also more discoverable.

  9. datajanko commented on Oct 4, 2018

    @datajanko
    Contributor

    I'd also vote (again) for a separate class. One more argument for a separate class: one-hot and ordinal encoder are not necessarily invertible using the "frequency cut off"-implementation.

    Appending to an existing category is fine for me as well.
    How do we want to treat already integer encoded classes? Append there as well or add to let's say min(classes) -1. What shall we do with float labels? (Not common but might appear).

  10. amueller commented on Oct 4, 2018

    @amueller
    MemberAuthor

    I like modularity but I also like convenience ;)

  11. jorisvandenbossche commented on Oct 4, 2018

    @jorisvandenbossche
    Member

    I also think that a separate class makes the most sense (at least for the default).

    We could put this code in a different class but then add a convenience call into OneHotEncoder? That might make it also more discoverable.

    How would you see this in practice API-wise? (I think it would be nice, but just not sure how it would look like)
    One keyword to trigger using this other class with its default values? Or a way to pass through arguments?

  12. datajanko commented on Oct 5, 2018

    @datajanko
    Contributor

    I would suggest to pass through the arguments. Then in fit and transform, as a first step one alters X by applying the transformer to it and returning a copy, if necessary. I think using the class with default values will not be flexible enough. Then, in my opinion, it would be better to pass the class object directly. But for the user this would be more cumbersome

  13. amueller commented on Oct 5, 2018

    @amueller
    MemberAuthor

    One keyword to trigger using this other class with its default values? Or a way to pass through arguments?

    Yes, basically as for #12294 (though that one has less options).
    Here we have two parameters, right? a threshold and a column name?
    Adding both to OneHotEncoder might not be terrible, and the threshold is a natural way to enable the feature (i.e. have a threshold different from None or something). Though: is the threshold float or int? Generally we try to avoid changing semantics with the type, so I guess we would have a min_frequency and a min_count parameter for the threshold, and maybe max_levels (max_categories?)
    Adding all these to OneHotEncoder might be a bit much.

    (sorry for rambling lol)

  14. datajanko commented on Oct 5, 2018

    @datajanko
    Contributor

    So for me, I'd suggest to construct the new transformer. I'd vote for something like GroupTransformer, GroupEncoder. With that name, we might even pass dictionaries in the future to group certain values together under one class.

    Concerning OneHot and Ordinal-Encoder: What about adding simply a pre_transformer=None keyword (or similar) and add **kwargsand pass those keyword arguments to the pre_transformer class?

  15. amueller commented on Oct 5, 2018

    @amueller
    MemberAuthor

    yeah let's just go with a separate transformer for now.

  16. 5 remaining items

  17. jnothman commented on Nov 5, 2018

    @jnothman
    Member

    If integer + NaN, convert to float! I think/hope it should be a relatively safe cast in this context...

    One Enhancement of the GroupingEncoder, as I had in mind, would be to pass something like: {'label_a': grouped1, 'label_b': 'grouped1', 'label_c': 'grouped_2', 'label_d': 'grouped_2'}
    However, this could be done at other preprocessing steps.

    A big -1 from me. What we don't need is more stateless transformers to do arbitrary preprocessing. That's what FunctionTransformer is for. We can't hope to provide an API for a vast range of data transformation needs. It's just not maintainable.

    I would rather something with a lightweight API here.

  18. amueller commented on Nov 5, 2018

    @amueller
    MemberAuthor

    Happy to have this in the OHE transformer. Sorry I've been busy.

  19. amueller commented on Nov 5, 2018

    @amueller
    MemberAuthor

    Basically conditioned on the fact that we can have a lightweight API ;)

  20. amueller commented on Nov 22, 2019

    @amueller
    MemberAuthor

    btw there was a discussion on this topic earlier this year and Sole Galli cited some things from the 2009 KDD competition http://www.mtome.com/Publications/CiML/CiML-v3-book.pdf:

    Page 4 of the summary and introductory article:
    "For categorical variables, grouping of under-represented categories proved to be useful  to avoid overfitting. The winners of the fast and the slow track used similar strategies consisting in retaining the most populated categories and coarsely grouping the others in  an unsupervised way"
    
    Page 23:
    "Most of the learning algorithms we were planning to use do not handle categorical variables,  so we needed to recode them. This was done in a standard way, by generating indicator vari-  ables for the different values a categorical attribute could take. The only slightly non-standard  decision was to limit ourselves to encoding only the 10 most common values of each categorical  attribute, rather than all the values, in order to avoid an explosion in the number of features from  variables with a huge vocabulary"
    
    Page 36:
    "We consolidate the extremely low populated entries  (having fewer than 200 examples) with their neighbors to smooth out the outliers. Similarly, we  group some categorical variables which have a large number of entries (  >  1000 distinct values)  into 100 categories."
    
    See bulletpoints in Page 47
    

    These might provide some additional pointers / references.

  21. self-assigned this
    on Nov 22, 2019
  22. rth commented on Nov 22, 2019

    @rth
    Member

    I currently have a preliminary preprocessor (e.g. a CategoricalPreprocessor) for infrequent categories and NaN handling that I could contribute. Though it only works with dataframes (and relies heavily on pd.Categorical) so maybe it would be more a thing for sklearn-extra.

    It could be worthwhile having a separate estimator for this maybe, even if we keep it private in the end, if only to share code between OHE and OrdinalEncoder?

    I also agree with @glemaitre in #14953 (comment) that it would be good to have a meta issue on the behavior we want for categories in OHE. Right now there are multiple issues and PRs that pull it in different directions (with partially overlapping discussions).

  23. thomasjpfan commented on Dec 4, 2019

    @thomasjpfan
    Member

    @rth I really do want to see all the issues surrounding encoders resolve for 0.23. I do keep a list of all the encoder issues and PRs revolving them. Although, I think placing all the issues into one meta issue may be kind of overwhelming. I think the issue around missing value has the most overlapping PRs and ideas.

    After speaking with @amueller regarding infrequent categories, I propose this API:

    Infrequent One hot encoder

    ohe = OneHotEncoder(sparse=False, handle_unknown='error', min_frequency=10)
    X_train = np.array([['a'] * 5 + ['b'] * 20 + ['c'] * 15 + ['d'] * 2]).T
    ohe.fit(X_train)
    
    X_test = np.array([['a', 'b', 'c', 'd']]).T
    ohe.tranform(X_test)
    # [[0, 0, 1],
    #  [1, 0, 0],
    #  [0, 1, 0],
    #  [0, 0, 1]]
    
    X_test = np.array([['a', 'e']]).T
    ohe.transform(X_test)  # errors because there is an unknown

    Change handle_unknown to 'auto'

    ohe = OneHotEncoder(sparse=False, handle_unknown='auto', min_frequency=10)
    ohe.fit(X_train)
    
    X_test = np.array([['a', 'e']]).T
    ohe.transform(X_test)
    # [[0, 0, 1],
    #  [0, 0, 1]]  # unknown is mapped infrequent bin

    No infrequent categories in training

    ohe = OneHotEncoder(sparse=False, handle_unknown='auto', min_frequency=10)
    X_train = np.array([['a'] * 11 + ['b'] * 20 + ['c'] * 15 + ['d'] * 12]).T
    ohe.fit(X_train)
    
    X_test = np.array([['a', 'e']]).T
    ohe.transform(X_test)
    # [[1, 0, 0, 0],
    #  [0, 0, 0, 0]]  # the same as ignore

    This API uses 'auto' to mean the following:

    1. If there are infrequent categories during fit, during transform, unknown values are mapped to that bin.
    2. If there are no infrequent categories during fit, during transform, unkwnon values will have all zeros. This is the same behavior as handle_unknown='ignore'.

    With this 'auto', we can deprecate 'ignore'.

  24. jnothman commented on Dec 4, 2019

    @jnothman
    Member
  25. rth commented on Dec 4, 2019

    @rth
    Member

    I really do want to see all the issues surrounding encoders resolve for 0.23. I do keep a list of all the encoder issues and PRs revolving them.

    It would be definitely good to have this addressed for the next release. I tried to write a summary in #15796 but you (and other people in this thread) have spent much more time on so don't hesitate to comment/edit if I missed or misunderstood something.

  26. catherinenelson1 commented on Mar 19, 2021

    @catherinenelson1

    I would love to see a min_frequency option in the OneHotEncoder and would use it frequently :-) thanks for all the work on this, and looking forward to seeing it released in the future.

  27. zas97 commented on May 19, 2021

    @zas97

    A hack to create a transformer that does exactly OneHotEncoding with frequency filtering is to use

    CountVectorizer(lowercase=False, min_df = 0.01, max_df=1., token_pattern=r"^.*$")
    

    Where min_df is the min fequency and max_df is the max fequency

  28. glmcdona commented on Nov 29, 2021

    @glmcdona

    This would be a really useful feature. @thomasjpfan, by any chance are you still working on getting this feature merged? Anything you'd like a hand with?

  29. thomasjpfan commented on Nov 29, 2021

    @thomasjpfan
    Member

    Currently, #16018 is basically complete and needs a second approval from a maintainer.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions