I'm still trying to figure out how to add sample weights to MLForecast. According to
https://lightgbm.readthedocs.io/en/latest/pythonapi/lightgbm.LGBMRegressor.html#, sample weights can be added in two places; either in the parameters given when LGBMRegressor is called, or in the LGBMRegressor.fit method. However, the parameter only works "in case of loading data directly from text file" (
https://lightgbm.readthedocs.io/en/latest/Parameters.html), and the fit call from MLForecast doesn't allow a weight parameter (
https://nixtla.github.io/mlforecast/forecast.html#mlforecast.fit).
Anyway, my thought was that I could make a work-around by making a MLForecast object, doing the preprocessing, then extracting the model and training it with lightgbm calls, plugging it back in to MLForecast, and then using the predict feature of MLForecast (hence allowing me to take advantage of Nixtla's rolling feature calculations for future features). Code (see below) did not work as I was hoping (
ValueError: No fitted models found. You have to call fit or preprocess + fit_models.). Is there a way to make this work?
import pandas as pd
import numpy as np
from mlforecast import MLForecast
import lightgbm as lgb
cols = 4 # col 0 is target, col 1 is weights, cols 2 and 3 are features
rows = 20000
data=np.random.rand(rows,cols)
columns = ['y','weight','feat_1','feat_2']
data_df = pd.DataFrame(data[:,0:4], columns=columns)
data_df['unique_id'] = 1
data_df['ds']= [i for i in range(0, rows)]
model_1 = MLForecast(
models={
'LGBM': lgb.LGBMRegressor()
},
freq=1,
lags = [1, 2],
)
prep = model_1.preprocess(data_df, static_features = None, dropna = False)
train_weight = prep['weight']
train_x = prep.iloc[:,[2,3,6,7]]
train_y = prep['y']
model_1.models['LGBM'].fit(train_x,train_y,train_weight)
data_future = pd.DataFrame({'unique_id': [1,1,1],'ds':[rows,rows+1,rows+2], 'feat_1':[1,2,3], 'feat_2':[1,2,3]})
model_1.predict(3,X_df=data_future)