CatBoost categorical processing
Quick answer
CatBoost can accept categorical features directly and construct useful numerical statistics from category occurrences. Some category statistics use target information from earlier examples in a permutation, reducing direct self-label leakage. Small-cardinality features may instead be one-hot encoded. Native categorical processing does not remove the need for honest dataset splits. Static examples tested locally with Python 3.13.5, XGBoost 3.1.3, LightGBM 4.6.0 and CatBoost 1.2.8 where imported. ByteMind package execution is unverified.
Compute a prefix target statistic
from collections import defaultdictcount=defaultdict(int)positive=defaultdict(int)out=[]for c,y in zip(["a","a","b","a"],[1,0,1,1]): out.append(round((positive[c]+1)/(count[c]+2),3)) count[c]+=1 positive[c]+=yprint(out)Observed local output:
[0.5, 0.667, 0.5, 0.5]The current label affects only later rows with the same category.
How it works
Consider a simplified binary target statistic (past positives + prior)/(past count + prior strength). Process one fixed order of training rows, compute the statistic before seeing the current target, then update the counts. With prior numerator 1 and prior strength 2, the first row for a category receives 0.5. If that row is positive, the next row receives 2/3. This is a pedagogical illustration, not an implementation of CatBoost’s full CTR algorithms: the real library can quantize targets, combine features and use multiple permutations and statistics. Ordered categorical statistics and Ordered boosting are distinct techniques; setting boosting_type="Ordered" explicitly requests that training mode, rather than assuming it is the default for all data. The usual symmetric tree policy applies the same split at each level, unlike unrestricted leaf-wise growth. CatBoost accepts named categorical columns with string or integer values; explicitly replace missing categorical labels by a reserved string, keeping numeric NaNs separate. Keep the validation labels out of the training features and pass an eval_set only for evaluation/selection. XGBoost 3.1 supports category-aware dataframe inputs with enable_categorical=True; LightGBM supports categorical splits using consistent integer codes or compatible pandas categories. These paths differ; one shared one-hot baseline is useful for comparisons but does not benchmark every native capability.
Fit native categorical classification
import pandas as pdfrom catboost import CatBoostClassifierX=pd.DataFrame({"kind":["blue","red"]*12})y=[0,1]*12m=CatBoostClassifier(iterations=20,depth=2,learning_rate=0.3,cat_features=["kind"],one_hot_max_size=2,boosting_type="Ordered",task_type="CPU",thread_count=1,random_seed=7,verbose=False,allow_writing_files=False).fit(X,y)print(m.predict(pd.DataFrame({"kind":["blue","red"]})).astype(int).tolist())print(m.get_cat_feature_indices())Observed local output:
[0, 1][0]The model treats the first column as categorical. This two-category example deliberately uses the small-category one-hot lane, not evidence that target statistics were used.
Common mistakes
Never compute target means using all labels before splitting. Do not call the simplified prior statistic the exact CatBoost implementation. Do not pass floating NaN as a categorical string value or assume unknown category behavior transfers to another library.
Practice and next steps
Trace the statistic for [a,a,b,a] and labels [1,0,1,1]. Explain why changing the third label cannot alter the first three statistics. Try an unseen string in the native model; the prediction is not a guarantee of reliability for novel categories. Complete the knowledge check and staged exercise. Next: Missing values and monotonicity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CatBoost categorical processing — knowledge check
Apply catboost categorical processing to new scenarios.
When may a row label enter the simplified ordered statistic?
Is the toy formula the complete CatBoost CTR algorithm?
Are Ordered boosting and ordered categorical statistics identical?
How should a missing categorical string be represented in this fixture?
What does a successful prediction for an unseen category prove?