Light GBM 提前停止不适用于自定义指标

Light GBM early stopping does not work for custom metric

我为 light gbm 使用了一个自定义指标,但提前停止工作是为了日志丢失,这是 objective 函数我如何修复它或更改提前停止以用于 eval 指标。

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)

我预计它会 运行 进行 1000 次或更少的迭代,但它会 运行 进行 25 次,因为对数损失没有改善,但 f1 指标正在改善。

更新

我找到了一个解决方案,我们可以在 LGBM 分类器中设置 metric="custom" 然后它将使用 eval 度量。


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