Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of using the XGBoost CV with custom function (Python) #10838

Open
YojanaGadiya opened this issue Sep 23, 2024 · 0 comments
Open

Example of using the XGBoost CV with custom function (Python) #10838

YojanaGadiya opened this issue Sep 23, 2024 · 0 comments

Comments

@YojanaGadiya
Copy link

YojanaGadiya commented Sep 23, 2024

Hello XGBoost team,

I am trying to use the XGBoost CV module with Optuna for parameter optimization with the following code:

def kappa_scorer(y_true, y_pred):
    """Kappa scorer for the XGBoost model."""
    return "kappa", cohen_kappa_score(y_true, y_pred)


def objective_xgboost(trial, study_name, X_train, y_train, label_to_idx, exp_type: str):
    """Objective function for the XGBoost classifier with final eval metric as Kappa."""
    params = {
        "verbosity": 0,
        "objective": "multi:softmax",
        "num_class": len(label_to_idx),
        "feval": kappa_scorer,
        "n_estimators": 1000,
        "eta": trial.suggest_float("learning_rate", 1e-2, 0.1, log=True),
        "max_depth": trial.suggest_int("max_depth", 2, 10),
        "colsample_bytree": trial.suggest_float(
            "colsample_bytree", 0.1, 0.7
        ),  # Percentage of features used per tree.
        "disable_default_eval_metric": 1,
    }

    # Training data
    y_train = y_train.map(label_to_idx)

    dtrain = xgb.DMatrix(X_train, label=y_train)

    # Optimization of kappa scoe
    pruning_callback = optuna.integration.XGBoostPruningCallback(trial, "test-kappa")
    xgboost_model = xgb.cv(
        params, dtrain, callbacks=[pruning_callback], seed=SEED, nfold=5
    )

    # Save model
    trial_path = _generate_dirs(exp_type)
    save_xgboost_trial(
        trial=trial, model=xgboost_model, study_name=study_name, trial_dir=trial_path
    )

    mean_kappa = xgboost_model["test-kappa-mean"].values[-1]  # Optimized for kappa

    return mean_kappa

However, I am not able to extract the metric results from the trained model and stable upon an error: KeyError: 'test-kappa'. Is there any pointer to what I am doing wrong?

Thank you.

@YojanaGadiya YojanaGadiya changed the title Example of using the XGBoost CV with custom function Example of using the XGBoost CV with custom function (Python) Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant