how to pass additional parameters to lgbm custom loss function?

Viewed 599

I have write the rmsse custom loss function in a following way

def wrmsse(preds, y_true,store_name):
    '''
    preds - Predictions: pd.DataFrame of size (30490 rows, N day columns)
    y_true - True values: pd.DataFrame of size (30490 rows, N day columns)
    sequence_length - np.array of size (42840,)
    sales_weight - sales weights based on last 28 days: np.array (42840,)
    '''
    preds = preds[-(30490 * 28):]
    y_true = y_true.get_label()[-(30490 * 30490):]
    preds = preds.reshape(28, 30490).T
    y_true = y_true.reshape(28, 30490).T    
    sw = list(SW_store.keys())[key]
    return 'wrmsse', np.sum(np.sqrt(np.mean(np.square(rollup(preds-y_true)),axis=1)) * sw)/12,False #<-used 

and I'm training the modal like below

model = 

store_name = 'CA_1    lgbm.train(params,train_set=train_set,num_boost_round=2500,early_stopping_rounds=50,valid_sets=val_set,verbose_eval = 100, feval= wrmsse)

I would like pass the store name as parameter, how can I do it?

1 Answers

You can do it by attaching ur custom ndarray with the dataset,

For example, after you declare your datasets set the custom class attributes,

dtrain = lgb.Dataset(X_train, y_train, feature_name =feature_names, categorical_feature=categorical_feature, free_raw_data=False)
dval = lgb.Dataset(X_val, y_val, reference=dtrain, feature_name =feature_names, categorical_feature=categorical_feature, free_raw_data=False)

dtrain.indexes = np.arange(0, X_train.shape[0])
dval.indexes =  np.arange(0, X_val.shape[0])

Here indexes are the custom array that I would like to use in metric,

Then, in your metric function pass your custom arrays as closure and access them with the indexes,

def utility_score(weight, resp, date_):   
    def func(preds, train_data):
        score = 0.
        labels = train_data.get_label()
        indexes = train_data.indexes
        y_pred = preds.reshape(-1, 1)

        weight_ = weight[indexes, :]
        resp_ = resp[indexes, :]
        date__ = date_[indexes, :]
       
        # do whatever with ur custom vars and calculate score....
        
        return 'utility', score, True
    return func

Use it like this,

feval=utility_score(weight, resp, date_)
Related