모듈 설치
!pip install hyperopt
모듈 가져오기
import hyperopt
from hyperopt import hp
from hyperopt import STATUS_OK
from hyperopt import fmin, tpe, Trials
from sklearn.metrics import accuracy_score as accuracy
검색 공간 설정
search_space = {
'max_depth': hp.quniform('max_depth', 3, 15, 1),
'min_child_weight': hp.quniform('min_child_weight', 1, 3, 1),
'learning_rate': hp.uniform('learning_rate', 0.005, 0.2),
'colsample_bytree': hp.uniform('colsample_bytree', 0.5, 1),
}
목적 함수 정의
def objective_func(xgbc_search_space):
# fmin은 실수값을 반환해서 정수형 변환
model = XGBC(n_estimators = 100,
max_depth = int(search_space['max_depth']),
min_child_weight = int(search_space['min_child_weight']),
learning_rate = search_space['learning_rate'],
colsample_bytree = search_space['colsample_bytree'],
eval_metric = 'logloss'
)
cv_score = cross_val_score(
model,
x_train, y_train,
scoring=make_scorer(accuracy, greater_is_better=True),
cv=3,
n_jobs=-1,
verbose=0
)
# fmin은 최소를 찾는 함수이고, accuracy는 높을수록 좋은 점수라서 -1 곱하기
return {'loss':-1 * np.mean(cv_score), 'status': STATUS_OK}
베이지안 최적화 실행
trial = Trials()
best = fmin(fn=objective_func, # 목적함수
space=search_space, # 검색공간
algo=tpe.suggest, # Tree structured parsen estimator (항상 넣을 것)
max_evals=5, # 최대 수행횟수
trials=trial, # 입력한 결과값 저장할 trials 객체 (문서 리스트들)
rstate=np.random.default_rng(seed=0) # 랜덤시드값 안주는 것이 좋은 성능으로 나옴
)
print('best score : ', best)
결과 확인
print(trial.results, trial.vals, trial.best_trial)
print(f'colsample_bytree : {round(best["colsample_bytree"], 6)}')
print(f'max_depth : {int(best["max_depth"])}')
print(f'min_child_weight : {int(best["min_child_weight"])}')
최적의 하이퍼파라미터로 재학습
# fmin으로 도출한 파라미터를 그대로 다시 학습
model = XGBC(
n_estimators=200,
max_depth=int(best['max_depth']),
min_child_weight=int(best['min_child_weight']),
learning_rate=round(best['learning_rate'], 6),
colsample_bytree=round(best['colsample_bytree'], 6)
)
model.fit(x_train, y_train, early_stopping_rounds=100,
eval_metric="auc", eval_set=[(x_train, y_train), (x_val, y_val)])
y_pred = model.predict_proba(x_test)[:,1]
roc_score = roc_auc_score(y_test, y_pred)
print(f'ROC AUC: {roc_score:.4f}')
참고
Hyperopt Documentation
From here you can search these documents. Enter your search terms below.
hyperopt.github.io
베이지안 최적화에 기반한 HyperOpt를 활용한 하이퍼 파라미터 튜닝
베이지안 최적화에 기반한 HyperOpt를 활용한 하이퍼 파라미터 튜닝 방법에 대하여 알아 보도록 하겠습니다.
teddylee777.github.io
'AI > - Library' 카테고리의 다른 글
[CatBoost] 알아보기 (0) | 2023.03.09 |
---|---|
[Scikit-learn] pipeline 이해하기 (0) | 2023.03.09 |
[scikit-learn] KFold 와 StratifiedKFold 의 차이 (0) | 2023.03.09 |
[Pandas] index 활용 정리 (0) | 2023.03.06 |
[lightGBM] 파라미터, 메소드, 어트리뷰트 파헤치기 (0) | 2023.03.03 |
[PyTorch] 기본 알아둘 것 정리 (0) | 2023.03.03 |
댓글