176
$\begingroup$

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?

$\endgroup$
3
  • 3
    $\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
    Commented Jan 23, 2018 at 9:16
  • 1
    $\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
    Commented Jun 26, 2018 at 11:24
  • 1
    $\begingroup$ Here's a visual example of the negative effect of using LabelEncoder on categorical features $\endgroup$
    – yatu
    Commented May 5, 2020 at 12:57

4 Answers 4

212
$\begingroup$

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.

$\endgroup$
5
  • 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
    Commented Dec 21, 2015 at 20:36
  • 1
    $\begingroup$ @AN6US, can you eloborate your point about "orthogonal vector space"? $\endgroup$
    – A.B
    Commented Dec 23, 2019 at 3:40
  • $\begingroup$ "Still there are algorithms like decision trees and random forests that can work with categorical variables just fine" what do you mean "just fine". Once you Label Encode it essentially treats the column as ordered category. You are unable to specify non-ordered categories. $\endgroup$
    – Pandian Le
    Commented Jan 23, 2021 at 13:30
  • 1
    $\begingroup$ How about Target Encoder $\endgroup$
    – Mohith7548
    Commented Jan 27, 2021 at 7:35
  • $\begingroup$ @A.B - orthogonal vectors are vectors that act independently of each other. In this case a column with dog, cat $\endgroup$ Commented Jul 25, 2023 at 9:43
56
$\begingroup$

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

Let's 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.

$\endgroup$
4
  • 1
    $\begingroup$ Very intuitive explanation. Shouldn't it be "splits", not "spilts"? $\endgroup$
    – dzieciou
    Commented May 29, 2019 at 15:30
  • 2
    $\begingroup$ So sorry for that, ill edit that out $\endgroup$ Commented Dec 17, 2019 at 5:03
  • $\begingroup$ Would you guys call cabin classes ordinal? Economy, Premium Economy and Business. They seem like they could go either way. What about flight routes like short-haul long-haul? Or even passenger types as frequent flyer and new Seems like these decisions are subjective. Also, when we use OHE the norm is to drop one of the cardinal values at the same time. But what happens when we create our train/test X,y variables? The columns were dropped so it's not the original data frame. Is it totally fine to keep all the cardinal values of the columns we performed OHE on? $\endgroup$
    – Edison
    Commented Jul 2, 2022 at 11:37
  • $\begingroup$ So its the classification of ordinal and non-ordinal can get very subjective (like in your case), ill suggest trying both approaches and see which one gives the least test-set error. $\endgroup$ Commented Jul 7, 2022 at 15:49
6
$\begingroup$

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

$\endgroup$
2
$\begingroup$

You can also use frequency encoding in which you map values to their frequencies

Example taken from How to Win a Data Science Competition from Coursera, eg. for titanic dataset:

   encoding = titanic.groupby('Embarked').size()
   encoding = encoding/len(titanic)   // calculates frequency
   titanic['enc'] = titanic.embarked.map('Encoding')

This will preserve info about values distribution and will help both linear and tree based models. Make sure you don't have multiple categories with same frequencies, you can use rank operation instead or any other relevant operation.

$\endgroup$

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