1
$\begingroup$

In my dataset one column has a large no of categories. For model training or building we need to convert these categories to numeric. There are several methods we can use to convert labels to numeric, one-hot encoding and label encoders, but here my problem is that the categories are not ordinal (no need to preserve order). We can use one-hot encoding, but it creates a large number of dimensions. In this case, which method do I need to choose for converting the categories to numeric effectively? It is not supposed to affect the dimensions of the model.

Please advise on this case. Thanks in advance.

$\endgroup$
2
  • $\begingroup$ Take a look at ordinal encoder on sklearn library $\endgroup$ Commented Jul 14, 2021 at 8:39
  • $\begingroup$ Consider using one-hot encoding. sklearn uses sparse matrix to store the large matrix created and helps optimally use computer memory. $\endgroup$
    – DataFramed
    Commented Jul 15, 2021 at 13:36

3 Answers 3

2
$\begingroup$

Target encoding calculated using an appropriate cross-validation strategy can also be powerful for high-cardinality categorical features.

In some instances, frequency encoding can also be useful.

$\endgroup$
2
$\begingroup$

You could use Binary or BaseN encoding because they are commonly used with high dimensional nominal categories. Because binary or baseN encoding encodes the categories into ordinal numbers and then into binary or base of N respectively in an effective way. They are so efficient in terms of complexity and the dimensionality. I recommend you to read this for more information here. Moreover, you can find here a different number of encodings with the comparison between them here

Note: BaseN is a general encoding and other encodings are a special case of BaseN. For instance, if N=1, this is one-hot encoding. If N=2, this is a binary encoding, and so on.

$\endgroup$
1
$\begingroup$

The standard method to encode a categorical variable is one hot encoding. Replacing categories with numbers (ordinal encoding) would certainly introduce errors in the model because it would rely on numerical comparisons which are meaningless with categorical values.

The high number of dimensions can be a problem if the number of instances is too low and/or the variable has too many rare values. The risk is overfitting: the model would rely on values which happen by chance because it doesn't have a large enough representative sample. In general the solution is to simplify the data: replace rare values (those which occur less than $n$ times in the training set) with a special value other for example. Note that you can adjust the number of dimensions by varying the threshold $n$. It's very likely that there are many rare values and a smaller number of frequent values, therefore this method reduces the number of dimensions quickly. Note that the threshold $n$ can be determined by parameter tuning, but in this case you need a separate validation set (different from the final test set).

Note that you must always define the encoding using only the training data, then apply the predefined encoding on the test set. If the test set contains a value which doesn't exist in the training set, it should similarly be replaced with the special value other.


[edit] Note that all of the above is very generic advice about the potential problems and possible solutions for this case. As usual, it depends a lot on the specifics of the task and data. The only way to find the optimal method is to experiment (Thanks to Sammy for reminding me to mention this).

$\endgroup$
3
  • $\begingroup$ Does simplification work better than label encoding (w/o simplification) in your experience, Erwan? And to which extend you think that is task-, dataset- and model-specific? $\endgroup$
    – Jonathan
    Commented Jul 15, 2021 at 7:22
  • 1
    $\begingroup$ @Sammy thanks for the comment, I updated my answer to mention that it depends on the specifics of the case, indeed. About label encoding I don't think I personally experimented a lot with it for a categorical feature, I tend to assume that this is likely to cause problems for the reason I mentioned. But maybe this a wrong assumption on my part, do you think it's a good option for this case? $\endgroup$
    – Erwan
    Commented Jul 15, 2021 at 10:13
  • $\begingroup$ I think it's really case-dependent. To have an idea I'd need to know the type of feature, what role it plays in the task and the number of unique values. But I've never used simplification with ML even though I use it a lot with "regular" analytics. That's why I've found your suggestion very interesting. $\endgroup$
    – Jonathan
    Commented Jul 16, 2021 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.