하이퍼파라미터 튜닝

Taixi·2024년 10월 22일

생성형 AI 교육

목록 보기
20/35
post-thumbnail
  • 하이퍼파라미터 튜닝시 경우의 수가 너무 많음
  • 자동화할 필요가 있음
  • sklearn 패키지에서는 자주 사용되는 하이퍼파라미터 튜닝을 돕는 클래스가 존재
    • RandomizedSearchCV
    • GridSearchCV

RandomizedSearchCV

  • 모든 매개변수값이 시도되는 것이 아니라 지정된 분포에서 고정된 수의 매개변수 설정이 샘플링됨
  • 시도된 매개변수 설정의 수는 n_iter에 의해 제공됨
from sklearn.model_selection import RandomizedSearchCV
params = {
    'n_estimators': [200, 500, 1000, 2000],
    'learning_rate': [0.1, 0.05, 0.01],
    'max_depth': [6, 7, 8],
    'colsample_bytree': [0.8, 0.9, 1.0], # 샘플 사용 비율(max_features와 비슷한 개념)
    'subsample': [0.8, 0.9, 1.0]
}
# n_iter: 총 몇회의 시도를 진행할 것인지 설정(횟수가 늘어나면 좋은 parameter를 찾을 확률이 높지만 시간이 오래걸림)
clf = RandomizedSearchCV(LGBMRegressor(), params, random_state=10, n_iter=25, cv=3, scoring='neg_mean_squared_error')
clf.fit(x_train, y_train)
clf.best_score_
clf.best_params_
lgbm = LGBMRegressor(subsample=0.9, n_estimators=200, max_depth=6, learning_rate=0.1,colsample_bytree=0.8,random_state=10)
lgbm.fit(x_train, y_train)
lgbm_best_pred = lgbm.predict(x_test)
mse_eval('RandomSearch LGBM', lgbm_best_pred, y_test)

0 Elasticnet(l1_ratio=0.8) 23.156236
1 Standard EleasticNet 22.694970
2 Ridge(alpha=1) 22.480476
3 Lasso(alpha=0.01) 22.210557
4 LinearRegression 22.098695
5 Voting Ensemble 20.474757
6 Poly EleasticNet 15.710492
7 LGBM 10.937726
8 RandomSearch LGBM 10.686249
9 RandomForest Ensemble Tuning 10.517588
10 XGBoost 10.118980
11 RandomForest Ensemble 9.887423
12 Weighted Blending 9.119023
13 Stacking Ensenble 8.819256
14 GrandientBoost Ensemble 8.812709

GridSearchCV

  • 모든 매개변수 값에 대하여 완전 탐색을 시도
  • 최적화할 파라미터가 많다면 시간이 매우 오래 걸림
params = {
    'n_estimators': [500, 2000],
    'learning_rate': [0.1, 0.05, 0.01],
    'max_depth': [7, 8],
    'colsample_bytree': [0.8, 0.9],
    'subsample': [0.8, 0.9]
}

from sklearn.model_selection import GridSearchCV
grid_search = GridSearchCV(LGBMRegressor(), params, cv=3, n_jobs=-1, scoring='neg_mean_squared_error')
grid_search.fit(x_train, y_train)
grid_search.best_score_
grid_search.best_params_
lgbm = LGBMRegressor(subsample=0.8, n_estimators=2000, max_depth=7, learning_rate=0.01,colsample_bytree=0.8,random_state=10)
lgbm.fit(x_train, y_train)
lgbm_best_pred = lgbm.predict(x_test)
mse_eval('GridSearch LGBM', lgbm_best_pred, y_test)

0 Elasticnet(l1_ratio=0.8) 23.156236
1 Standard EleasticNet 22.694970
2 Ridge(alpha=1) 22.480476
3 Lasso(alpha=0.01) 22.210557
4 LinearRegression 22.098695
5 Voting Ensemble 20.474757
6 Poly EleasticNet 15.710492
7 LGBM 10.937726
8 RandomSearch LGBM 10.686249
9 RandomForest Ensemble Tuning 10.517588
10 GridSearch LGBM 10.297233
11 XGBoost 10.118980
12 RandomForest Ensemble 9.887423
13 Weighted Blending 9.119023
14 Stacking Ensenble 8.819256
15 GrandientBoost Ensemble 8.812709

profile
개발자를 위한 첫시작

0개의 댓글