首页 > 解决方案 > 对 lightgbm 模型使用 F1 自定义指标-Python

问题描述

我指的是这个链接来实现 custom_f1 eval_metric 但不知何故没有成功。

lightgbm 中的 f1_score 指标

我的代码是:

def lgb_f1_score(label, preds):
    #y_true = data.get_label()
    label= val_y.ravel()
    #preds = neigh.predict_proba(val_x)
    
    preds = preds.reshape(-1, 1)
    preds = preds.argmax(axis = 1)
    y_hat = np.where(preds < 0.5, 0, 1)   # scikits f1 doesn't like probabilities
    print (y_hat)
    print(label)
    return 'f1', f1_score(label, y_hat, average='binary'), True

folds = StratifiedKFold(n_splits=10, shuffle=True, random_state=123)
oof_preds = np.zeros((train_data.shape[0],2))
sub_preds = np.zeros((test_df.shape[0],2))
for n_fold, (trn_idx, val_idx) in enumerate(folds.split(train_data, target_feature)):
    trn_x, trn_y = train_data.iloc[trn_idx], target_feature[trn_idx]
    val_x, val_y = train_data.iloc[val_idx], target_feature[val_idx]
    
    neigh.fit(trn_x, trn_y.ravel(), eval_set=(val_x,val_y.ravel()),eval_metric=lgb_f1_score)
    oof_preds[val_idx] = neigh.predict_proba(val_x)
    print (oof_preds)
    test_df = test_df[main_cols]
    sub_preds += neigh.predict_proba(test_df)/ folds.n_splits

pred_prob=pd.DataFrame(sub_preds, columns=['pred_0', 'pred_1'])

它不会立即进行预测。fold 6/7 的示例输出如下所示:F1-分数即将归零。

[6] valid_0's binary_logloss: 0.318003  valid_0's f1: 0
[0.09100276 0.03411894 0.15204226 ... 0.05577297 0.03602447 0.13468759]
[0 0 0 ... 0 0 0]
[0. 0. 0. ... 0. 0. 0.]
[7] valid_0's binary_logloss: 0.3158    valid_0's f1: 0
[0.08955322 0.02772688 0.14978066 ... 0.04866767 0.03135154 0.13264376]
[0 0 0 ... 0 0 0]
[0. 0. 0. ... 0. 0. 0.]

标签: pythonlightgbm

解决方案


推荐阅读