I have been building models with categorical data for a while now and when in this situation I basically default to using scikit-learn's LabelEncoder function to transform this data prior to building a model.

I understand the difference between OHE, LabelEncoder and DictVectorizor in terms of what they are doing to the data, but what is not clear to me is when you might choose to employ one technique over another.

Are there certain algorithms or situations in which one has advantages/disadvantages with respect to the others?

  • 2
    $\begingroup$ In reference to AN6U5's answer, and this statement: > Still there are algorithms like decision trees and random forests that can work with categorical variables just fine and LabelEncoder can be used to store values using less disk space. Wouldn't using LabelEncoder transform a categorical to a numeric feature, thereby causing a decision tree to perform splits at some value which don't really make sense since the mapping is arbitrary? $\endgroup$ – Nico Jan 23 '18 at 9:16
  • $\begingroup$ Nico, I think what AN6U5 is saying is specifically for decision trees it works fine, because the tree would split on dog,cat,mouse or 1,2,3 and the meaning of the "cat" vs "2" is not important for a tree (think about the way it splits). In the case of something like logistic regression, the values are part of an equation since you multiply the weight*values so it could cause training issues and weight issues given that dog:1 and cat:2 has no numeric 1*2 relationship (though it can still work with enough training examples and epochs). $\endgroup$ – Doug F Jun 26 '18 at 11:24

There are some cases where LabelEncoder or DictVectorizor are useful, but these are quite limited in my opinion due to ordinality.

LabelEncoder can turn [dog,cat,dog,mouse,cat] into [1,2,1,3,2], but then the imposed ordinality means that the average of dog and mouse is cat. Still there are algorithms like decision trees and random forests that can work with categorical variables just fine and LabelEncoder can be used to store values using less disk space.

One-Hot-Encoding has the advantage that the result is binary rather than ordinal and that everything sits in an orthogonal vector space. The disadvantage is that for high cardinality, the feature space can really blow up quickly and you start fighting with the curse of dimensionality. In these cases, I typically employ one-hot-encoding followed by PCA for dimensionality reduction. I find that the judicious combination of one-hot plus PCA can seldom be beat by other encoding schemes. PCA finds the linear overlap, so will naturally tend to group similar features into the same feature.

  • 1
    $\begingroup$ Thank you very much - this is very helpful and makes a lot of sense. Are there any other encoding schemes you use for specific/edge cases? Do you ever find that you're in a situation where you'll use different encoding schemes for different features? $\endgroup$ – anthr Dec 21 '15 at 20:36

While AN6U5 has given a very good answer, I wanted to add a few points for future reference. When considering One Hot Encoding(OHE) and Label Encoding, we must try and understand what model you are trying to build. Namely the two categories of model we will be considering are:

  1. Tree Based Models: Gradient Boosted Decision Trees and Random Forests.
  2. Non-Tree Based Models: Linear, kNN or Neural Network based.

Let's consider when to apply OHE and when to apply Label Encoding while building tree based models.

We apply OHE when:

  1. When the values that are close to each other in the label encoding correspond to target values that aren't close (non - linear data).
  2. When the categorical feature is not ordinal (dog,cat,mouse).

We apply Label encoding when:

  1. The categorical feature is ordinal (Jr. kg, Sr. kg, Primary school, high school ,etc).
  2. When we can come up with a label encoder that assigns close labels to similar categories: This leads to less spilts in the tress hence reducing the execution time.
  3. When the number of categorical features in the dataset is huge: One-hot encoding a categorical feature with huge number of values can lead to (1) high memory consumption and (2) the case when non-categorical features are rarely used by model. You can deal with the 1st case if you employ sparse matrices. The 2nd case can occur if you build a tree using only a subset of features. For example, if you have 9 numeric features and 1 categorical with 100 unique values and you one-hot-encoded that categorical feature, you will get 109 features. If a tree is built with only a subset of features, initial 9 numeric features will rarely be used. In this case, you can increase the parameter controlling size of this subset. In xgboost it is called colsample_bytree, in sklearn's Random Forest max_features.

In case you want to continue with OHE, as @AN6U5 suggested, you might want to combine PCA with OHE.

Lets consider when to apply OHE and Label Encoding while building non tree based models.

To apply Label encoding, the dependance between feature and target must be linear in order for Label Encoding to be utilised effectively.

Similarly, in case the dependance is non-linear, you might want to use OHE for the same.

Note: Some of the explanation has been referenced from How to Win a Data Science Competition from Coursera.

  • $\begingroup$ Very intuitive explanation. Shouldn't it be "splits", not "spilts"? $\endgroup$ – dzieciou May 29 at 15:30

LabelEncoder is for ordinal data, while OHE is for nominal data.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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