xgboost custom evaluation function: multi-category F1 macro 2020-09-24

When training xgboost, the evaluation index needs to use f1 macro, but it is not in the official eval_metric, and all need to be customized.

def f1_macro(preds, dtrain):
    """
    自定义f1_macro用于XGBoost的eval_metric.
    输入是xgboost模型预测的类别概率(margin aka probability)

    :param preds: 'numpy.ndarray'
        Estimated targets as returned by a classifier. Its Shape is (num_sample, num_class)
    :param dtrain: Dmatrix
        Data with the train labels
    :return:
    """
    #
    y_train = dtrain.get_label() # 'numpy.ndarray'
    y_pred = [np.argmax(d) for d in preds]
    return 'f1_macro', metrics.f1_score(y_train, y_pred, average='macro')

 

Python custom objective demo

https://github.com/dmlc/xgboost/blob/master/demo/guide-python/custom_objective.py

# user defined evaluation function, return a pair metric_name, result

# NOTE: when you do customized loss function, the default prediction value is
# margin, which means the prediction is score before logistic transformation.
def evalerror(preds, dtrain):
    labels = dtrain.get_label()
    preds = 1.0 / (1.0 + np.exp(-preds))  # transform raw leaf weight
    # return a pair metric_name, result. The metric name must not contain a
    # colon (:) or a space
    return 'my-error', float(sum(labels != (preds > 0.5))) / len(labels)

 

Guess you like

Origin blog.csdn.net/authorized_keys/article/details/108766953