Given data with an ordinal variable, says "house quality" with values ex
(excellent), gd
(good), fa
(fair) and bd
(bad), we obviously cannot just throw data into sklearn's LabelEncoder
as the resulting labels can be in wrong order, e.g. {bd: 3, gd: 2, fa: 1, ex:0}
. Instead, we need to manually specify an order, right? However, if we do not have domain knowledge, how can we specify the order? Also a manual way is usually prone to error. Thus, I am curious if there is any encoder which can auto detect the correct order in an ordinal variable?
$\begingroup$
$\endgroup$
2
-
1$\begingroup$ For me it would beg the question, how without domain knowledge one would even know whether a categorical variable is ordinal or nominal. $\endgroup$– FnguyenCommented Jul 16, 2020 at 7:46
-
$\begingroup$ Point taken @Fnguyen . Let me re-frame my question, i) we know the variable is ordinal e.g. based on its name, ii) we may have partial or full domain knowledge, but we want to avoid the manual process of providing the order, especially when we have lots of categories, says 10-100. $\endgroup$– Victor LuuCommented Jul 16, 2020 at 18:09
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
1
Yup, it should be target encoding. By calculating the target mean for each category, you are ordering the categories based on the target, I don't think of a best way to order them. Of course, this is only valid in a supervised learning setting. If not, I can't think of a way of automatically ordering categories.
See this question and this blogpost to dig deeper in target encoding.
-
$\begingroup$ Thanks @David for a detailed answer, nice blog post. Actually I kind of knowing that target encoding is a solution. Just wonder if there is any other encoding scheme. $\endgroup$ Commented Jul 16, 2020 at 18:14