首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >eval_metric = 'mlogloss‘的GridSearchCV和XGBClassifier

eval_metric = 'mlogloss‘的GridSearchCV和XGBClassifier
EN

Stack Overflow用户
提问于 2017-04-20 00:09:53
回答 1查看 3.4K关注 0票数 2

是否可以在搜索GridSearchCV中的XGBClassifier时使用eval_metric = 'mlogloss‘?一些例子将会受到很大的赞赏。

EN

回答 1

Stack Overflow用户

发布于 2017-08-23 05:16:35

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

代码语言:javascript
复制
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_)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43500893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档