Which supervised model families require explicit encoding of categorical inputs?
Last updated
Any model that computes with numeric arithmetic on inputs needs categories encoded: linear/logistic regression, SVMs, k-NN, and neural networks. Some tree implementations (LightGBM, CatBoost, sklearn HistGradientBoosting) and Naive Bayes can consume categories natively, while classic sklearn trees still need numeric input.
Linear and logistic regression, and GLMs: Label codes (0, 1, 2) would impose a false order and equal spacing, so use one-hot encoding (dropping one level if unregularized, to avoid collinearity).
Distance and kernel models (k-NN, SVM): Distances need numeric vectors. One-hot encoding plus scaling is typical, or a categorical-aware metric (e.g. Hamming or Gower distance).
Neural networks: Use one-hot for low-cardinality features and learned embeddings (
nn.Embedding) for high-cardinality ones like user or item IDs.Tree models: it depends on the implementation:
Native support:
LightGBM(categorical_feature),CatBoost(ordered target statistics), andHistGradientBoosting(categorical_features), which split on subsets of categories.sklearn
DecisionTreeandRandomForestneed numeric input. Ordinal encoding often works acceptably here because trees can isolate codes with multiple splits.
Naive Bayes: Categorical NB models per-category frequencies directly (
CategoricalNBstill expects integer codes, but no one-hot expansion is needed).High cardinality: one-hot explodes dimensionality, so consider target encoding (computed out-of-fold to avoid leakage), hashing, or embeddings.