mattharrison / effective_xgboost_book

268 stars 30 forks source link

Writing over built in function round() p.106 #16

Open FutureGoose opened 11 months ago

FutureGoose commented 11 months ago

This use of round as a variable name shadows the built-in round() function within the scope of the for loop. It's generally considered poor practice to use the names of built-in functions for variable names, as it can lead to confusion and bugs if one intends to use the built-in function later on in the same scope.

Step-wise Tuning with Hyperopt

Groups of Hyperparameters

from hyperopt import fmin, tpe, hp, Trials
params = {'random_state': 42}

rounds = [{'max_depth': hp.quniform('max_depth', 1, 8, 1),  # tree
           'min_child_weight': hp.loguniform('min_child_weight', -2, 3)},
          {'subsample': hp.uniform('subsample', 0.5, 1),   # stochastic
           'colsample_bytree': hp.uniform('colsample_bytree', 0.5, 1)},
          {'reg_alpha': hp.uniform('reg_alpha', 0, 10),
            'reg_lambda': hp.uniform('reg_lambda', 1, 10),},
          {'gamma': hp.loguniform('gamma', -10, 10)}, # regularization
          {'learning_rate': hp.loguniform('learning_rate', -7, 0)} # boosting
]

all_trials = []
for round in rounds:  # <<<<<-------------
    params = {**params, **round}
    trials = Trials()
    best = fmin(fn=lambda space: xhelp.hyperparameter_tuning(space, X_train, 
                                        y_train, X_test, y_test),            
        space=params,           
        algo=tpe.suggest,            
        max_evals=20,            
        trials=trials,
    )
    params = {**params, **best}
    all_trials.append(trials)