# Categorical features in a random forest

I am currently training a random forest. After transforming a categorical feature into dichotomous columns, should I drop the first level?

For example, I have three unique values in a featured named sex:

1. m for male
2. f for female
3. na for not available

Thus, I encoded sex into three columns:

sex  sex_m  sex_f  sex_na
m      1      0       0
f      0      1       0
na      0      0       1


I dropped sex (obviously), but should I also drop one of the three encoded columns?

Dropping the base level is necessary when running a regression to avoid multicollinearity, but this is not a problem when running a random forest. So what is the most common approach?

For reference, each tree is being trained with a randomly selected set of 8 out of 63 features.

• Decision trees can usually cope directly with categorical variables May 29, 2020 at 15:24
• This answer is provided in the context of gradient boosting, but the logic also applies to random forest. stats.stackexchange.com/questions/438875/…
– Sycorax
May 29, 2020 at 15:37
• May 30, 2020 at 18:22

Integer coding often does the job pretty well. The more levels, the more it helps to use a meaningful order. Some implementations - e.g. ranger in R - do smart ordering, internally. Avoiding dummy coding also greatly helps to interprete the models by the usual suspects (variable importance, partial dependence plots).