lightgbm 手动评分功能:F1_SCORE

lightgbm Manual scoring function : F1_SCORE

这里我试图实现手动f1_score函数进行评估,但在代码中这不是调用manual_scoring函数{No print o/p} 而不是因错误而失败:

" call() missing 1 required positional argument: 'y_true' "

如果我删除 feval 代码就可以正常工作。

def maual_scoring(y_hat, data):
    print("I am here")
    y_true = data.get_label()
    y_hat = np.argmax(y_hat, axis =1 ) #multi classification problem
    return 'f1', f1_score(y_true, y_hat), True

model = lgb.train(
    params = lgb_params.copy(),
    train_set=lgb_model,
    valid_sets=[lgb_model, lgb_val],
    valid_names=['Train', 'Validation'],
    verbose_eval=100, 
    feval=maual_scoring,
    num_boost_round=99999,
    early_stopping_rounds=100
)

得到答案: 需要重塑 pred

def maual_scoring(preds, dtrain):
    labels = dtrain.get_label()
    preds = preds.reshape(-1, 4)  # I should have reshaped pred
    preds = preds.argmax(axis = 1)
    f_score = f1_score(preds, labels, average = 'macro')
    return 'f1_score', f_score, True

feval (callable or None, optional (default=None)) – Customized evaluation function. Should accept two parameters: preds, train_data. For multi-class task, the preds is group by class_id first, then group by row_id. If you want to get i-th row preds in j-th class, the access way is preds[j * num_data + i]. Note: should return (eval_name, eval_result, is_higher_better) or list of such tuples. To ignore the default metric corresponding to the used objective, set the metric parameter to the string "None" in params.

因为这是多分类问题所以我们需要重塑预测来得到 o/p 形状与 model.predict_proba()

相似