I'm using the newest version of xgboost package in python 2.7 and based on my problem, I'm going to change xgboost cost function to use my own defined cost function. Couple of questions:

In which part of the xgboost package should I find and change the cost function? After changing the cost function, how can I add the updated code to the xgboost package to use the new training function instead of xgboost's pre-defined function?

  • 3
    $\begingroup$ Reading the docs will help $\endgroup$ – Aditya Apr 22 '18 at 2:10

This is discussed in stackoverflow, just to recap:

Define your your customized cost function, e.g.:

 def new_cost(y_pred, y_true):
    # perform calculation for new cost
    return 'new_cost', score

Then pass it to the feval argument (see official doc) for training like:

model = xgboost.train(params, dtrain, num_rounds, watchlist, feval=new_cost)

There are many examples out there if you search harder for more concrete use cases.

| improve this answer | |
  • 2
    $\begingroup$ Here you can also see an example of a custom objective in xgboost github.com/dmlc/xgboost/blob/master/demo/guide-python/… $\endgroup$ – wacax Apr 23 '18 at 2:46
  • 1
    $\begingroup$ feval is used for customized evaluation function. feval (function) – Customized evaluation function. For defining customized cost function use obj (function) – Customized objective function. $\endgroup$ – user90242 Feb 18 at 5:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.