【发布时间】:2022-11-04 00:11:55
【问题描述】:
问题:
- 第一个问题可能非常愚蠢,但我还是会问:在下面的示例中,修剪和提前停止是否相同?还是两个独立的选项控制两个独立的进程?
- 我的目标不平衡,那么如何在此处使用自定义评估指标而不是“binary_logloss”,例如平衡精度?
- 当我得到最佳参数时,'n_estimators' 仍将等于 999999。对于不平衡的目标,建议使用“无限”数量的估计器并使用提前停止进行修剪,这就是它如此之高的原因。修剪后如何用最优的 n_estimators 拟合最终模型?
非常感谢您帮助我解决这个问题,我很沮丧。
def objective(trial, X, y): param_grid = { # "device_type": trial.suggest_categorical("device_type", ['gpu']), "n_estimators": trial.suggest_categorical("n_estimators", [999999]), "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3), "num_leaves": trial.suggest_int("num_leaves", 20, 3000, step=20), "max_depth": trial.suggest_int("max_depth", 3, 12), "min_data_in_leaf": trial.suggest_int("min_data_in_leaf", 200, 10000, step=100), "lambda_l1": trial.suggest_int("lambda_l1", 0, 100, step=5), "lambda_l2": trial.suggest_int("lambda_l2", 0, 100, step=5), "min_gain_to_split": trial.suggest_float("min_gain_to_split", 0, 15), "bagging_fraction": trial.suggest_float( "bagging_fraction", 0.2, 0.95, step=0.1 ), "bagging_freq": trial.suggest_categorical("bagging_freq", [1]), "feature_fraction": trial.suggest_float( "feature_fraction", 0.2, 0.95, step=0.1 ), } cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=1121218) cv_scores = np.empty(5) for idx, (train_idx, test_idx) in enumerate(cv.split(X, y)): X_train, X_test = X.iloc[train_idx], X.iloc[test_idx] y_train, y_test = y.iloc[train_idx], y.iloc[test_idx] model = LGBMClassifier( objective="binary", **param_grid, n_jobs=-1, scale_pos_weight=len(y_train) / y_train.sum() ) model.fit( X_train, y_train, eval_set=[(X_test, y_test)], eval_metric="binary_logloss", # replace this with e.g. balanced accuracy or f1 callbacks=[ LightGBMPruningCallback(trial, "binary_logloss"), # replace this with e.g. balanced accuracy or f1 early_stopping(100, verbose=False) ], ) preds = model.predict(X_test)#.argmax(axis=1) cv_scores[idx] = balanced_accuracy_score(y_test, preds) loss = 1 - np.nanmedian(cv_scores) return loss跑:
study = optuna.create_study(direction="minimize", study_name="LGBM Classifier") func = lambda trial: objective(trial, X_train, y_train) study.optimize(func, n_trials=1)适合最后的问题。但是在这里我不想拟合 n_estimators=999999,而是使用最佳数量的 n_estimators:
model = LGBMClassifier( objective="binary", **study.best_params, n_jobs=-1, scale_pos_weight=len(y) / y.sum() )
【问题讨论】: