python - 对 lightgbm 模型使用 F1 自定义指标-Python
问题描述
我指的是这个链接来实现 custom_f1 eval_metric 但不知何故没有成功。
我的代码是:
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.]
解决方案
推荐阅读
- c++ - C++ 为什么我一调用 a->val 指针就将我带到某个奇怪的位置?
- python - 将特定文件移动到特定目录
- sql - Oracle SQL 加入和扩展
- angular - Angular,BsDatepicker 格式
- rally - 获取给定版本的 Rally 用户故事快照
- sql - 在 SQL Oracle 中逐行条件计数项目
- opengl - 几何着色器正在复制形状(处理中)
- node.js - 从 Kubernetes NGINX 入口控制器公开服务总是返回 502 Bad Gateway
- javascript - 如何使用 Firebase Functions 中的 firebase 实时数据库?(电报机器人)
- html - 用 html 消失内容