You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decision trees cannot handle categorical features natively #354
From community criticism review. This is the most repeated concrete algorithmic complaint after the logistic default: it appears in the Reddit thread in detail and in three separate G2 reviews.
u/111llI0__-__0Ill111 (r/datascience):
Then the decision tree algo in sklearn also sucks. They admit actually categorical variables CANNOT be handled as is. People will often label encode them which is completely bogus as that assumes ordinality. While one hot encoding increases cardinality and degrades performance (look it up). Even Julia, a newer language, has the proper Decision Tree algorithm in DecisionTrees.jl.
G2:
have issues when we have categorical attributes in variables - encoding them to binary or one hot encoded will not solve the issue. Many of the recent technologies like h20, tensor flow gives the ability to inout categorical attributes as inputs to algorithm
Current state
grep -n 'categorical' lib/scikit/tree.flow returns nothing. flow-scikit inherits the defect exactly: every feature is treated as ordered numeric, so a user must label-encode (imposing false ordinality) or one-hot encode (inflating cardinality).
What a fix means
A split on a categorical feature is a subset partition, not a threshold. The standard tractable approach for the binary-outcome case is Breiman's: sort categories by mean response and evaluate the k-1 contiguous splits, which is provably optimal for binary classification and regression. That needs a per-feature categorical flag threaded through DecisionTreeClassifier/DecisionTreeRegressor fit and through the node representation so predict can evaluate set membership.
Named counterexamples from the sources: CatBoost, LightGBM, H2O, DecisionTrees.jl, R.
Note this would also propagate to RandomForest and the Bagging estimators, which build on the same tree code.
PR #441 implements this for DecisionTreeClassifier and DecisionTreeRegressor.
Representation: TreeNode gains cat_kind plus a 128-bit inline left set in cat_mask0..3. cat_kind is 0 for a numeric split and node arrays are calloc'd, so every node any existing builder writes is a numeric node. Keeping the set inline rather than in a side table on the model leaves DecisionTreeClassifier and DecisionTreeRegressor at the field lists they already have, which the reference fits under tests/ and ensemble.flow's tree arrays depend on. sizeof(TreeNode) goes 32 to 52, so every literal byte count for a node array goes 36 to 64.
Threading: decision_tree_classifier_fit_categorical and decision_tree_regressor_fit_categorical take an X.cols mask; the existing fits call them with an all-zero mask, so there is one implementation and the old signatures are untouched.
Algorithm: Breiman's, k-1 prefixes of the levels ordered by mean response. Exact for regression and two-class classification; a heuristic beyond two classes, and the code says so rather than implying optimality it does not have.
Numeric behaviour is bit-identical, verified node by node: 245 DecisionTree digits nodes, 64 feature importances, 360 RandomForest digits predictions and 113 regressor nodes, 795 lines, zero diff. DecisionTree digits 0.833333313 and RandomForest digits 0.955555558 unchanged.
Not covered, and worth a follow-up issue: RandomForest, the Bagging estimators and the ExtraTree builders. They go through their own builders, never set cat_kind, and ensemble.flow has three sites that walk nodes inline rather than through predict_one. Each of those would need the subset test, and the RF builder does not presort, so its categorical scan is a separate piece of work.
Activity
godofecht commentedon Aug 19, 2026
PR #441 implements this for
DecisionTreeClassifierandDecisionTreeRegressor.Representation:
TreeNodegainscat_kindplus a 128-bit inline left set incat_mask0..3.cat_kindis 0 for a numeric split and node arrays are calloc'd, so every node any existing builder writes is a numeric node. Keeping the set inline rather than in a side table on the model leavesDecisionTreeClassifierandDecisionTreeRegressorat the field lists they already have, which the reference fits undertests/andensemble.flow's tree arrays depend on. sizeof(TreeNode) goes 32 to 52, so every literal byte count for a node array goes 36 to 64.Threading:
decision_tree_classifier_fit_categoricalanddecision_tree_regressor_fit_categoricaltake an X.cols mask; the existing fits call them with an all-zero mask, so there is one implementation and the old signatures are untouched.Algorithm: Breiman's, k-1 prefixes of the levels ordered by mean response. Exact for regression and two-class classification; a heuristic beyond two classes, and the code says so rather than implying optimality it does not have.
Numeric behaviour is bit-identical, verified node by node: 245 DecisionTree digits nodes, 64 feature importances, 360 RandomForest digits predictions and 113 regressor nodes, 795 lines, zero diff. DecisionTree digits 0.833333313 and RandomForest digits 0.955555558 unchanged.
Not covered, and worth a follow-up issue: RandomForest, the Bagging estimators and the ExtraTree builders. They go through their own builders, never set
cat_kind, andensemble.flowhas three sites that walk nodes inline rather than throughpredict_one. Each of those would need the subset test, and the RF builder does not presort, so its categorical scan is a separate piece of work.