0
$\begingroup$

Sorry for long post,im triying to run a xgb model but for some reason takes like 20 to 30 min(per run) with a specific set of hyperparams, but when i run hyperopt to get best params, takes like 7 seconds to run (per combination of params), dont know what i am doing wrong in my code (tried to run the same params from the 7 second run from hyperopt in the stand alone model, still 20-30 min to finish),even AUC score differs alot and i dont know what is causing this.

heres what i got:

 import matplotlib.pylab as plt
 from sklearn import metrics
 from matplotlib import pyplot
 %matplotlib inline


 def evaluate_model(alg, train, target, predictors,  early_stopping_rounds=10):


    
    evaluation = [( independent, dependent), ( independent1, dependent1)]#sd
    alg.fit(train[predictors], target['FAILURE_TARGET'], eval_metric=["error", "logloss","auc"], eval_set=evaluation, verbose=False)
        
 
    dtrain_predictions = alg.predict(train[predictors])
    dtrain_predprob = alg.predict_proba(train[predictors])[:,1]


    #Print Reporte de modelo:
    print("\n Reporte de Modelo")
    #print("No. de vars : %.4g" % feat_imp.count())
    print("Accuracy : %.4g" % metrics.accuracy_score(target['FAILURE_TARGET'].values, dtrain_predictions))
    print("AUC Score (Balanced): %f" % metrics.roc_auc_score(target['FAILURE_TARGET'], dtrain_predprob)) 

then i define the model (these params are from the 7 second hyperopt run)

import xgboost.sklearn as xgb
from xgboost.sklearn import XGBClassifier
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder


xgb0 = xgb.XGBClassifier(max_depth=8, learning_rate=0.004218004917815386, n_estimators=945, silent=None, objective='binary:logistic', n_jobs=8,  
                         gamma=18.035064499985307, min_child_weight=3, subsample=1, colsample_bytree=0.8692537560022474, colsample_bylevel=1, colsample_bynode=1, 
                         reg_alpha=10, reg_lambda=0.9500090445813872, scale_pos_weight=1, base_score=0.5, random_state=0, seed=0, use_label_encoder =False)

finally heres my hyperopt code

#!pip install hyperopt
import hyperopt
from hyperopt import STATUS_OK, Trials, fmin, hp, tpe
    from hyperopt.pyll import scope

    from sklearn import metrics
space={'max_depth':hp.quniform ('max_depth', 3, 10, 1),
        'gamma': hp.uniform ('gamma', 1,20),
        'reg_alpha' : hp.quniform('reg_alpha', 1,60,1),
        'reg_lambda' : hp.uniform('reg_lambda', 0,1),
        'colsample_bytree' : hp.uniform('colsample_bytree', 0.8,1),
        'min_child_weight' : hp.quniform('min_child_weight', 0, 10, 1),
        'learning_rate' : hp.uniform ('learning_rate', .0001,.005),
        'n_estimators' :hp.quniform ('n_estimators', 100, 1000, 1),
        'seed': 0
    }
def objective(space):
    clf=xgb.XGBClassifier(
                    n_estimators =int(space['n_estimators']), max_depth = int(space['max_depth']), gamma = space['gamma'],
                    reg_alpha = int(space['reg_alpha']),min_child_weight=int(space['min_child_weight']),
                    colsample_bytree=int(space['colsample_bytree']),learning_rate=space['learning_rate'], use_label_encoder =False,
                    objective='binary:logistic')
                     

    
    evaluation = [( independent, dependent), ( independent1, dependent1)]
    
    clf.fit(independent, dependent,early_stopping_rounds=10,verbose=False, eval_metric=["auc"], eval_set=evaluation)
    
    dtrain_predictions = clf.predict(independent1)
    dtrain_predprob = clf.predict_proba(independent1)[:,1]

   
    accuracy = metrics.accuracy_score(df_testing['FAILURE_TARGET'].values, dtrain_predictions)
    accuracy_AUC = metrics.roc_auc_score(df_testing['FAILURE_TARGET'], dtrain_predprob)
   
    


    
    print ("SCORE :", accuracy , space,
           "SCORE AUC:", accuracy_AUC)
    #print ("hyper",hyperopt.space_eval(space, best_hyperparams))
    #return {'loss': -accuracy_AUC, 'status': STATUS_OK 
    return {'loss': -accuracy_AUC, 'status': STATUS_OK }
trials = Trials()

    best_hyperparams = fmin(fn = objective,
                            space = space,
                            algo = tpe.suggest,
                            max_evals = 50000,
                            trials = trials)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.