XGBRegressor Hyperopt

mail@pastecode.io avatar
unknown
python
3 years ago
1.5 kB
1
Indexable
Never
space = {
    'max_depth': scope.int(hp.uniform('max_depth', 1, 11)),
    'eta': hp.loguniform('eta', np.log(0.0001), np.log(0.5)) - 0.0001,
    'n_estimators': scope.int(hp.quniform('n_estimators', 100, 6000, 200)),
    'gamma': hp.loguniform('gamma', np.log(0.0001), np.log(5)) - 0.0001,
    'min_child_weight': scope.int(hp.loguniform('min_child_weight', np.log(1), np.log(100))),
    'subsample': hp.uniform('subsample', 0.5, 1),
    'colsample_bytree': hp.uniform('colsample_bytree', 0.5, 1),
    'colsample_bylevel': hp.uniform('colsample_bylevel', 0.5, 1),
    'alpha': hp.loguniform('alpha', np.log(0.0001), np.log(1)) - 0.0001,
    'lambda': hp.loguniform('lambda', np.log(1), np.log(4)),
    'seed': hp.randint('seed', 5)
}

default_hiperparameters = {
    #'objective': 'binary',
    'scale_pos_weight': 1,
    #'random_state': None,
    'n_jobs': -1,
    'objective': 'reg:squarederror',
    'eval_metric': 'mae'
    #'random_state': 0
}

def objective(space):
    hiperparameters = {**default_hiperparameters, **space}
    model = xgb.XGBRegressor(
        **hiperparameters
    )

    accuracy = walk_forward_val_mean_score(model)

    return {'loss': (-100 + accuracy), 'status': STATUS_OK }

#spark_trials = SparkTrials(parallelism=4)
trials = Trials()
best = fmin(
        fn=objective,
        space=space,
        algo=tpe.suggest,
        max_evals=150,
        #trials=spark_trials,
        trials=trials,
        rstate=np.random.RandomState(seed=0),
        return_argmin=False
    )
best