Skip to content

Allowing to group infrequent categories in HistGradientBoosting #27947

Open

Description

@jeromedockes

Describe the workflow you want to enable

HistGradientBoostingClassifier and HistGradientBoostingRegressor have built-in support for categorical features and use an OrdinalEncoder to encode them. Each feature must have less than max_bins (255) categories and if there are more we get a ValueError.

Would it be useful to add a parameter allowing to group together the least frequent categories when there are too many (and thus avoid the error)? This amounts to setting max_categories=self.max_bins in the internal OrdinalEncoder.

The workflow I would like to enable is fitting a HGB estimator when a categorical feature has a few too many categories, without needing to encode them myself beforehand.

Describe your proposed solution

the HGB estimators would have a parameter (maybe something like group_infrequent_categories) to control whether they should display the current behavior (raise an error) or group together rare categories (with the OrdinalEncoder's max_categories) when there are more than max_bins categories

Describe alternatives you've considered, if relevant

It is relatively easy to apply an OrdinalEncoder before but (i) it is more verbose, and we have to use a pipeline and probably a ColumnTransformer, (ii) we lose the dtypes of pandas dataframe columns so we have to specify the categorical columns manually instead of using categorical_features="from_dtype", (iii) we end up doing the categorical feature encoding twice, once before and once inside the estimator

Additional context

No response

Activity

  1. betatim commented on Dec 12, 2023

    @betatim
    Member

    I thought we already do this (group infrequent categories when there are more than 255). Maybe we only do this in some other part of scikit-learn though.

    -> I think this would be a useful feature to have.

    It would be nice if we could find a way to build this kind of "infrequent category handling" once and then use it in all the places where we deal with categories. Instead of having to build it several times. I'm not sure how realistic it is to do this, but it would be interesting to explore I think.

  2. jeromedockes commented on Dec 13, 2023

    @jeromedockes
    ContributorAuthor

    the encoders OrdinalEncoder and OneHotEncoder already implement this and it can be used by setting either min_frequency or max_categories, so all pipelines that deal with categories with those encoders can rely on it.
    I'm not sure if that is what you are referring to.

    besides the histogram gradient boosting, one place I see the OneHotEncoding being used is the RandomTreesEmbedding but in this case the number of categories is controlled by the number of trees. After a quick search I didn't find other estimators that use those encoders but there may be others that need to handle categories.

    In the meanwhile the HistGradientBoosting already relies on OrdinalEncoder so the infrequent category handling is already built, AFAICT all that would be needed would be to take advantage of it by setting OrdinalEncoder(max_categories=self.max_bins)

  3. thomasjpfan commented on Dec 13, 2023

    @thomasjpfan
    Member

    I have a PR up for this feature: #26268. API wise it adds a on_high_cardinality_categories to configure between grouping and erroring.

  4. jeromedockes commented on Dec 14, 2023

    @jeromedockes
    ContributorAuthor

    great, sorry I missed it when searching for already existing issues about this!
    (the link above has a typo, here it is: #26268)

  5. removed
    Needs Triage
    on Dec 14, 2023
  6. lorentzenchr commented on May 1, 2026

    @lorentzenchr
    Member
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions