Gradient Boosting
Gradient Boosting
An ensemble technique that sequentially combines weak learners. XGBoost, LightGBM.
Gradient Boosting
An ensemble technique that sequentially combines weak learners. XGBoost, LightGBM.
Gradient Boosting is an ensemble technique that sequentially combines multiple weak learners (usually decision trees) to build a powerful predictive model. At each step, it adds a new model that learns the previous model's residual, that is, its error. In this process, it uses gradient descent to improve the model in the direction that minimizes the loss function.
The core idea of boosting is "focus on what the previous model got wrong." The second tree learns the error predicted by the first tree, the third tree learns that error in turn, and so on. The final prediction is computed as the sum of all the trees' predictions. Unlike Bagging, which trains in parallel like a Random Forest, this approach trains sequentially, so it can compensate for the weaknesses of earlier models.
The most widely used implementations in practice are XGBoost, LightGBM, and CatBoost. XGBoost has won many Kaggle competitions thanks to its regularization and parallel processing optimizations. LightGBM is faster on large datasets thanks to its leaf-wise tree growth. CatBoost handles categorical variables without separate preprocessing and reduces overfitting with ordered target encoding.
Gradient boosting often still outperforms deep learning on tabular data. Because it is sensitive to feature engineering and hyperparameter tuning, you must carefully adjust the learning rate (learning_rate), number of trees (n_estimators), tree depth (max_depth), and regularization parameters (reg_alpha, reg_lambda). It is common to use early stopping together to prevent overfitting.
Here is an example comparing the three implementations: XGBoost, LightGBM, and CatBoost.
# pip install xgboost lightgbm catboost scikit-learn
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.metrics import accuracy_score, classification_report
import xgboost as xgb
import lightgbm as lgb
from catboost import CatBoostClassifier
import time
# 데이터 생성
X, y = make_classification(n_samples=10000, n_features=20, n_informative=15,
n_redundant=3, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 1. XGBoost
print("=== XGBoost ===")
start = time.time()
xgb_model = xgb.XGBClassifier(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
reg_alpha=0.1,
reg_lambda=1.0,
early_stopping_rounds=10,
eval_metric='logloss',
random_state=42
)
xgb_model.fit(X_train, y_train, eval_set=[(X_test, y_test)], verbose=False)
xgb_pred = xgb_model.predict(X_test)
print(f"정확도: {accuracy_score(y_test, xgb_pred):.4f}")
print(f"학습 시간: {time.time() - start:.2f}초")
# 2. LightGBM
print("\n=== LightGBM ===")
start = time.time()
lgb_model = lgb.LGBMClassifier(
n_estimators=100,
max_depth=6,
learning_rate=0.1,
subsample=0.8,
colsample_bytree=0.8,
reg_alpha=0.1,
reg_lambda=1.0,
random_state=42,
verbose=-1
)
lgb_model.fit(
X_train, y_train,
eval_set=[(X_test, y_test)],
callbacks=[lgb.early_stopping(stopping_rounds=10, verbose=False)]
)
lgb_pred = lgb_model.predict(X_test)
print(f"정확도: {accuracy_score(y_test, lgb_pred):.4f}")
print(f"학습 시간: {time.time() - start:.2f}초")
# 3. CatBoost
print("\n=== CatBoost ===")
start = time.time()
cat_model = CatBoostClassifier(
iterations=100,
depth=6,
learning_rate=0.1,
l2_leaf_reg=3.0,
random_seed=42,
verbose=False,
early_stopping_rounds=10
)
cat_model.fit(X_train, y_train, eval_set=(X_test, y_test))
cat_pred = cat_model.predict(X_test)
print(f"정확도: {accuracy_score(y_test, cat_pred):.4f}")
print(f"학습 시간: {time.time() - start:.2f}초")
# 피처 중요도 (XGBoost)
print("\n=== XGBoost 피처 중요도 Top 5 ===")
importance = xgb_model.feature_importances_
indices = np.argsort(importance)[::-1][:5]
for i, idx in enumerate(indices):
print(f"{i+1}. Feature {idx}: {importance[idx]:.4f}")
# 결과 예시:
# === XGBoost ===
# 정확도: 0.9285
# 학습 시간: 0.42초
#
# === LightGBM ===
# 정확도: 0.9310
# 학습 시간: 0.18초