Custom Objective Function #437
Unanswered
jrodenberg0
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Hey. You can do something like here but to define the weights: import numpy as np
import pandas as pd
from lightgbm import LGBMRegressor
from mlforecast import MLForecast
from mlforecast.utils import generate_daily_series
class LGBMWeights(LGBMRegressor):
def fit(self, X, y):
weights = X['weight']
X = X.drop(columns='weight')
return super().fit(X=X, y=y, sample_weight=weights)
def predict(self, X):
if 'weight' in X.columns:
X = X.drop(columns='weight')
return super().predict(X=X)
freq = 'D'
h = 10
series = generate_daily_series(1, freq=freq)
series['weight'] = np.random.default_rng(seed=0).random(series.shape[0])
train_end = series['ds'].max() - h * pd.tseries.frequencies.to_offset(freq)
train_mask = series['ds'] <= train_end
train = series[train_mask]
valid = series[~train_mask]
fcst = MLForecast(
models=LGBMWeights(verbosity=-1),
freq=freq,
date_features=['dayofweek'],
)
fcst.fit(train, static_features=[]).predict(h=h, X_df=valid) I've been thinking of adding a wrapper like that, which takes the model and the name of the weight argument ( I've also been meaning to remove the custom training guide, since everything there can be achieved with scikit-learn estimators and/or pipelines and that allows you to also use it in cross validation. Please let us know if this doesn't work as expected and if you have any thoughts regarding the previous points. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible currently to integrate a custom objective function via MLForecast to LGBM or XGB?
I am attempting to use sample wieghts and I know that isn't currently supported. I was hoping to simply integrate my weights into my objective function (RMSE). Is there a better workaround for this?
(I know you can fit/predict with sample weights, but i have also noticed I can't pass exog features).
Beta Was this translation helpful? Give feedback.
All reactions