How do Random Forests handle missing values and categorical features in practice?
Last updated
Missing values: scikit-learn ≥ 1.4 handles NaN natively by learning which child missing values go to. R uses proximity-based imputation (
rfImpute). A common default isSimpleImputerplus a missing-indicator column.Categoricals: scikit-learn needs encoding. Ordinal encoding works well for trees, one-hot suits low cardinality, and out-of-fold target encoding suits high cardinality. R and H2O split on category subsets natively.
Fit imputers and encoders inside a
Pipelineto avoid CV leakage, and handle unseen categories withhandle_unknown.