2
$\begingroup$

I have seen this in a kaggle notebook. I understand we add some weight to classes. what I don't understand is how those weights are generated. below is the code. Can you explain why it's useful and how weights are produced?

p = 0.369197853026293
pos_public = (0.55410 + np.log(1 - p)) / np.log((1 - p) / p)
pos_private = (0.55525 + np.log(1 - p)) / np.log((1 - p) / p)
average = (pos_public + pos_private) / 2
print (pos_public, pos_private, average)

w1 = average / p
w2 = (1 - average) / (1 - p)

dtrain = xgb.DMatrix(X, label = y)
def weighted_log_loss(preds, dtrain):
    label = dtrain.get_label()
    return "weighted_logloss", -np.mean(w1 * label * np.log(preds) + w2 * (1 - label) * 
              np.log(1 - preds))
$\endgroup$
1
  • $\begingroup$ The return statement implies that you are trying to define a metric, not a loss. If you want to define a loss function for xgboost you need 1st order and 2nd order derivative of your loss w.r.t. x also called gradient and hessian respectively. So what is it you are looking for , loss or metric? $\endgroup$ Commented Jan 10, 2021 at 19:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.