66-ROC曲线

  
  上一篇博客介绍了 PR 曲线,这一篇博客介绍另一种曲线 ROC 曲线。


ROC曲线

  ROC(Receiver Operating Characteristic Curve),它其实是统计学上经常使用的一个术语,它描述的是 TPRFPR 之间的关系。

  那么我们首先来看看 TPRFPR 这两个指标是什么意思?

  TRP,是 True Positive Result 的缩写。其实 TPR 和我们之前学的 R e c a l l Recall Recall 是一个意思。

在这里插入图片描述
  

FPR 是 False Positive Result 的缩写。其实它和 TPR 是对应的关系。

在这里插入图片描述
  
  其实 TPRPPR 之前也存在着一种联系。我们还是画出一条 score 轴,轴上分布着很多样本点,而星形是我们关注的样本点,即分类为 1,那么圆形的分类为 0。

  当我们取不同的 t h r e s h o l e threshole threshole(阈值)来对我们的样本进行分类的时候,相应的 TPRFRP 是怎样变化的?

  当我们的 t h r e s h o l e threshole threshole 取得非常大的时候,我们计算相应的 TPRPPR

在这里插入图片描述
  如果我们减小 t h r e s h o l e threshole threshole ,计算相应的 TPRPPR

在这里插入图片描述

  我们继续减少 t h r e s h o l e threshole threshole ,计算相应的 TPRPPR

在这里插入图片描述
  
  那么很容易观察到,随着 t h r e s h o l e threshole threshole 的变化,我们的 TPRPPR 是怎么变化的。在 t h r e s h o l e threshole threshole 逐渐降低的过程中,FPR 是在逐渐升高的,TPR 也在逐渐升高。换句话说,TPRPPR 之间是呈现相一致的趋势。TPR 越高,FPR 跟着也变高。和 p r e c i s i o n precision precision r e c a l l recall recall 之间的关系是正好相反的。

  当然,我们也可以直观地理解一下,为了提高 TPR,我们就必须拉低阈值,阈值拉低以后,相应的我们犯 FP 这种错误也会增高。其实 ROC 曲线就是刻画这两个指标之间的关系。

  下面我们实现一个 TPRFPR 两个函数。我们在之前的 metrics.py 文件中添加下列函数。

# metrics.py
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
# f1_score
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.0
def FPR(y_true, y_predict):
    tn = TN(y_true, y_predict)
    fp = FP(y_true, y_predict)
    try:
        return fp / (tn + fp)
    except:
        return  0.0

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


  那么 ROC 的应用场合在哪里呢?就在于比较两个模型的孰优孰劣。比如下面这个图。

在这里插入图片描述
  这两根曲线就代表了两个模型,或者是同一个模型由两组超参数得到的模型,这种情况下,我们应该选择曲线下面面积(曲线与 x x x 轴和 x = 1 x = 1 x=1 所围的面积)更大的那个模型,我们认为这个模型相应的是一个更好的模型。

  至此,我们谈论的都是二分类问题,下一篇博客将会谈论在多分类问题的情况。

  具体代码见 66 ROC曲线.ipynb

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值