The R implementation of RandomForest can take in categorical features as factors and train and predict on these features without encoding. Normally, I use the python implementation from scikit-learn that requires encoding of categorical data before training. I am trying to determine the best encoding strategy for my problem, which has me wondering, how does the R implementation handle categorical data? I would think it would have to be encoded somehow under the hood. If so, what encoding strategy is used?

  • $\begingroup$ Do you mean „one hot“ or dummy encoding? $\endgroup$
    – Peter
    Oct 23, 2020 at 19:49
  • $\begingroup$ Among others yes. I have some categorical features with high cardinality, and python's random forest isn't handling them well, though R's seems to do better. The R implementation doesn't require you to encode the categorical features, but I'm curious about how it does handle them. $\endgroup$ Oct 23, 2020 at 20:32
  • $\begingroup$ Not knowing what package you use, I suppose that encoding simply is „dummy“ style. In R factors (rdocumentation.org/packages/base/versions/3.6.2/topics/factor) are dummy encoded internally. To my best knowledge this is the same as dummy encoding via a model matrix as far as a data.frame (in contrast to a matrix) is accepted (github.com/Bixi81/R-ml/blob/master/prep_factor_to_dummies.R). So in principle no big difference to Python $\endgroup$
    – Peter
    Oct 23, 2020 at 21:03


Your Answer

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

Browse other questions tagged or ask your own question.