# What is the best way to handle ordinal features having numeric values in python? [closed]

What is the best way to encode ordinal feature?

1. Is it by transforming it using OneHotEncoder so values going from 1 to 7 lets say would become head of new field feature.

2. Or by using StandardScaler() to scale the values between -1 and 1 ?

• What do you mean by "handle"? What problem are you trying to solve? How does leaving the features as-is not solve that problem? – Sycorax says Reinstate Monica Oct 10 at 14:22

The problem isn't really with how you scale it but with what you do with it once it's scaled (if you scale it at all).

There are two usual methods of dealing with ordinal independent variables: 1) Treat them as continuous (both methods of scaling that you propose seem to do this) or 2) Treat them as categorical and ignore the ordering.

The problem with 1) is that it may not be reasonable. It assumes that you can impose some sort of intervals on the ordinal data. The two scalings impose different intervals, but both impose an interval. That might be reasonable (e.g. it's usually reasonable with Likert scale data) but it might not (e.g. if 1 is 10 times a day, 2 is "a few times a day" .... and 7 is "never").

The problem with 2) is that it ignores the ordering altogether.

If you are doing regression and this is an independent variable, you could look into optimal scaling. I don't know if it's available in Python, but it's in R (optiscale package) and SAS (PROC TRANSREG).

If you are doing something like chi-square, then you could look into the Jonckheere Terpstra test, which I think is quite useful for this sort of thing.

And, for any particular ordinal scale, there may be some particular sensible scaling.

• I can't find anything similar in python. Anyway, in my case, the values goes from 1 to 7 whereas 1 is very bad and 7 is very good. Can we simply scale it between [0, 1] interval ? – alim1990 Oct 11 at 5:28
• There doesn't seem to me to be any benefit to doing so. – Peter Flom - Reinstate Monica Oct 11 at 11:10

It depends on the problem you are trying to solve, more importantly what these features are representing.

Let's say they are representing floors in a building it's okay to map it to [-1, 1] which will help in learning weights quickly.

But, if they are representing days of a week then it's difficult for any model trained on this data to identify that 1st day(-1) comes after 7th day(1), and model won't perform well if this information is required to identify/calculate the output. In this case one hot encoder will help, it just gives the model to identify the pattern that event happened on a particular day. and if you pass it like an embedding layer, it might even find the relation b/n days in terms of output(large data required).

• I have a field which is numerical but it is considered as ordinal as its values varies from 1 to 7, whereas 1 is very good and 7 represent very bad. Should I re-classify them into smaller values or split them using hot encoder? Or numeric at the end is numeric whether its ordinal or un-ordered numbered values? – alim1990 Oct 10 at 5:02
• Here scaling it to [-1, 1] makes sense, It gives continuous measure of how good it's. Even though it can learn for -1 as good(ordinal 1) and 1 as bad(ordinal 7), if your activation functions are ReLU or similar it's better to try out other way by reversing it as well. Also tryout mapping from [0 to 1] or [-1 to 2] or [-2 to 1], which can improve performance and learn the training quickly. – yugandhar Oct 10 at 9:39