# Encoding of categorical data/feature/predictor for binary classification

ML newbie here, currently looking at a binary classification problem. I have quite a good number of training data (easily over 50k) which consists of both numeric and categorical data. The categorical data consists of both ordinal and nominal types.

Here's the problem. I am unsure of what is the most proper way of encoding the categorical data, and what are the factors I should consider when deciding the encoding method. I have came across several encoding methods, which can be summarized in this article.

As additional information, I am thinking of using logistic regression and random forest as my first test classifiers. I have read that certain encoding methods are more suitable for certain types of classifiers. Hope to have more insight on that matter as well.

I hope that you guys/girls can lend me a helping hand. Thank you very much in advance.

EDIT

Due to P&C, I cannot share instances of the data, however these are examples of the categorical features, and the number of different data for each feature:

• Country (nominal) (40)
• Year/Quarter Joined (can be ordinal or nominal) (15)

Library used: scikit-learn

• Most standard classification algorithms work well with many types of variables. As to how you should do to specifically for your data we cannot tell. You should show us the data if we are to give advice. Sep 13 '18 at 7:34
• @user2974951 I have updated my question to the best of my abilities. Hope it helps. Thank you for the tips. Sep 13 '18 at 8:03
• You should mention which software you are using, if it's R or Python then you don't need to bother with this since they can handle this types of data without issues, they handle it internally. Sep 13 '18 at 8:09
• @user2974951 I am currently using python scikit-learn. I understand there are default ways to handle it, but it doesn't mean the default way would be the more "proper" way of handling it, example it wont be able to differentiate between ordinal and nominal data. Unless I'm missing something here, would appreciate if you could provide some details. Thank you. Sep 13 '18 at 8:17
• I don't know about Python, but in R you have the functions factor() and ordinal() to convert variables, so there must be something similar in Python too. As to which variables is which is something that you have to figure out, which will be more useful. Sep 13 '18 at 8:21

If you have nominal (or categorical) variables you should perform a one-hot encoding. With this scheme you create $M$ new variables, where $M$ is the number of unique values in your variable (e.g. above $M$ was $3$). Each of these variables corresponds to one of the values. To encode the data this way, for each sample, you look at the value of the nominal variable and place $1$ to the corresponding new variable you created; the other variables take the value of $0$.
For example, say you have a variable called color and it takes the values "yellow", "red" and "green". These values can't be ordered in any way, so we clearly have a nominal variable. Like we said we now create $3$ new variables, each one corresponding to a value of the nominal (i.e. each one corresponds to a color). If a sample has a color of "red" the new red variable becomes $1$ while the other two are set to $0$.