# Categorical Variables - Classification

I have a categorical variable, country which takes on values like India, US, Pakistan etc. I am currently using a linear SLM for a classification task.

So my country value varies from 1-20. How should this be a feature in the classification task. Should i have a one hot vector like (1,0,0..) for us and assign this vector 20 weights, or should i have integer from 1_20 and assign a single weight? I am using scikit learn. Does the answer depend on classifier?

## 3 Answers

The answer depends less on the classifier and more on the nature of the variable. In your case One Hot Encoding might be the best answer.

Label Encoding (Replacing categorical variables with integers) is useful when the variable is ordinal, i.e. it has a sense of order. For example the days of the week or the months of the year. Since they follow a fixed order, you can encode January as 1 and February as 2 and so on. The classifier would interpret Feb as being greater than Jan in some way (which is okay for a task like weather prediction and so on).

Can your countries be considered to be ordinal? If not, One Hot Encode them.

• Even in month, for ordinal encoding to make sense, should there not be some sort of continous variation with months? i.e like as month increase uniformly, y increases? – Sridhar Thiagarajan Jun 18 '17 at 19:59
• That correlation would be necessary only if you use a strictly linear classifier/regressor. Non linear algorithms won't be bothered by the lack of correlation. For the weather prediction example, a decision tree would be able to identify that it's hot in months 4 to 8 and cold in 1 to 3 and 11 to 12. So would an SVM with non linear kernels or a Neural Network with one or more hidden layers. A logistic regression model may not be successful though. – Adarsh Chavakula Jun 19 '17 at 5:13

Make a label for all your available countries. Your Y(predictive) value will be a vector of the length = no of countries. Having just one label and varying its value will affect the accuracy as it is not a linear regression problem, you need the class labels instead of the values. Country1 - Country2 != Country3 - Country4

Another possibility is to order you country-variable by the rate of the target variable.

For ex. in the case of the binary classification problem (0-1), you can order your countries by rate of the majority class and then assign the number from 1 to 20. This coding allow you to make the logical operations with this variable.