-->

了解ROC曲线(Understanding ROC curve)

2019-10-29 23:15发布

import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc , roc_auc_score
import numpy as np

correct_classification = np.array([0,1])
predicted_classification = np.array([1,1])

false_positive_rate, true_positive_rate, tresholds = roc_curve(correct_classification, predicted_classification)

print(false_positive_rate)
print(true_positive_rate)

从https://en.wikipedia.org/wiki/Sensitivity_and_specificity :

True positive: Sick people correctly identified as sick 
False positive: Healthy people incorrectly identified as sick 
True negative: Healthy people correctly identified as healthy 
False negative: Sick people incorrectly identified as healthy

我使用这些值0:生病了,1:健康

从https://en.wikipedia.org/wiki/False_positive_rate :

FLASE阳性率=假阳性/(假阳性+真阴性)

假阳性的数量:0数目的真阴性的:1

因此,假阳性率= 0/0 + 1 = 0

阅读roc_curve的返回值( http://scikit-learn.org/stable/modules/generated/sklearn.metrics.roc_curve.html#sklearn.metrics.roc_curve ):

FPR:阵列,形状= [> 2]

增加假阳性率,使得基元i与得分> =阈值的预测[I]的假阳性率。

TPR:阵列,形状= [> 2]

增加真阳性率,使得基元i与得分> =阈值的预测[I]的真阳性率。

阈值:阵列,形状= [n_thresholds]

在决策功能降低阈值来计算FPR和TPR。 阈值[0]表示被预测没有实例,并任意设置为MAX(y_score)+ 1。

这是一个多么不同的值,以我的假阳性率的手工计算? 阈值是如何设置的? 这里提供了一些门槛模式的信息: https://datascience.stackexchange.com/questions/806/advantages-of-auc-vs-standard-accuracy但我很困惑,它如何与这个实现适合?

Answer 1:

首先,维基百科正在考虑患病= 1。

真阳性:生病的人正确地识别为生病

第二,每一个模型具有基于正类(通常为0.5)的概率某个阈值。

因此,如果阈值是0.1,具有概率大于0.1将被分类为阳性的所有样本。 产生预测样本的概率是固定的,阈值将被改变。

roc_curve ,从scikit学习提高阈值:

 0 (or minimum value where all the predictions are positive) 

1 (Or the last point where all predictions become negative).

中间点是基于从正到负的预测的变化决定。

例:

Sample 1      0.2
Sample 2      0.3
Sample 3      0.6
Sample 4      0.7
Sample 5      0.8

这里的最低概率是0.2,所以最低门槛做出任何意义是0.2。 现在,随着我们不断的门槛越来越高,因为有在这个例子非常少点,临界点,将在每个概率修改(等于概率,因为多数民众赞成在肯定和否定的数量变化的位置)

                     Negative    Positive
               <0.2     0          5
Threshold1     >=0.2    1          4
Threshold2     >=0.3    2          3
Threshold3     >=0.6    3          2
Threshold4     >=0.7    4          1
Threshold5     >=0.8    5          0


Answer 2:

上面的演示中,所述阈值是橙色条。 00类的分布是在红色(分类器的输出)和1类的分布是在蓝色(分类器的输出的相同,PROBA分布)。 它的工作原理与在一类或另一个是概率:如果一个样品具有输出的[0.34,0.66],然后0.25 1类的阈值将把他在1类即使0.66 PROBA较高。

你不带班的ROC曲线,但却使用的一类是probas工作。

我希望这回答了这个问题(对不起,如果不是,如果需要的话我会更精确)



文章来源: Understanding ROC curve