[Python] optuna: Different Results Even With Same random_state

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to understand why running the below code for hypterparameter tuning using optuna gives me different best parameter values even if I am running the exact same code with the same random_state = 42. Where is the random part coming from?

    import optuna
    import sklearn
    from sklearn.ensemble import RandomForestClassifier

    from sklearn.model_selection import cross_val_score

    def objective(trial):
    digits = sklearn.datasets.load_digits()
    x, y = digits.data, digits.target
    max_depth = trial.suggest_int("rf_max_depth", 2, 64, log=True)
    max_samples = trial.suggest_float("rf_max_samples", 0.2, 1)

    rf_model = RandomForestClassifier(
    max_depth = max_depth,
    max_samples = max_samples,
    n_estimators = 50,
    random_state = 42
    )

    score = cross_val_score(rf_model, x, y, cv=3).mean()
    return score
    study = optuna.create_study(direction = "maximize")
    study.optimize(objective, n_trials = 3)
    trial = study.best_trial

    print("Best Score: ", trial.value)
    print("Best Params: ")
    for key, value in trial.params.items():
    print(" {}: {}".format(key, value))

    Continue reading...

Compartilhe esta Página