【发布时间】:2018-03-15 13:47:36
【问题描述】:
使用 XGBoost 包时,我们可以在 param dict 中设置 'objective'(例如:'objective': 'binary:logistic' )并将 dict 传递给 train 函数。 同时,train函数中有一个obj参数。据我所知,它们都是目标函数。那么他们有什么区别呢?如果两者都设置了,哪一个会生效?
param = {'max_depth': 3, 'eta': 1, 'silent': 1, 'objective': 'binary:logistic'}
bst = xgb.train(param, data_train, num_boost_round=n_round, obj=g_h, feval=error_rate)
其中 g_h 是自定义的目标函数。
很奇怪,我发现如果 'objective': 'binary:logistic' 和 obj 都设置了,y_hat 是
y_hat: [6.0993789e-06 9.8472750e-01 6.0993789e-06 ... 9.9993265e-01 4.4560062e-07
9.9993265e-01]
如果我跳过 'objective': 'binary:logistic' 并且只设置了 train 中的 obj,则 y_hat 是
y_hat: [-5.6174016 5.2989674 -5.6174016 ... 7.6525593 -6.4794073 6.7979865]
所以 train 函数中的 obj 不会覆盖 'objective': 'binary:logistic'!
这是代码:
import xgboost as xgb
def g_h(y_hat, y):
p = 1.0 / (1.0 + np.exp(-y_hat))
g = p - y.get_label()
h = p * (1.0-p)
return g, h
# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = {'max_depth':3, 'eta':1, 'silent':1, 'objective':'binary:logistic' }
num_round = 7
bst = xgb.train(param, dtrain, num_round)
# make prediction
y_hat = bst.predict(dtest)
print(y_hat)
【问题讨论】:
标签: python machine-learning xgboost