0

I'm attempting to tune the hyperparameters of my lightGBM model but I keep getting the same error:

RuntimeError: A single direction cannot be retrieved from a multi-objective study. Consider using Study.directions to retrieve a list containing all directions.

Which is really confusing because I'm following the advice explained in this answer which means I'm passing a list of directions to the study. Any and all help will be greatly appreciated.

def objective(trial, X, y, group):
    param = {
        "objective": "binary",
        "metric": "auc",
        "verbosity": -1,
        "boosting_type": "gbdt",
        "lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
        "lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 2, 256),
        "feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
        "bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
        "bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
        "min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
    }

    cv = StratifiedGroupKFold(n_splits=5, shuffle=True, random_state=RANDOM_STATE)

    cv_scores = np.empty(5)
    auc_scores = np.empty(5)
    for idx, (train_idx, test_idx) in enumerate(cv.split(X, y,groups=group)):
        X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
        y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]

        pruning_callback = optuna.integration.LightGBMPruningCallback(trial, "auc")
        
        model = lgb.LGBMClassifier(**param)
        
        model.fit(
            X_train,
            y_train,
            eval_set=[(X_test, y_test)],
            early_stopping_rounds=100,
            callbacks=[pruning_callback])
        
        preds = model.predict_proba(X_test)
        cv_scores[idx] = log_loss(y_test, preds)
        auc_scores[idx] = roc_auc_score(y_test, preds)
        
    return np.mean(cv_scores), np.mean(auc_scores)

study = optuna.create_study(directions=["minimize", "maximize"], study_name="LGBM Classifier")
func = lambda trial: objective(trial, X, y, group)
study.optimize(func, n_trials=2)

Evan
  • 31
  • 8
  • Unfortunately, pruning feature, `LightGBMPruningCallback` in the code, cannot be used with a multi-objective function. – nzw0301 Feb 27 '23 at 13:07

0 Answers0