f1_score自定义损失函数及评测函数
def custom_f1_eval(y_true, y_pred):
num_TP = 0
num_pred = 0
threshold = 0.5
for i in range(len(y_true)):
if y_pred >= threshold:
num_pred += 1
if y_true == 1:
num_TP += 1
precision = num_TP / num_pred
recall = num_TP / len(y_true[y_true == 1])
f1 = 2 * precision * recall / (precision + recall)
return "f1", f1_mean, True
def f1_loss(y, pred):
beta = 2
p = 1. / (1 + np.exp(-pred))
grad = p * ((beta - 1) * y + 1) - beta * y
hess = ((beta - 1) * y + 1) * p * (1.0 - p)
return grad, hess
f1_lossfe
使用方法:
clf = LGBMClassifier(
learning_rate=0.001,
n_estimators=n_estimators,
num_leaves=127, # 20, max_depth:5
subsample=0.8,
colsample_bytree=0.8,
random_state=2019,
#scale_pos_weight=50,
metric=None,
)
clf.set_params(**{"objective": f1_loss})
clf.fit(X_train, y_train, eval_set=[(X_test, y_test)],
eval_metric=lambda y_true, y_pred: [custom_f1_eval(y_true, y_pred)],
early_stopping_rounds=early_stopping_rounds, verbose=10)
参考网址: