Hi, i would like to build a custom objective funct...
# mlforecast
j
Hi, i would like to build a custom objective function for xgboost. but all stand solutions fail, because i cant really alter the fit method like i could do in xgboost (where i can just specify a custom objective function). i tried multiple ways but all failed so far. do you guys have any idea on how to do this? it seems one issue is that the train data that i can access is not in the Dmatrix style. i am happy for any suggestions here
when i do something simple like having this custom objective function:
_def_ mse(_self_, _preds_, _labels_):
'''
mse
'''
grad = 2.0 * (_preds_ - _labels_)
hess = 2.0 * np.ones_like(_labels_)
return grad, hess
then i get an error like this during optimisation using optuna: UserWarning: Found null values in lag1, expanding_std_lag1, expanding_max_lag1, exponentially_weighted_mean_lag1_alpha0.3407844284244683, rolling_mean_lag1_window_size7_min_samples1, rolling_mean_lag1_window_size77_min_samples1, rolling_std_lag1_window_size7_min_samples1, rolling_std_lag1_window_size77_min_samples1, seasonal_rolling_mean_lag1_season_length7_window_size8_min_samples1, seasonal_rolling_std_lag1_season_length7_window_size8_min_samples1. warnings.warn(f'Found null values in {", ".join(cols_with_nulls)}.')
j
Hey. Have you tried this?
Oh nvm, I just read the optuna part
j
it is a bit weird, because when i work with dtrain like this, the error returned says they are numpy arrays not not Dmatrix objects, which i find starnge:
_def_ mse_jan(_self_, _preds_, _dtrain_):
labels = _dtrain_.get_label()
#black_friday_density = global_black_friday_density[dtrain.indices]
#penalties = np.where(black_friday_density > 0, 1000, 1.0)
grad = 2.0 * (_preds_ - labels) * penalties
hess = 2.0 * np.ones_like(labels)
return grad, hess
-- >
An error occurred in validation step 1: 'numpy.ndarray' object has no attribute 'get_label'
ok, live update: one of these, probably th labels had to be converted to np. arrays. this seems to work now:
_def_ mse_jan(_self_, _preds_, _y_):
#labels = dtrain.get_label()
#black_friday_density = global_black_friday_density[dtrain.indices]
#penalties = np.where(black_friday_density > 0, 1000, 1.0)
grad = 2.0 * (np.array(_preds_) - np.array(_y_)) # * penalties
hess = 2.0 * np.ones_like(_y_)
return grad, hess