【乾貨】 xgboost如何自定義eval_metric ( feval ) ?

,#【乾貨】 xgboost如何自定義eval_metric ( feval ) ?

轉載請註明原文地址:http://blog.csdn.net/weixin_38100489/article/details/78714251

問題來源:xgboost的eval_metric裏如果沒有你要用的,比如kaggle裏有用到的rmsle就沒有就需要自己定義。

http://xgboost.readthedocs.io/en/latest/parameter.html
官網截圖

上代碼:(把eval_metric變成rmsle的例子)

dtrain=xgb.DMatrix(train_x,train_y)
dtest=xgb.DMatrix(test_x,test_y_real)
watchlist=[(dtrain,'train'),(dtest,'test')]

#############
def evalerror(preds, dtrain):       # written by myself
    labels = dtrain.get_label()
    # return a pair metric_name, result
    # since preds are margin(before logistic transformation, cutoff at 0)
    return 'error', math.sqrt(mean_squared_log_error(preds,labels))
#############

params = {
            'objective': 'reg:gamma',
            'eta': 0.1,
            'seed': 0,
            #'eval_metric': 'rmse',        # if it is useless, delete it
            'missing': -999,
            'silent' : 1,
            'gamma' : 1,
            'subsample' : 0.5,
            'alpha' : 1,
            'max_depth':10,
            'min_child_weight':1
            }
num_rounds=500
clf=xgb.train(params,dtrain,num_rounds,watchlist, feval=evalerror)

這樣就可以啦,關鍵就是‘feval=’,在xgb.train內的實參是叫feval,不是叫eval_metric !!!
(上面代碼參數我都可以改花了)

xgb.train(params, dtrain, num_boost_round, evals, obj, feval, maximize, early_stopping_rounds, evals_result, verbose_eval, learning_rates, xgb_model, callbacks)

中的第六個。

思路來源是https://www.zhihu.com/question/39496618 上的大神 lau phunter 的回答,https://github.com/dmlc/xgboost/blob/master/demo/guide-python/custom_objective.py
———————————————————————————————————————————————————————
~一把辛酸淚,可費勁了~
我的知乎:https://www.zhihu.com/people/xu-jian-zhi/activities

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章