【问题标题】:GridSearchCV and XGBClassifier with eval_metric = 'mlogloss'带有 eval_metric = 'mlogloss' 的 GridSearchCV 和 XGBClassifier
【发布时间】:2017-09-15 23:42:42
【问题描述】:

是否可以在 GridSearchCV 中搜索 XGBClassifier 期间使用 eval_metric = 'mlogloss' ?一些例子将不胜感激。

【问题讨论】:

    标签: xgboost


    【解决方案1】:

    是的,这是可能的。 您可能需要向 GridSearchCV 提供一个返回 logloss 的 score 函数(负数,网格选择得分较高的模型,我们希望损失较小的模型),并使用最佳迭代的模型,如:

    from xgboost import XGBClassifier     
    from sklearn.grid_search import GridSearchCV
    from sklearn import metrics
    
    tuned_parameters = {'learning_rate': [0.4,0.5],
            'max_depth': [6,7]
        }
    
    fit_params={
        "eval_set":[(X_test_tr_boost, y_test)],
        "eval_metric": 'mlogloss',
        "early_stopping_rounds":100,
        "verbose":True
    }
    
    # XGBClassifier with early stopping Returns the model from the last iteration (not the best one).
    # In order to provide to GridSearchCV the score of the best model, we need to use a score function
    # to evaluate log_loss calling the estimator with the appropiate  ntree_limit param 
    #(instead of using scoring=‘neg_log_loss’ in GridSearchCV creation)
    #in order to use the best iteration of the estimator (ntree_limit)
    
    def _score_func(estimator, X, y):
        score1 = metrics.log_loss(y,estimator.predict_proba(X,
                               ntree_limit=estimator.best_ntree_limit),
                              labels=[0, 1, 2, 3, 4, 5, 6, 7, 8])
        return -score1
    
    model = XGBClassifier( objective ='multi:softprob',  seed=0,n_estimators=1000 )
    gridsearch = GridSearchCV(model, tuned_parameters, verbose=999999 ,
        scoring=_score_func,
        fit_params=fit_params
        )
    gridsearch.fit(X_train_tr_boost, y_train)
    
    print (gridsearch.best_params_)
    print (gridsearch.best_score_)
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2018-01-06
      • 2019-09-10
      • 2017-03-02
      • 2021-01-23
      • 2020-12-11
      • 1970-01-01
      • 2020-11-22
      • 2018-08-04
      相关资源
      最近更新 更多