Перейти к содержанию

Ошибки

Модуль catboost_utils.errors превращает длинные ошибки CatBoost в короткое описание и подсказку, что с этим делать.

from catboost_utils import wrap, CBXError

m = wrap(CatBoostClassifier(iterations=10))
try:
    m.fit(X, y)   # в X есть строковая колонка, но она не указана в cat_features
except CBXError as e:
    print(e.human_message)
    print(e.hint)
    print(e.feature_name, e.feature_idx)

Имя колонки в ошибке определяется в таком порядке (на каждом вызове fit и predict):

  1. X.columns, если передан pandas DataFrame
  2. X.get_feature_names(), если передан catboost.Pool
  3. явный аргумент feature_names=
  4. model.feature_names_ после обучения
  5. если ничего нет — показывается номер колонки

catboost_utils.errors.exceptions.CBXError

CBXError(
    *,
    original_error: BaseException,
    human_message: str,
    hint: str,
    feature_name: str | None = None,
    feature_idx: int | None = None,
)

Bases: Exception

A CatBoost error translated into a human-readable form.

Attributes:

Name Type Description
original_error BaseException

the underlying CatBoostError (or other exception)

human_message str

short description in plain English

hint str

actionable advice for the user

feature_name str | None

resolved feature name when feature_idx was extractable

feature_idx int | None

raw feature index from the original error

catboost_utils.errors.wrapper.wrap

wrap(model: T, *, validate: bool = False) -> T

Enhance an existing CatBoost model with readable errors and feature-name resolution.

Mutates model in place by swapping __class__; returns the same object for chaining. isinstance(model, CatBoostClassifier) continues to work.

Parameters:

Name Type Description Default
model T

a CatBoostClassifier or CatBoostRegressor instance.

required
validate bool

reserved for future use; pre-flight validation hook.

False

Returns:

Type Description
T

The same model instance with enhanced error reporting.

catboost_utils.errors.wrapper.unwrap

unwrap(model: Any) -> Any

Reverse wrap(): restore the original CatBoost class on the instance.