[Python] What is max_evals in hyperopt fmin function?

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to run a multi-classification problem. I have run a baseline lightGBM model with around 80% accuracy rate. I'm trying to further fine-tuning its hyperparameter using Hyperopt. However, when configuring Fmin, I'm confused in how should I set the max_evals to be, and what exactly is max_evals?

    from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
    import lightgbm as lgb
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import accuracy_score
    from hyperopt.pyll.base import scope
    # Load randomized search best parameters
    with open('best_params.json', 'r') as file:
    best_params = json.load(file)

    print("Best hyperparameters loaded from best_params.json")
    # Define the search space around the best parameters
    search_space = {
    'num_leaves': scope.int(hp.quniform('num_leaves', best_params['num_leaves'] - 10, best_params['num_leaves'] + 10, 1)),
    'learning_rate': hp.uniform('learning_rate', best_params['learning_rate'] * 0.8, best_params['learning_rate'] * 1.2),
    'n_estimators': scope.int(hp.quniform('n_estimators', best_params['n_estimators'] - 100, best_params['n_estimators'] + 100, 10)),
    'min_child_weight': hp.uniform('min_child_weight', best_params['min_child_weight'] * 0.8, best_params['min_child_weight'] * 1.2),
    'subsample': hp.uniform('subsample', max(0.5, best_params['subsample'] * 0.8), min(1.0, best_params['subsample'] * 1.2)),
    'colsample_bytree': hp.uniform('colsample_bytree', max(0.5, best_params['colsample_bytree'] * 0.8), min(1.0, best_params['colsample_bytree'] * 1.2)),
    'reg_alpha': hp.uniform('reg_alpha', best_params['reg_alpha'] * 0.8, best_params['reg_alpha'] * 1.2),
    'reg_lambda': hp.uniform('reg_lambda', best_params['reg_lambda'] * 0.8, best_params['reg_lambda'] * 1.2)
    }

    # Define the objective function
    def objective(params):
    model = lgb.LGBMClassifier(objective='multiclass', num_class=len(set(y_train)), **params)
    score = cross_val_score(model, X_train, y_train, cv=3, scoring='accuracy').mean()
    return {'loss': -score, 'status': STATUS_OK}

    # Run the optimization
    trials = Trials()
    best_hyperparams = fmin(fn=objective,
    space=search_space,
    algo=tpe.suggest,
    max_evals=50, # Adjust the number of evaluations based on your needs
    trials=trials,
    rstate=np.random.default_rng(42))

    print("Best hyperparameters found using Hyperopt: ", best_hyperparams)

    Continue reading...

Compartilhe esta Página