Search code examples
pythonmachine-learningdata-sciencelightgbm

Light GBM early stopping does not work for custom metric


I have used a custom metric for light gbm but early stopping work for log loss which is the objective function how can I fix that or change early stopping to work for eval metric.

def evaluate_macroF1_lgb(truth, predictions):  
    pred_labels = predictions.reshape(len(np.unique(truth)),-1).argmax(axis=0)
    f1 = f1_score(truth, pred_labels, average='macro')
    return ('macroF1', f1, True) 

lg = LGBMClassifier(n_estimators=1000)

lg.fit(x_train,y_train,eval_set=(x_test,y_test),eval_metric=evaluate_macroF1_lgb,early_stopping_rounds=25)

I expected it to run for 1000 iteration or less but it ran for 25 as the log loss was not improving but f1 metric is improving.


Solution

  • Update

    I had found a solution we can set metric="custom" in LGBM classifier then it will use the eval metric.

    
    lg = LGBMClassifier(n_estimators=1000,metric="custom")