0

I am trying to build a custom loss function for XGBRegressor. My custom function looks like below. However, the predictions I receive are way off and are negative. I am not understanding where I am going wrong.

def custom_loss():
    def custom_se(y_pred, y_true):
        #error = y_true - y_pred
        grad = 2 * y_pred * y_true - 2 * (y_true ** 2)
        hess = 2 * y_true 
        return grad, hess
    return custom_se

xgbr = XGBRegressor(objective=custom_loss())

xgbr.fit(x_train, y_train)
ypred = xgbr.predict(x_test)

# first 10 predictions and actual values
ypred = [-4.3285046 -4.3285046 -4.3285046 -5.356496  -3.9780703 -4.3285046 -4.723314  -3.9780703 -4.3285046 -6.49606  ]
y_test = [17.21 16.02 15.05 17.35 14.14 15.94 19.16 17.33 17.12 29.49]

I did check this link and followed the steps but still not getting correct results. Creating a Custom Objective Function in for XGBoost.XGBRegressor

Flavia Giammarino
  • 7,987
  • 11
  • 30
  • 40
shwts
  • 11
  • 4
  • Try https://datascience.stackexchange.com/questions/74780/how-to-implement-custom-loss-function-that-has-more-parameters-with-xgbclassifie – Chris Jun 10 '22 at 14:04

0 Answers0