ROC,AUC,PRC,AP+Python代码实现

ROC,AUC,PRC,AP+Python代码实现

输入:所有测试样本的真值,分类预测结果
输出:PR曲线,AP,ROC曲线,AUC
ROC曲线可以使用自写代码,也可以直接使用sklearn给的方法
PRC,AP计算都用的sklearn的方法


代码块

代码块语法遵循标准markdown代码,例如:

from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score

from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score

import matplotlib.pyplot as plt
import numpy as np

def Get_ROC(y_true,pos_prob):
    pos = y_true[y_true==1]
    neg = y_true[y_true==0]
    threshold = np.sort(pos_prob)[::-1]        # 按概率大小逆序排列
    y = y_true[pos_prob.argsort()[::-1]]
    tpr_all = [0] ; fpr_all = [0]
    tpr = 0 ; fpr = 0
    x_step = 1/float(len(neg))
    y_step = 1/float(len(pos))
    y_sum = 0                                  # 用于计算AUC
    for i in range(len(threshold)):
        if y[i] == 1:
            tpr += y_step
            tpr_all.append(tpr)
            fpr_all.append(fpr)
        else:
            fpr += x_step
            fpr_all.append(fpr)
            tpr_all.append(tpr)
            y_sum += tpr
    return tpr_all,fpr_all,y_sum*x_step         # 获得总体TPR,FPR和相应的AUC

if __name__ == '__main__':
    y_score = np.loadtxt('Youtput_prediction.txt',dtype=np.float32)#np.array([])
    y_true = np.loadtxt('Youtput_true.txt',dtype=np.float32)#np.array([])
    # print(y_score)
    # print(y_true)
    precision, recall, _ = precision_recall_curve(y_true, y_score)
    average_precision = average_precision_score(y_true, y_score)

    p_r_curve =plt.figure()
    plt.step(recall, precision, color='b', alpha=0.2,
             where='post')
    plt.fill_between(recall, precision, step='post', alpha=0.2,
                     color='b')

    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.ylim([0.0, 1.05])
    plt.xlim([0.0, 1.05])
    plt.title('2-class Precision-Recall curve: AP={0:0.2f}'.format(
              average_precision))
    plt.show(p_r_curve)


    # tpr,fpr,auc = Get_ROC(y_true,y_score)
    #
    # roc_c = plt.figure()#figsize=(10,6)
    # plt.plot(fpr,tpr,label="Area Under the Curve (AUC: {:.3f})".format(auc),linewidth=2)
    # plt.xlabel("False Positive Rate",fontsize=16)
    # plt.ylabel("True Positive Rate",fontsize=16)
    # plt.title("ROC Curve",fontsize=16)
    # plt.legend(loc="lower right",fontsize=16)
    # plt.show(roc_c)


    fpr, tpr, thresholds = roc_curve(y_true, y_score, pos_label=1)
    AUC_ROC = roc_auc_score(y_true, y_score)

    roc_curve =plt.figure()
    plt.plot(fpr,tpr,'-',label='Area Under the Curve (AUC = %0.4f)' % AUC_ROC)
    plt.title('ROC curve')
    plt.xlabel("FPR (False Positive Rate)")
    plt.ylabel("TPR (True Positive Rate)")
    plt.legend(loc="lower right")
    plt.show(roc_curve)
  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷失的walker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值