在训练xgboost时,评价指标需要使用f1 macro,但是官方eval_metric中没有,所有需要自定义。
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')
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)