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
Allowing to group infrequent categories in HistGradientBoosting #27947
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
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.
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)
Activity
betatim commentedon Dec 12, 2023
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.
jeromedockes commentedon Dec 13, 2023
the encoders
OrdinalEncoderandOneHotEncoderalready implement this and it can be used by setting eithermin_frequencyormax_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
RandomTreesEmbeddingbut 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)thomasjpfan commentedon Dec 13, 2023
I have a PR up for this feature: #26268. API wise it adds a
on_high_cardinality_categoriesto configure between grouping and erroring.jeromedockes commentedon Dec 14, 2023
great, sorry I missed it when searching for already existing issues about this!
(the link above has a typo, here it is: #26268)
lorentzenchr commentedon May 1, 2026
See also #18894 (comment).