zoukankan      html  css  js  c++  java
  • 【笔记】ROC曲线

    ROC曲线

    前文讲了PR曲线

    这里说ROC曲线,其描述的是TPR和FPR之间的关系

    TPR是什么呢,TPR就是召回率

    FPR是什么呢,FPR就是和TPR对应的,即真实值为0的一行中的预测为1的部分比例

    和精准率和召回率一样,TPR和FPR之间也有着内在的联系,TPR越高,FPR越高,反之一样,ROC曲线就是刻画这样的关系的曲线

    快速的实现一下TPR和FPR的函数,在python chame中的metrics中写入下列代码,依次是实现TN,FP,FN,TP,混淆矩阵,精准率,召回率,F1 score,TPR,FPR,前面部分都在前面博客有相应的原理的代码,关于TPR和FPR的,也只是将公示带入使用

    代码如下

    def TN(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        return np.sum((y_true == 0) & (y_predict == 0))
    
    
    def FP(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        return np.sum((y_true == 0) & (y_predict == 1))
    
    
    def FN(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        return np.sum((y_true == 1) & (y_predict == 0))
    
    
    def TP(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        return np.sum((y_true == 1) & (y_predict == 1))
    
    
    def confusion_matrix(y_true, y_predict):
        return np.array([
            [TN(y_true, y_predict), FP(y_true, y_predict)],
            [FN(y_true, y_predict), TP(y_true, y_predict)]
        ])
    
    
    def precision_score(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        tp = TP(y_true, y_predict)
        fp = FP(y_true, y_predict)
        try:
            return tp / (tp + fp)
        except:
            return 0.0
    
    
    def recall_score(y_true, y_predict):
        assert len(y_true) == len(y_predict)
        tp = TP(y_true, y_predict)
        fn = FN(y_true, y_predict)
        try:
            return tp / (tp + fn)
        except:
            return 0.0
    
    
    def f1_score(y_true, y_predict):
        precision = precision_score(y_true, y_predict)
        recall = recall_score(y_true, y_predict)
    
        try:
            return 2 * precision * recall / (precision + recall)
        except:
            return 0.0
    
    
    def TPR(y_true, y_predict):
        tp = TP(y_true, y_predict)
        fn = FN(y_true, y_predict)
        try:
            return tp / (tp + fn)
        except:
            return 0.
    
    
    def FPR(y_true, y_predict):
        fp = FP(y_true, y_predict)
        tn = TN(y_true, y_predict)
        try:
            return fp / (fp + tn)
        except:
            return 0.
    

    具体使用

    (在notebook中)

    使用手写数据集,进行先前的操作布置好需要的变量以及数据分割,不再赘述

    使用封装好的FPR和TPR,和前面绘制PR曲线的思想一致,然后绘制图像

      from metrics import FPR,TPR
    
      fprs = []
      tprs = []
      thresholds = np.arange(np.min(decision_scores),np.max(decision_scores),0.1)
      for threshold in thresholds:
          y_predict = np.array(decision_scores >= threshold,dtype='int')
          fprs.append(FPR(y_test,y_predict))
          tprs.append(TPR(y_test,y_predict))
    
      plt.plot(fprs,tprs)
    

    图像如下

    使用sklearn中的ROC曲线,调用方式和先前PR曲线的使用很像,绘制图像

      from sklearn.metrics import roc_curve
    
      fprs, tprs, thresholds = roc_curve(y_test,decision_scores)
    
      plt.plot(fprs,tprs)
    

    图像如下(ROC曲线下的面积可以作为一个指标)

    求解的话一样可以使用sklearn中的roc_auc_score,即可求出面积值的作为的指标

      from sklearn.metrics import roc_auc_score
    
      roc_auc_score(y_test,decision_scores)
    

    结果如下

    可以看出来,ROC的指标对偏斜的数据不算敏感,不想精准率和召回率那样敏感,所以针对极度偏斜的数据使用精准率和召回率是不错的,ROC曲线的应用场景是可以确定更好的模型,即面积更大模型

  • 相关阅读:
    【leetcode❤python】 1. Two Sum
    【leetcode❤python】 67. Add Binary
    【leetcode❤python】 396. Rotate Function
    【leetcode❤python】 400. Nth Digit
    【leetcode❤python】 160. Intersection of Two Linked Lists
    【leetcode❤python】 203. Remove Linked List Elements
    【leetcode❤python】 225. Implement Stack using Queues
    Kotlin:【标准库函数】apply(配置函数)、let、run函数
    Kotlin:【字符串操作】substring、split、replace、字符串比较==与===、foreach遍历字符
    Kotlin:【针对空安全管理的操作】安全调用操作符、使用带let的安全调用、非空断言操作符(感叹号操作符)、使用if判断null值情况、使用空合并操作符(类似三元表达式)
  • 原文地址:https://www.cnblogs.com/jokingremarks/p/14325313.html
Copyright © 2011-2022 走看看