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
Add "other" / min_frequency option to OneHotEncoder #12153
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.
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
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.
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.
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)
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?
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).
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?
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
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.
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?
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.
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.
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).
@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]).Tohe.fit(X_train)
X_test=np.array([['a', 'b', 'c', 'd']]).Tohe.tranform(X_test)
# [[0, 0, 1],# [1, 0, 0],# [0, 1, 0],# [0, 0, 1]]X_test=np.array([['a', 'e']]).Tohe.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']]).Tohe.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]).Tohe.fit(X_train)
X_test=np.array([['a', 'e']]).Tohe.transform(X_test)
# [[1, 0, 0, 0],# [0, 0, 0, 0]] # the same as ignore
This API uses 'auto' to mean the following:
If there are infrequent categories during fit, during transform, unknown values are mapped to that bin.
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'.
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.
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.
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?
Activity
datajanko commentedon Sep 25, 2018
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
jnothman commentedon Sep 26, 2018
datajanko commentedon Sep 26, 2018
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?
jnothman commentedon Sep 26, 2018
datajanko commentedon Oct 2, 2018
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 fordtype not objectessentially, 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_encodeif 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_valuesbtw)Please apologize my slowness. Feel free to provide any comment, as I'm here to learn.
jorisvandenbossche commentedon Oct 4, 2018
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)
amueller commentedon Oct 4, 2018
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?amueller commentedon Oct 4, 2018
We could put this code in a different class but then add a convenience call into
OneHotEncoder? That might make it also more discoverable.datajanko commentedon Oct 4, 2018
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).
amueller commentedon Oct 4, 2018
I like modularity but I also like convenience ;)
jorisvandenbossche commentedon Oct 4, 2018
I also think that a separate class makes the most sense (at least for the default).
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?
datajanko commentedon Oct 5, 2018
I would suggest to pass through the arguments. Then in
fitandtransform, 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 cumbersomeamueller commentedon Oct 5, 2018
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
OneHotEncodermight 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 amin_frequencyand amin_countparameter for the threshold, and maybemax_levels(max_categories?)Adding all these to
OneHotEncodermight be a bit much.(sorry for rambling lol)
datajanko commentedon Oct 5, 2018
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=Nonekeyword (or similar) and add**kwargsand pass those keyword arguments to thepre_transformerclass?amueller commentedon Oct 5, 2018
yeah let's just go with a separate transformer for now.
5 remaining items
jnothman commentedon Nov 5, 2018
If integer + NaN, convert to float! I think/hope it should be a relatively safe cast in this context...
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.
amueller commentedon Nov 5, 2018
Happy to have this in the OHE transformer. Sorry I've been busy.
amueller commentedon Nov 5, 2018
Basically conditioned on the fact that we can have a lightweight API ;)
amueller commentedon Nov 22, 2019
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:
These might provide some additional pointers / references.
rth commentedon Nov 22, 2019
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 onpd.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).
thomasjpfan commentedon Dec 4, 2019
@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
Change handle_unknown to 'auto'
No infrequent categories in training
This API uses
'auto'to mean the following:handle_unknown='ignore'.With this '
auto', we can deprecate'ignore'.jnothman commentedon Dec 4, 2019
rth commentedon Dec 4, 2019
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.
catherinenelson1 commentedon Mar 19, 2021
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.
zas97 commentedon May 19, 2021
A hack to create a transformer that does exactly OneHotEncoding with frequency filtering is to use
Where min_df is the min fequency and max_df is the max fequency
glmcdona commentedon Nov 29, 2021
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?
thomasjpfan commentedon Nov 29, 2021
Currently, #16018 is basically complete and needs a second approval from a maintainer.