# When to use One Hot Encoding vs LabelEncoder vs DictVectorizor?

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?

• 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? – Nico Jan 23 '18 at 9:16
• 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). – Doug F Jun 26 '18 at 11:24
• Here's a visual example of the negative effect of using LabelEncoder on categorical features – yatu May 5 at 12:57

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.

• 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? – anthr Dec 21 '15 at 20:36
• @AN6US, can you eloborate your point about "orthogonal vector space"? – A.B Dec 23 '19 at 3:40

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 splits 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.

• Very intuitive explanation. Shouldn't it be "splits", not "spilts"? – dzieciou May 29 '19 at 15:30
• So sorry for that, ill edit that out – Pushkaraj Joshi Dec 17 '19 at 5:03

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