Your first CatBoost model

CatBoost · Essentials · 10 min

CatBoost often matches or beats tuned alternatives on tabular data with default settings, so it's a strong first model.

CatBoost implements gradient boosting: it builds hundreds or thousands of small decision trees, each one fitted to correct the errors of the ensemble so far. The Python package (pip install catboost) follows scikit-learn conventions:

  • CatBoostClassifier for binary and multiclass problems, CatBoostRegressor for numbers, CatBoostRanker for ranking
  • model.fit(X, y), model.predict(X), and for classifiers model.predict_proba(X)
  • X can be a pandas DataFrame, a NumPy array or a CatBoost Pool

Defaults are good: 1000 trees (iterations), depth 6, and a learning rate picked automatically from the data size and iteration count. Many datasets need little more.

Two practical settings you'll set constantly:

  • verbose: by default CatBoost prints a line per iteration. Use verbose=0 to silence it, or verbose=100 to print every 100 iterations.
  • random_seed: training has randomness; fix it for reproducible results.

CatBoost also writes a catboost_info/ folder of training logs by default; set train_dir= to choose the location or allow_writing_files=False to disable it.

Models also work inside scikit-learn tooling such as cross_val_score and Pipeline, though CatBoost's native features (categorical handling, its own cross-validation, early stopping) are usually used directly.

Example

from catboost import CatBoostClassifier, CatBoostRegressor
from sklearn.datasets import load_breast_cancer, load_diabetes
from sklearn.model_selection import train_test_split

X, y = load_breast_cancer(return_X_y=True, as_frame=True)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=0)

clf = CatBoostClassifier(iterations=300, verbose=0, random_seed=0,
                         allow_writing_files=False)
clf.fit(X_tr, y_tr)
print(clf.predict(X_te[:3]))                 # e.g. [0 1 1] class labels
print(clf.predict_proba(X_te[:1]).round(3))  # [[p_class0 p_class1]]
print(round(clf.score(X_te, y_te), 3))       # accuracy, about 0.98

Xr, yr = load_diabetes(return_X_y=True)
reg = CatBoostRegressor(iterations=300, verbose=0, random_seed=0,
                        allow_writing_files=False)
reg.fit(Xr[:350], yr[:350])
print(reg.predict(Xr[350:353]).round(1))     # three predicted values
print(reg.get_params()["iterations"], reg.tree_count_)   # 300 300

Remember

  • CatBoostClassifier/Regressor follow the fit/predict/predict_proba API
  • Defaults (1000 trees, depth 6, automatic learning rate) are strong
  • Use verbose=0 and random_seed for quiet, reproducible runs
  • allow_writing_files=False stops the catboost_info log folder

Watch out for

  • Leaving the default verbose output on in loops or notebooks, flooding the output with thousands of lines.

Check yourself

  1. What does each new tree in gradient boosting try to do?
  2. Which call returns class probabilities for a CatBoostClassifier?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: catboost.ai/docs/en/concepts/python-reference_catboostclassifier