I'm using Spark's Machine Learning Library, and features are categorical. The features are strings, and Spark's MLlib (like many other machine learning libraries) does not accept Strings as inputs.

The normal procedure for overcoming this is to convert Strings to integers, and then encode these integers (using a onehotencoder for example), because converting to integers implies that there is an ordering between features.

My question is - do categorical features always need to be encoded? In what situation could integers be used instead of encoding?

I'm using Logistic Regression and Naive Bayes. When using integers as features I get an 84% accuracy, when these integers are encoded I get an 82% accuracy.

Is it necessary to encode?


1 Answer 1


You have partly answered this question yourself ("because converting to integers implies that there is an ordering between features").

I will just clarify the terminology a bit more.

  1. Categorical data: information has categories, but no natural ordering defined between them (gender, name of user's cat)
  2. Ordinal data: information has categories with natural ordering defined between them (annual income scale defined in terms of categories \$40000, \$40000 - $80000).

If the variable is of type ordinal, you can replace it with integers and proceed with the algorithm. If it is categorical, it should be converted as well as encoded.

Hope this helps.

  • $\begingroup$ Very clear, thank you. I have a mixture of categorical and ordinal data - so will make sure ordinal data is mapped correctly, and will encode the categorical data. $\endgroup$
    – gbhrea
    Sep 13, 2016 at 14:10

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.