Sub topic 3
CatBoost: Symmetric Trees
Assume Python, pandas, train/validation splits, decision trees, and AUC. CatBoost is a gradient-boosting library designed to work well with categorical columns.
Symmetric trees
CatBoost usually builds symmetric, or oblivious, trees. At one depth level, every leaf uses the same split condition. For example, the whole level might test age > 55. This structure makes prediction fast and acts as a useful regularizer, but it can be less flexible than LightGBM’s leaf-wise trees.
Training with `Pool` and `cat_features`
A Pool stores features, labels, and metadata. Declare categorical columns explicitly. CatBoost then applies its leakage-resistant categorical encoding internally.
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(0)
n = 5000
X = pd.DataFrame({
"age": rng.integers(18, 70, n),
"city": rng.choice(["A", "B", "C", "D"], n),
"plan": rng.choice(["free", "basic", "pro"], n),
"income": rng.normal(50_000, 12_000, n),
})
X.loc[rng.random(n) < 0.05, "income"] = np.nan
y = (((X["plan"] == "pro") | (X["age"] > 55))
^ (rng.random(n) < 0.1)).astype(int)
cat_features = ["city", "plan"]
X[cat_features] = X[cat_features].fillna("__MISSING__")
X_train, X_valid, y_train, y_valid = train_test_split(
X, y, test_size=0.2, random_state=0
)
train_pool = Pool(X_train, y_train, cat_features=cat_features)
valid_pool = Pool(X_valid, y_valid, cat_features=cat_features)
model = CatBoostClassifier(
iterations=1000, learning_rate=0.1, depth=6,
eval_metric="AUC", verbose=False
)
model.fit(train_pool, eval_set=valid_pool, early_stopping_rounds=50)
print(model.get_best_iteration())Tuning and interpretation
Tune a small set of parameters first:
| Parameter | Main effect |
|---|---|
| `depth` | Tree complexity and interaction strength |
| `learning_rate` | Step size; lower values usually need more iterations |
| `l2_leaf_reg` | Regularization strength |
| `iterations` | Maximum number of trees; use early stopping |
Use a validation split or cross-validation, and tune only on the training portion. Random search is often more efficient than a large grid.
CatBoost provides split-based feature importance, but importance is not causation. SHAP values explain each prediction as feature contributions:
importance = model.get_feature_importance(valid_pool)
shap_values = model.get_feature_importance(valid_pool, type="ShapValues")
mean_abs_shap = np.abs(shap_values[:, :-1]).mean(axis=0)
print(dict(zip(X.columns, mean_abs_shap)))Choosing CatBoost or LightGBM
Choose CatBoost when the dataset has many categorical columns, categories are messy, or preprocessing and tuning time are limited. Choose LightGBM when the data is mostly numeric, the dataset is very large, or maximum training speed and fine-grained tree control matter. Benchmark both when the choice affects production quality.
Exercise
Why should a categorical column with missing values be filled before creating a CatBoost Pool?
Show answer
CatBoost needs categorical values in a supported non-missing representation, so a sentinel string preserves the missing category explicitly.