std sklearn gridsearch无法与xgboost一起使用

时间:2019-11-12 12:26:58

标签: xgboost

将lgbm和xgboost与std gridsearch结合使用的代码:

y = df['default_0']
x = df.iloc[:, :-1]
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size = 0.25, random_state = 0)
#grid search + LGBM
def gridsearch_lgb(X_train, X_test, y_train, y_test):
    estimator = lgb.LGBMClassifier(learning_rate = 0.125, metric = 'auc', 
                        n_estimators = 20, num_leaves = 38)
    param_grid = {
        'n_estimators': [x for x in range(24,40,2)],
        'learning_rate': [0.10, 0.125, 0.15, 0.175, 0.2, 0.5]}

    gridsearch = GridSearchCV(estimator, param_grid,verbose=False)
    gridsearch.fit(X_train, y_train,
            eval_set = [(X_test, y_test)],
            eval_metric = ['auc'],
            early_stopping_rounds = 5)
    print()
    print("********************************************")
    print('Best parameters found by grid search are:', gridsearch.best_params_)
    print('best score: {0:.3f}'.format(gridsearch.best_score_))
def train_with_best_params_lgbm(X_train, X_test, y_train, y_test):
    print()
    print()
    print("LGBM: training with best parameters")
    print()
    gbm = lgb.LGBMClassifier(
    learning_rate = gridsearch.best_params_['learning_rate'],
    n_estimators = gridsearch.best_params_['n_estimators'],
    metric= 'auc')

    gbm.fit(X_train, y_train,
            eval_set=[(X_test, y_test)],
            eval_metric=['auc'],
    early_stopping_rounds=5)

它给了我

  
     

通过网格搜索找到的最佳参数为:> {{learning_rate':0.2,'n_estimators':24}最佳   得分:0.819

但是,当我尝试使用xgboost运行它时,gridsearch无法正常工作。

def gridsearch_xgb(X_train, X_test, y_train, y_test):
    estimator = xgb.XGBClassifier(verbosity=2,
                                  max_depth=4,learning_rate = 0.125,  
                        n_estimators = 20)
    param_grid = {
        'n_estimators': [x for x in range(24,40,2)],
        'max_depth':[2,4,6,8],
        'learning_rate': [0.10, 0.125, 0.15, 0.175, 0.2, 0.5]}

    gridsearch_xgb = GridSearchCV(estimator, param_grid,verbose=False)
    gridsearch_xgb.fit(X_train, y_train,
            eval_set = [(X_test, y_test)],
            eval_metric = ['auc'],
            early_stopping_rounds = 5)
    print()
    print("********************************************")
    print("********************************************")
    print("********************************************")
    print('Best parameters found by XGB grid search are:', gridsearch_xgb.best_params_)
    print('best score: {0:.3f}'.format(gridsearch_xgb.best_score_))

def train_with_best_params_xgb(X_train, X_test, y_train, y_test):
    print()
    print()
    print("XGBOOST: training with best parameters")
    print()
    gbm = xgb.XGBClassifier(
    learning_rate = gridsearch_xgb.best_params_['learning_rate'],
    n_estimators = gridsearch_xgb.best_params_['n_estimators'],

    base_score= 'auc')

    gbm.fit(X_train, y_train,
            eval_set=[(X_test, y_test)],
            eval_metric=['auc'],
    early_stopping_rounds=5)
  

错误:gridsearch_xgb没有“最佳参数”

该怎么办?如何运行xgboost + gridsearcH?

0 个答案:

没有答案