Suppose I have a data set which consists a dependent variable
y and independent variables
X. Suppose that there is a specific variable
x which is a categorical variable; suppose that it takes values
best in the training data. I would be inclined to use an ordinal encoder, such as
sklearn.preprocessing. This would map
good --> 1 and
best --> 0, say.
Suppose that the model I'm using requires no
NAs. My hypothetical dataset is lovely and has no
NAs! Grand. I train it.
I now come to the test set. In this, the variable
x sees a new value:
bad. I would, obviously, have wanted to set this to
2. What should I do? Should I look at the entire dataset when encoding? This seems dodgy. Plus, if I add more data in the future, I might run into the same issue: maybe I see
Might this simply be classed as "bad practice". I should make sure that I know all the options in advance so that I can encode them appropriately in the first place.
If I were doing one-hot encoding, such as with
OneHotEncoder, I'd be fine. I'd just write a
0 in the column representing "is
bad?" and be done. But something more intelligent needs to be done with the ordinal version. Is it ok to just stick in a value of
2 retrospectively? Seems dodgy...