固定随机种子. F1阈值搜索和F-0.3的实现

固定随机种子

def set_seed(seed=42):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    
set_seed()

F1阈值搜索

def f_score_search(df):
    t0 = 0.01
    v = 0.001
    best_t = t0
    best_f = 0
    best_p, best_r = 0, 0
    for step in range(950):
        curr_t = t0 + step * v

        p, r, curr_f = f_beta(df_oof['pred'], df_oof['label'], threshold=curr_t)
        if curr_f > best_f:
            best_t = curr_t
            best_f = curr_f
            best_p = p
            best_r = r
    print(f'best threshold: {best_t},  precision={best_p}; recall={best_r};  best f_score: {best_f}')
    return best_t, best_f

F-0.3实现

def f_beta(y_hat, y_true,  threshold):

    y_hat = y_hat > threshold
    y_hat = np.int8(y_hat)
    
    true_positive = np.sum(y_hat*y_true, axis=0)
    false_positive = np.sum(y_hat*(1-y_true), axis=0)
    false_negative = np.sum((1-y_hat)*y_true, axis=0)
    
    precision = true_positive / (true_positive + false_positive )
    recall = true_positive / (true_positive + false_negative )

    f_score = (1 + (0.3 ** 2)) * precision * recall / ((0.3 ** 2) * precision + recall)
    
    return precision, recall, f_score
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值