본문 바로가기

04. 캐글

00016. [KAG-002] House Prices 예측 #5 - XGBoost & LightGBM 적용

반응형

이번 편에서는 고성능 머신러닝 모델인 XGBoostLightGBM을 활용해 House Prices 회귀 문제를 해결해보겠습니다.

이 두 모델은 Kaggle 대회에서 가장 널리 사용되는 Gradient Boosting 계열 알고리즘입니다.


⚙️ 1. 라이브러리 설치 및 불러오기

pip install xgboost lightgbm
import xgboost as xgb
import lightgbm as lgb
from sklearn.model_selection import cross_val_score

🧪 2. 평가 지표 (RMSLE)

from sklearn.metrics import mean_squared_log_error
import numpy as np

def rmsle_cv(model):
    score = -cross_val_score(model, X, y, scoring="neg_mean_squared_log_error", cv=5)
    return np.sqrt(score.mean())

✅ 3. XGBoost 적용

model_xgb = xgb.XGBRegressor(
    n_estimators=1000,
    learning_rate=0.05,
    max_depth=3,
    subsample=0.8,
    colsample_bytree=0.8,
    random_state=42
)

model_xgb.fit(X, y)
print("XGBoost RMSLE:", rmsle_cv(model_xgb))

✅ 4. LightGBM 적용

model_lgb = lgb.LGBMRegressor(
    objective='regression',
    num_leaves=31,
    learning_rate=0.05,
    n_estimators=1000
)

model_lgb.fit(X, y)
print("LightGBM RMSLE:", rmsle_cv(model_lgb))

📈 5. 결과 예시 (Cross Validation 기준)

모델명RMSLE (예시)

XGBoost 0.1362
LightGBM 0.1345

두 모델 모두 이전 베이스라인 모델(RF, Lasso 등)보다 성능 우수


📤 6. Kaggle 제출 파일 생성

preds = model_lgb.predict(X_test)
submission = pd.DataFrame({
    'Id': test['Id'],
    'SalePrice': np.expm1(preds)  # 로그 역변환
})
submission.to_csv('submission_lgb.csv', index=False)

✅ 요약

  • XGBoost와 LightGBM 모두 뛰어난 성능을 보임
  • Cross Validation 기반 RMSLE로 과적합 방지
  • 로그 변환과 역변환 처리에 주의 필요

➡️ 다음 편에서는 하이퍼파라미터 튜닝을 통해 이 모델들의 성능을 한층 더 끌어올려보겠습니다 🔧

반응형