Repository navigation
Description
Describe the bug
With the new native categorical support in trees (#33354, #33972, forests in #34832), the input values get ordinal-encoded internally before validation, so the raw category values can be anything (negative, non-integer, > 255). The real limit is on the number of distinct categories, and it's 256 (not 255) with splitter="best".
But the docstring and error message still describe the older "codes must already be in [0, 255]" design:
- the
categorical_featuresdocstring (DecisionTreeClassifier,DecisionTreeRegressorand the forests) says "at most 255 unique categories", but 256 work fine (all 256 predicted correctly, also with missing values present) - with 257+ categories the error says
Values for categorical features should be integers in [0, 255]. Found 256.even when my values are e.g.1000..1256. The256is the internal encoded max (n_cats - 1), not something from my data, so the message points at the wrong problem - same wording for
splitter="random":integers in [0, 16777215]
Steps/Code to Reproduce
import numpy as np
from sklearn.tree import DecisionTreeRegressor
# 1. values outside [0, 255], negative and non-integer values are all accepted
X = np.array([[-1.0], [0.5], [300.0], [2000.0]] * 10)
y = np.array([1.0, 2.0, 3.0, 4.0] * 10)
tree = DecisionTreeRegressor(categorical_features=[0]).fit(X, y)
print(tree.predict([[-1.0], [0.5], [300.0], [2000.0]]))
# 2. 256 unique categories are accepted (docstring says at most 255)
X = np.arange(256, dtype=float).reshape(-1, 1)
DecisionTreeRegressor(categorical_features=[0]).fit(X, np.arange(256.0))
print("256 categories: fitted")
# 3. 257 unique categories: the error talks about the value range
X = np.arange(1000, 1257, dtype=float).reshape(-1, 1)
DecisionTreeRegressor(categorical_features=[0]).fit(X, np.arange(257.0))Expected Results
- the docstring states the real limit (256 categories with
splitter="best") - the error describes the actual problem, e.g.
Categorical feature 0 has 257 categories, but at most 256 are supported with splitter="best".
Actual Results
[1. 2. 3. 4.]
256 categories: fitted
ValueError: Values for categorical features should be integers in [0, 255]. Found 256.
Additional context
I think this comes from the first version of #33354, where the codes had to be "contiguous integers in [0, 255]". The ordinal encoding was added later, but the message and docstring weren't updated. #35181 moves this check but keeps the same message, so cc @cakedev0. Happy to open a PR for it, either after #35181 or folded into it, whichever you prefer.
Versions
sklearn: 1.10.dev0 (nightly wheel, main @ 59106029d)
python: 3.12.4, Windows 11
numpy: 2.6.0.dev0, scipy: 2.0.0.dev0, pandas: 3.2.0.dev0
narwhals: 2.27.1, joblib: 1.6.0, threadpoolctl: 3.7.0
Disclosure: I used Claude Code to help with the investigation.
Activity
GauravAgrawal11 commentedon Oct 25, 2026
Hi, I'd like to contribute to this issue. I understand that the categorical feature limit and error message need to reflect the number of distinct categories rather than the raw encoded value range.
I noticed the issue mentions PR #35181 and the possibility of folding the fix into it. Would you prefer a separate PR after #35181, or should this change be incorporated there? I'm happy to work on the docstrings, error message, and regression tests once the intended approach is confirmed.
cakedev0 commentedon Oct 26, 2026
Thanks for the report @aaronanish-t, and thanks @GauravAgrawal11 for offering to help! #35181 already rewrites this check, so I folded the fix in there. The error now gives the feature index and its number of categories, and the docstrings say 256. So no separate PR is needed for this one.
Fix: correct categorical_features docstring and error message (scikit…
Test & Doc: update tree/forest tests and docstrings for 256 categoric…