Evan Miller

08/31/2023, 8:08 PM
I'm still trying to figure out how to add sample weights to MLForecast. According to, sample weights can be added in two places; either in the parameters given when LGBMRegressor is called, or in the method. However, the parameter only works "in case of loading data directly from text file" (, and the fit call from MLForecast doesn't allow a weight parameter ( 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?
Copy code
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

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(
        'LGBM': lgb.LGBMRegressor()
    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']

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]})

José Morales

08/31/2023, 8:15 PM
This example I shared yesterday is exactly about using sample weights

Evan Miller

08/31/2023, 8:30 PM
ah thank you!