pythonintermediate

CatBoost Gradient Boosting Training

Train a CatBoost model with automatic categorical feature handling and built-in cross-validation.

python
from catboost import CatBoostClassifier, cv, Pool
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True, as_frame=True)
X['cat_feature'] = np.where(X['petal length (cm)'] > 3, 'long', 'short')

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = CatBoostClassifier(
    iterations=200, learning_rate=0.1, depth=6,
    cat_features=['cat_feature'], verbose=50, random_seed=42,
)
model.fit(X_train, y_train, eval_set=(X_test, y_test), early_stopping_rounds=20)

print('Test accuracy:', model.score(X_test, y_test))
print('Best iter:', model.best_iteration_)

Use Cases

  • categorical ML
  • gradient boosting
  • tabular classification

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.