CatBoost
Categorical Boosting
Yandex의 그래디언트 부스팅. 범주형 특성 자동 처리.
Categorical Boosting
Yandex의 그래디언트 부스팅. 범주형 특성 자동 처리.
CatBoost(Categorical Boosting)는 러시아 검색 기업 Yandex가 2017년에 오픈소스로 공개한 그래디언트 부스팅 라이브러리입니다. XGBoost, LightGBM과 함께 3대 GBDT(Gradient Boosted Decision Trees) 라이브러리로, 특히 범주형 특성(Categorical Features) 처리에 특화되어 있습니다.
CatBoost의 가장 큰 차별점은 범주형 특성을 자동으로 처리한다는 점입니다. 기존 GBDT 라이브러리들은 범주형 특성을 원-핫 인코딩이나 라벨 인코딩으로 전처리해야 했지만, CatBoost는 Target Statistics라는 기법으로 범주형 특성을 수치화하면서도 Target Leakage를 방지합니다.
또한 CatBoost는 Ordered Boosting이라는 독자적인 알고리즘을 사용합니다. 기존 GBDT는 동일한 데이터로 잔차를 계산하고 학습하여 과적합 위험이 있었지만, CatBoost는 데이터 순서를 섞어 학습과 예측에 사용하는 데이터를 분리함으로써 더 일반화된 모델을 만듭니다.
실무에서 CatBoost는 테이블 데이터 대회(Kaggle)에서 자주 우승하며, 특히 전처리 시간을 줄이면서도 높은 성능을 원할 때 선택됩니다. 금융 리스크 모델링, 추천 시스템, CTR 예측 등 범주형 특성이 많은 도메인에서 강점을 보입니다.
# CatBoost를 사용한 분류 모델
from catboost import CatBoostClassifier, Pool
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, roc_auc_score
import pandas as pd
# 샘플 데이터 (범주형 특성 포함)
data = pd.DataFrame({
'age': [25, 30, 35, 40, 45, 50, 55, 60] * 100,
'gender': ['M', 'F', 'M', 'F', 'M', 'F', 'M', 'F'] * 100,
'city': ['Seoul', 'Busan', 'Seoul', 'Daegu', 'Busan', 'Seoul', 'Daegu', 'Busan'] * 100,
'income': [3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000] * 100,
'target': [0, 1, 0, 1, 1, 0, 1, 0] * 100
})
# 범주형 특성 지정
cat_features = ['gender', 'city']
# 학습/테스트 분리
X = data.drop('target', axis=1)
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# CatBoost Pool 생성 (범주형 특성 자동 처리)
train_pool = Pool(X_train, y_train, cat_features=cat_features)
test_pool = Pool(X_test, cat_features=cat_features)
# 모델 학습
model = CatBoostClassifier(
iterations=500,
learning_rate=0.05,
depth=6,
l2_leaf_reg=3,
verbose=100
)
model.fit(train_pool)
# 예측 및 평가
predictions = model.predict(test_pool)
probabilities = model.predict_proba(test_pool)[:, 1]
print(f"Accuracy: {accuracy_score(y_test, predictions):.4f}")
print(f"AUC-ROC: {roc_auc_score(y_test, probabilities):.4f}")
# 특성 중요도 확인
feature_importance = model.get_feature_importance()
for name, importance in zip(X.columns, feature_importance):
print(f"{name}: {importance:.2f}")
"대출 심사 모델에서 직업군, 거주지역 같은 범주형 특성이 30개가 넘는데, 전처리하는 것보다 CatBoost를 쓰면 cat_features만 지정해주면 됩니다. 라벨 인코딩보다 Target Encoding 효과를 자동으로 얻으면서 leakage도 방지됩니다."
"CatBoost의 Ordered Boosting은 학습 데이터를 무작위로 섞어서 현재 샘플을 예측할 때 이전 샘플들만 사용해 잔차를 계산합니다. 이로 인해 prediction shift가 줄어들고 과적합이 완화됩니다."
"CatBoost에서 early_stopping_rounds를 설정하면 validation loss가 개선되지 않을 때 학습이 자동 중단됩니다. iterations를 크게 잡고 early stopping을 쓰는 게 최적 반복 횟수를 찾는 좋은 방법이에요."
cat_features를 지정하지 않으면 CatBoost가 범주형 특성을 수치로 잘못 해석합니다. 문자열 타입은 자동 감지되지만, 숫자로 인코딩된 범주형(예: 지역코드)은 반드시 명시해야 합니다.
Ordered Boosting으로 인해 XGBoost나 LightGBM보다 학습 시간이 길 수 있습니다. 대용량 데이터에서는 task_type='GPU'를 설정하거나 subsample 파라미터를 조정하세요.
범주형 특성의 고유 값이 너무 많으면(예: 사용자 ID) Target Statistics 계산이 불안정해집니다. 이 경우 hashing이나 embedding 기법을 먼저 고려하세요.