본문 바로가기
머신러닝

[XGB][파라미터 최적화] Bayesian Optimization

by 방구석 데이터사이언티스트 2022. 2. 16.
728x90
반응형

1. Bayesian Optimization 이란?

출처: [머신러닝/딥러닝] 11-4. Bayesian Optimization (sonsnotation.blogspot.com)

미지의 함수가 최대/최소값을 갖게 하는 지점 X를  찾는 일종의 optimize 알고리즘으로 베이지안 룰에 의해 사전지식을 반영하면서 하이퍼파라미터를 찾는다. 

 

즉 베이지안 최적화는 현재까지 얻은 모델과 추가적인 실험정보를 통해 데이터가 주어졌을 때의 모델을 추정해 나가는 방식을 가진다. 

 

실질적으로 베이지안 최적화는 그리드 서치나 랜덤 서치 그리고 autoML 과 비교하였을 때 비교적 빠르면서 더 높은 성능을 이끌어낼 수 있도록 파라미터가 최적화되는 사례가 많다. 

 

2. Bayesian Optimization을 통한 XGBClassifier 최적화 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# xgb 분류문제 베이지안 최적화
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from sklearn import datasets
from sklearn.model_selection import cross_val_score
from bayes_opt import BayesianOptimization
import xgboost as xgb
 
dtrain1 = xgb.DMatrix(data=X_resampled1, label=y_resampled1)
dtest1 = xgb.DMatrix(data=X_test1, label=y_test1)
 
 
def xgb_evaluate(max_depth, gamma, colsample_bytree,min_child_weight,eta,subsample):
    params = {'eval_metric''auc',
              'objective':'binary:logistic',
              'min_child_weight': min_child_weight,
              'max_depth'int(max_depth),
              'subsample': subsample,
              'eta': eta,
              'gamma': gamma,
              'colsample_bytree': colsample_bytree}
    # Used around 1000 boosting rounds in the full model
    cv_result = xgb.cv(params, dtrain1, num_boost_round=100, nfold=3, metrics = 'auc')    
    
    return cv_result['test-auc-mean'].iloc[-1]
 
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth': (37), 
                                             'gamma': (01),
                                             'colsample_bytree': (0.30.9),
                                            'min_child_weight': (59),
                                            'eta':(0.10.3),
                                            'subsample':(0.7,1.0)},random_state = 0 )
 
xgb_bo.maximize(init_points=3, n_iter=5, acq='ei')
cs

 

2번부터 8번까지는 필요한 모듈을 불러오는 코드입니다. 특히 7번과 8번은 베이지안 최적화를 하기위해서는 필수적으로 필요한 모듈입니다. 만약 BayesianOptimization과 xgboost가 없다면 설치가 필요합니다!!

14번부터 26번까지는 xgboost를 교차검증하여 평균 auc를 도출하는 코드입니다. 이 함수를 베이지안 옵티마이제이션에 적용하고 파라미터들의 범위를 설정해주면 auc를 최대로 하는 XGB파라미터를 알 수 있습니다. 

 

※ 설치 링크

bayesian-optimization · PyPI

 

bayesian-optimization

Bayesian Optimization package

pypi.org

xgboost · PyPI

 

xgboost

XGBoost Python Package

pypi.org

 

3. Bayesian Optimization을 통한 XGBRegressor 최적화 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import xgboost as xgb
from xgboost import plot_importance
from bayes_opt import BayesianOptimization
import warnings
warnings.filterwarnings('ignore')
 
dtrain = xgb.DMatrix(data=X_train, label=y_train)
dtest = xgb.DMatrix(data=X_test, label=y_test)
 
 
def xgb_evaluate(max_depth, gamma, colsample_bytree,min_child_weight,eta,subsample):
    params = {'eval_metric''rmse',
              'objective':'reg:squarederror',
              'min_child_weight': min_child_weight,
              'max_depth'int(max_depth),
              'subsample': subsample,
              'eta': eta,
              'gamma': gamma,
              'colsample_bytree': colsample_bytree}
 
    cv_result = xgb.cv(params, dtrain, num_boost_round=100, nfold=3)    
    
 
    return -1.0 * cv_result['test-rmse-mean'].iloc[-1]
 
xgb_bo = BayesianOptimization(xgb_evaluate, {'max_depth': (37), 
                                             'gamma': (01),
                                             'colsample_bytree': (0.30.9),
                                            'min_child_weight': (59),
                                            'eta':(0.10.3),
                                            'subsample':(0.71.0)}, random_state=0)
 
xgb_bo.maximize(init_points=3, n_iter=5, acq='ei')
cs

 

회귀문제를 위한 베이지안 최적화 코드도 크게 다르지 않습니다. 

11번부터 24번까지는 xgboost를 교차검증하여 평균 rmse를 도출하는 코드입니다.

여기서 중요한 것은 24번을 보시면 -1.0을 곱해주는 것을 알 수 있습니다. 이는 마지막 줄에서 maximize로 출력을 하기 때문이죠. 즉 rmse는 작을 수록 좋기 때문에 음수로 바꿔주면서 -rmse를 최대로 하는 XGB파라미터를 알 수 있습니다.

 

 

 

728x90
반응형

댓글