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
Preprocessor class OneHotEncoder allows transformation if unknown values are found. It would be great to introduce the same option to OrdinalEncoder. It seems simple to do since OrdinalEncoder (as well as OneHotEncoder) is derived from _BaseEncoder which actually implements handling error policy.
Reacted by Andrew Grigorev, Marina Munkhoeva, Olivier Grisel, Rodrigo Ag, ValdarT, Yaroslav Kopotilov, nskadam, Juan Santos Ochoa, Amol Deshmukh, Luigi Filippo Chiara and 6 more
No, #12264 is not my case but it is desirable too. I would like OrdinalEncoder do not throw an exception if it meets an unexpected value. I mean that unexpected value is value which categories does not contain.
handle_unknown : ‘error’ or ‘ignore’, default=’error’.
Whether to raise an error or ignore if an unknown categorical feature is present during transform (default is to raise). When this parameter is set to ‘ignore’ and an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature will be all zeros. In the inverse transform, an unknown category will be denoted as None.
I could ignore unknown catregiries value as OneHotEncoder does. Another possible scenario (say sentinel) could replace unknown value with default one which could be specified in OrdinalEncoder's constructor.
Reacted by Nathaniel, MBrouns, Amol Deshmukh, Mark and Pedro Prates
The policy of OnehotEncoder for unknown categories is to set all the columns to 0. That wouldn't be great for OrdinalEncoder since unknown categories and the first one (i.e. zero) would be mixed.
Allowing a fallback category value seems reasonable to me.
@daskol could you please describe your particular use-case that motivates this change?
We discussed the issue with @jorisvandenbossche and I think the sanest strategy would be to have:
min_frequency=5 (5 is an example, the default could be 1) to set the threshold to collapse all categories that appear less than 5 times in the training set into a virtual category
rare_category="rare_value" as a parameter to control the name of the virtual category used to map all the rare values. This will be mostly be useful for inverse_transform and for consistency if we implement a similar category collapsing option in other categorical variable preprocessors such as OneHotEncoder and ImpactEncoder / TargetEncoder (whatever its name).
handle_unknown="treat_as_rare" that would map any inference-time unknown categories to the integer that is mapped to the virtual rare_category (even when its never used on the training set).
IMO handle_unknown="treat_as_rare" is the sanest way to handle inference-time unknown values from a statistical point of view.
Reacted by Joel Nothman, Thomas J. Fan, Roman Yurchak, crazycodingcat and Chatdanai Lumdee
Personally I find this issue really annoying. At the moment we cannot use OrdinalEncoder on data with a long-tailed distribution of categorical variable frequencies in a cross validation loops without triggering the unknown category exception at prediction time.
Reacted by Joel Nothman, Daniel Bershatsky, ValdarT, How, Roman Yurchak, JASON FOCHO , Kevin Markham, Yaroslav Kopotilov, Janet Liu, Ben Reiniger and 11 more
also see #12153 for something related. I think min_frequency is good, but I also want max_levels or something like that. Basically we could reimplement all the different pruning options we have in CountVectorizer....
If we add a “rare” category to OrdinalEncoder it seems like it goes against the “Ordinal” part of the encoder. It assumes the rare or unknown category has the highest (or lowest) value. If we do introduce this, it would be good to document this behavior.
Ideally, if OrdinalEncoder can handle most of the logic that has to deal with unknown and infrequent categories, OneHotEncoder would need to do less. (I am thinking of composition ie OneHotEncoder has a OrdinalEncoder)
Hi! I wanted to add that it seems to me that the feature of allowing the encoder to be fit on data that contains some categories, and then applied to data that contains maybe an additional category or two, seems like a common use case for all kinds of categorical data. Even if one category isn't very uncommon, if you're doing a random split for training and validation it's only a matter of time before this error comes up. I like the "min_frequency" solution for its generality, but to (naive) me, it seems too complicated. To me, it seems the default behavior should be to send all categories not present in the original fitting to a single virtual category. Or maybe a "create_virtual_category = True" option. If this is amenable, I'd be happy to take a crack at making it, I'm trying to spend more time working on open source code!
I didn't pick that up from the docs.. my understanding was that the encoder assigned essentially arbitrary integer values to each category, but you're saying it assigns them maybe based on frequency or something else which means that order matters? If that's the case, I'd like to suggest an update to the docs because I didn't quite understand that from the docs. If it's not, then couldn't we just stick the virtual category at the end of the original ordinal categories?
No, I just mean that if you want an ordinal encoding, rather than one-hot,
it will often be because order matters. You're certainly welcome to help us
improve the documentation by submitting specific changes as a pull request.
I think the OrdinalEncoder is weird because it is indeed the intention that the order matters - that's why it's called OrdinalEncoder. But the order is always lexical, which rarely makes sense. I have a hard time coming up with usage scenarios for OrdinalEncoder because of that.
I guess the user could always rename the their categories so that lexical ordering is useful. But then what's the point of the encoder?
If we say "it's ordered but the order is arbitrary" that might make it easier to reason about it?
@ogrisel iirc you were one of the people that wanted this encoder. Can you give examples of your motivation?
I totally agree @amueller. I was using it for preprocessing data before LightGBM, which requires integer data, but you can flag certain columns as categorical and so the order does NOT matter. I am confused about what "order matters" means because by its very nature, there is no correct order of the classes. I'm not sure that an "order matters" OrdinalEncoder makes sense to me, when no order is specified.
I think we never intended the behaviour to always require lexicographic
ordering... we just haven't got around to fixing that (taking order
specified by user, or from a pandas categorical dtype ordering). Should
that be a priority?
Friends, may you speed up this fix
since in real live, there are often many differences between train and data test?
as stated above by @ogrisel : Personally I find this issue really annoying
Can you do at least this as @daskol wrote above
I could ignore unknown catregiries value as OneHotEncoder does. Another possible scenario (say sentinel) could replace unknown value with default one which could be specified in OrdinalEncoder's constructor
yes just ignore unknown catregiries for transform - replace unknown value with default one which could be specified in OrdinalEncoder's constructor
or set to none...
but please do something!!!!
Reacted by Amol Deshmukh, Luigi Filippo Chiara, Kat, Aydin Gerek and Sachin Puranik
Order is not important
The idea is to make it practicable
Since for now it is not practicable distilled case
Meaning , in real data there are always new categories in test data, ( categories unseen in training data)
Then ordinal encoder just crashes
Pls add capabilities to handle cases when test data and training data have different categories
Meaning
Test data have categories that are not present in train data
I think that this would be a really helpful change. Given the choice of a default virtual encoding for unseen values, I would rather it be the first value as opposed to the last value.
I feel that this is intuitive because you always know where the first value is without looking (e.g. if the default encoding were -1 or 0).
On a practical use case, I am converting categorical data to numeric data to use with the RandomForestRegressor.
Some of my categories are quite small (~5 types) while some are larger (~5000 types).
I would like to use the OneHotEncoder on my smaller types because it will make my features more interpretable when I look at their permutation importance.
I would like to use the OrdinalEncoder on my larger categories because it will make actions like permutation importance less computationally expensive and my model choice will be robust to the effect of the OrdinalEncoder's choice of ordering the features.
However, I will no doubt encounter a number of examples in my test data for the larger categories that are not present in my training dataset, so if I use the OrdinalEncoder it will throw an error.
My other alternative is to do something hacky with Pandas or the DictVectorizer.
No, I just mean that if you want an ordinal encoding, rather than one-hot, it will often be because order matters.
That's not true though. Often times people use ordinalencoder because onehotencoder would expand the feature array too much and so filling in a variable with arbitrary numbers is a more space-efficient option that doesn't make too much of a difference in tree ensembles.
I encountered a similar need. I was hoping to use sklearn to encode categorical features prior to passing them to an Embedding layer in keras. In this case, the order of the categorical features does not matter, and it would be helpful to have the ability to make any unknown categories encountered at transform time be encoded by the same integer.
My solution for myself was to make a new encoder subclass I called CardinalEncoder. Its __init__ has a handle_unknown (bool) parameter. If True, then fit will fit a copy of X in which the final element is replaced with 'Unknown'. During transform, the original form of X is encoded. If the final element in X is unique, then no data loss will occur on a fit_transform call, but some data loss may occur on subsequent transforms if the data includes instances of the unique final fitted element as well as other unknown elements. That was an acceptable trade-off for me, as the likelihood of X's final element being unique was very low for my data.
Activity
jnothman commentedon Mar 21, 2019
daskol commentedon Mar 21, 2019
No, #12264 is not my case but it is desirable too. I would like
OrdinalEncoderdo not throw an exception if it meets an unexpected value. I mean that unexpected value is value whichcategoriesdoes not contain.jnothman commentedon Mar 22, 2019
daskol commentedon Mar 22, 2019
I could ignore unknown catregiries value as
OneHotEncoderdoes. Another possible scenario (saysentinel) could replace unknown value with default one which could be specified inOrdinalEncoder's constructor.NicolasHug commentedon Mar 22, 2019
The policy of
OnehotEncoderfor unknown categories is to set all the columns to 0. That wouldn't be great forOrdinalEncodersince unknown categories and the first one (i.e. zero) would be mixed.Allowing a fallback category value seems reasonable to me.
@daskol could you please describe your particular use-case that motivates this change?
ogrisel commentedon Mar 27, 2019
We discussed the issue with @jorisvandenbossche and I think the sanest strategy would be to have:
min_frequency=5(5 is an example, the default could be 1) to set the threshold to collapse all categories that appear less than 5 times in the training set into a virtual categoryrare_category="rare_value"as a parameter to control the name of the virtual category used to map all the rare values. This will be mostly be useful forinverse_transformand for consistency if we implement a similar category collapsing option in other categorical variable preprocessors such asOneHotEncoderandImpactEncoder/TargetEncoder(whatever its name).handle_unknown="treat_as_rare"that would map any inference-time unknown categories to the integer that is mapped to the virtualrare_category(even when its never used on the training set).IMO
handle_unknown="treat_as_rare"is the sanest way to handle inference-time unknown values from a statistical point of view.ogrisel commentedon Mar 27, 2019
Personally I find this issue really annoying. At the moment we cannot use
OrdinalEncoderon data with a long-tailed distribution of categorical variable frequencies in a cross validation loops without triggering the unknown category exception at prediction time.amueller commentedon Apr 2, 2019
also see #12153 for something related. I think min_frequency is good, but I also want max_levels or something like that. Basically we could reimplement all the different pruning options we have in CountVectorizer....
thomasjpfan commentedon May 10, 2019
If we add a “rare” category to
OrdinalEncoderit seems like it goes against the “Ordinal” part of the encoder. It assumes the rare or unknown category has the highest (or lowest) value. If we do introduce this, it would be good to document this behavior.Ideally, if OrdinalEncoder can handle most of the logic that has to deal with unknown and infrequent categories, OneHotEncoder would need to do less. (I am thinking of composition ie
OneHotEncoderhas aOrdinalEncoder)nathanielmhld commentedon Jul 30, 2019
Hi! I wanted to add that it seems to me that the feature of allowing the encoder to be fit on data that contains some categories, and then applied to data that contains maybe an additional category or two, seems like a common use case for all kinds of categorical data. Even if one category isn't very uncommon, if you're doing a random split for training and validation it's only a matter of time before this error comes up. I like the "min_frequency" solution for its generality, but to (naive) me, it seems too complicated. To me, it seems the default behavior should be to send all categories not present in the original fitting to a single virtual category. Or maybe a "create_virtual_category = True" option. If this is amenable, I'd be happy to take a crack at making it, I'm trying to spend more time working on open source code!
jnothman commentedon Jul 31, 2019
nathanielmhld commentedon Jul 31, 2019
I didn't pick that up from the docs.. my understanding was that the encoder assigned essentially arbitrary integer values to each category, but you're saying it assigns them maybe based on frequency or something else which means that order matters? If that's the case, I'd like to suggest an update to the docs because I didn't quite understand that from the docs. If it's not, then couldn't we just stick the virtual category at the end of the original ordinal categories?
jnothman commentedon Jul 31, 2019
amueller commentedon Aug 2, 2019
I think the
OrdinalEncoderis weird because it is indeed the intention that the order matters - that's why it's calledOrdinalEncoder. But the order is always lexical, which rarely makes sense. I have a hard time coming up with usage scenarios forOrdinalEncoderbecause of that.I guess the user could always rename the their categories so that lexical ordering is useful. But then what's the point of the encoder?
If we say "it's ordered but the order is arbitrary" that might make it easier to reason about it?
@ogrisel iirc you were one of the people that wanted this encoder. Can you give examples of your motivation?
nathanielmhld commentedon Aug 2, 2019
I totally agree @amueller. I was using it for preprocessing data before LightGBM, which requires integer data, but you can flag certain columns as categorical and so the order does NOT matter. I am confused about what "order matters" means because by its very nature, there is no correct order of the classes. I'm not sure that an "order matters" OrdinalEncoder makes sense to me, when no order is specified.
jnothman commentedon Aug 3, 2019
jnothman commentedon Aug 3, 2019
NicolasHug commentedon Aug 4, 2019
I think we should force the user to pass the order of the categories when they are strings. I opened #14563
Sandy4321 commentedon Feb 12, 2020
Friends, may you speed up this fix
since in real live, there are often many differences between train and data test?
as stated above by @ogrisel : Personally I find this issue really annoying
Can you do at least this as @daskol wrote above
I could ignore unknown catregiries value as OneHotEncoder does. Another possible scenario (say sentinel) could replace unknown value with default one which could be specified in OrdinalEncoder's constructor
yes just ignore unknown catregiries for transform - replace unknown value with default one which could be specified in OrdinalEncoder's constructor
or set to none...
but please do something!!!!
jnothman commentedon Feb 12, 2020
Sandy4321 commentedon Feb 12, 2020
Order is not important
The idea is to make it practicable
Since for now it is not practicable distilled case
Meaning , in real data there are always new categories in test data, ( categories unseen in training data)
Then ordinal encoder just crashes
Pls add capabilities to handle cases when test data and training data have different categories
Meaning
Test data have categories that are not present in train data
chutcheson commentedon Jun 5, 2020
I think that this would be a really helpful change. Given the choice of a default virtual encoding for unseen values, I would rather it be the first value as opposed to the last value.
I feel that this is intuitive because you always know where the first value is without looking (e.g. if the default encoding were -1 or 0).
On a practical use case, I am converting categorical data to numeric data to use with the RandomForestRegressor.
Some of my categories are quite small (~5 types) while some are larger (~5000 types).
I would like to use the OneHotEncoder on my smaller types because it will make my features more interpretable when I look at their permutation importance.
I would like to use the OrdinalEncoder on my larger categories because it will make actions like permutation importance less computationally expensive and my model choice will be robust to the effect of the OrdinalEncoder's choice of ordering the features.
However, I will no doubt encounter a number of examples in my test data for the larger categories that are not present in my training dataset, so if I use the OrdinalEncoder it will throw an error.
My other alternative is to do something hacky with Pandas or the DictVectorizer.
nickcorona commentedon Jun 7, 2020
That's not true though. Often times people use ordinalencoder because onehotencoder would expand the feature array too much and so filling in a variable with arbitrary numbers is a more space-efficient option that doesn't make too much of a difference in tree ensembles.
jdraines commentedon Jun 28, 2020
I encountered a similar need. I was hoping to use sklearn to encode categorical features prior to passing them to an Embedding layer in keras. In this case, the order of the categorical features does not matter, and it would be helpful to have the ability to make any unknown categories encountered at
transformtime be encoded by the same integer.My solution for myself was to make a new encoder subclass I called CardinalEncoder. Its
__init__has ahandle_unknown(bool) parameter. If True, thenfitwill fit a copy of X in which the final element is replaced with 'Unknown'. During transform, the original form of X is encoded. If the final element in X is unique, then no data loss will occur on afit_transformcall, but some data loss may occur on subsequenttransformsif the data includes instances of the unique final fitted element as well as other unknown elements. That was an acceptable trade-off for me, as the likelihood of X's final element being unique was very low for my data.My solution is at:
https://github.com/jdraines/cardinal_encoder/blob/master/cardinal_encoder.py
Sandy4321 commentedon Jun 29, 2020
Great idea
But what scikit learn people resistant to do it?
Why to do against?
Sandy4321 commentedon Aug 14, 2020
jdraines/cardinal_encoder: Implements a Scikit-Learn CardinalEncoder which differs from OrdinalEncoder in that it handles unknowns.
Is great