Python分类检测问题的常用指标 - TPR TNR TPR f1-score

1. fpr tpr等五个指标记录

def calculate_fpr_tpr_tnr_f1score_accuracy(y_true, y_pred):
    '''
    y_true 和 y_pred均是ndarray类型。
    '''
    y_true = y_true.flatten()
    y_pred = y_pred.flatten()
    Tp = 0
    Fp = 0
    Tn = 0
    Fn = 0
    for label, pred in zip(y_true, y_pred):
        if (label == 0) and (pred == 0):
            Tp = Tp + 1
        elif (label == 1) and (pred == 0):
            Fp = Fp + 1
        elif (label == 1) and (pred == 1):
            Tn = Tn + 1
        elif (label == 0) and (pred == 1):
            Fn = Fn + 1
        else:
            print('something weird with labels')
            return -1
            # sys.exit()
    # calculate precision, recall, accuracy, f1
    # it's possible for division by zero in some of these cases, so do a try/except
    try:
        precision = Tp / (Tp + Fp)
    except:
        precision = 0
    try:
        recall = Tp / (Tp + Fn)
    except:
        recall = 0
    try:
        accuracy = (Tn + Tp) / (Tn + Tp + Fn + Fp)
    except:
        accuracy = 0
    try:
        f1Score = 2 * precision * recall / (precision + recall)
    except:
        f1Score = 0
    try:
        fpr = Fp / (Fp + Tn)
    except:
        fpr = 0
    try:
        tpr = Tp / (Tp + Fn)
    except:
        tpr = 0
    try:
        tnr = Tn / (Tn + Fp)
    except:
        tnr = 0
    return (fpr, tpr, tnr, f1Score, accuracy)

(fpr, tpr, tnr, f1Score, accuracy) = \
                calculate_fpr_tpr_tnr_f1score_accuracy(test_label, prediction)

2. ROC曲线画图

ef compute_metrics(probs, yprobs):
    '''
    Returns
        fpr : Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i]
        tpr : Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].
        thresholds : Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1
        auc : Area Under the ROC Curve metric
    '''
    # calculate AUC
    auc = roc_auc_score(yprobs, probs)
    # calculate roc curve
    fpr, tpr, thresholds = roc_curve(yprobs, probs)

    return fpr, tpr, thresholds, auc





def find_nearest(array, value):
    idx = np.searchsorted(array, value, side="left")
    if idx > 0 and (idx == len(array) or math.fabs(value - array[idx - 1]) < math.fabs(value - array[idx])):
        return array[idx - 1], idx - 1
    else:
        return array[idx], idx


def draw_roc(fpr, tpr, thresholds):
    # find threshold
    targetfpr = 1e-3
    _, idx = find_nearest(fpr, targetfpr)
    threshold = thresholds[idx]
    recall = tpr[idx]

    # plot no skill
    plt.plot([0, 1], [0, 1], linestyle='--')
    # plot the roc curve for the model
    plt.plot(fpr, tpr, marker='.')
    plt.title('AUC: {0:.3f}\nSensitivity : {2:.1%} @FPR={1:.0e}\nThreshold={3})'.format(auc, targetfpr, recall,
                                                                                        abs(threshold)))
    # show the plot
    plt.show()

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值