
RandomizedSearchCV
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
