了解ROC曲线

问题描述 投票:0回答:2
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

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

假阳性数: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:array,shape = [> 2]

增加假阳性率,使得元素i是具有得分> =阈值[i]的假阳性预测率。

tpr:array,shape = [> 2]

增加真阳性率,使得元素i是具有得分> =阈值[i]的真实阳性预测率。

阈值:数组,形状= [n_thresholds]

降低用于计算fpr和tpr的决策函数的阈值。阈值[0]表示没有预测的实例,并且任意设置为max(y_score)+ 1。

这与我的误报率手动计算有何不同?如何设置阈值?这里提供了一些关于阈值的模式信息:https://datascience.stackexchange.com/questions/806/advantages-of-auc-vs-standard-accuracy但我对它如何适应这种实现感到困惑?

scikit-learn roc false-positive
2个回答
1
投票

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

真正的积极因素:生病的人正确认定为生病

其次,每个模型都有一些基于正类概率(通常为0.5)的阈值。

因此,如果阈值为0.1,则具有大于0.1的概率的所有样本将被归类为正。预测样本的概率是固定的,阈值将是变化的。

roc_curve中,scikit-learn增加了以下阈值:

 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

1
投票

enter image description here

在上面的演示中,阈值是橙色条。类00的分布为红色(分类器的输出),类1的分布为蓝色(相同,分类器输出的概率分布)。它适用于在一个类或另一个类中的概率:如果一个样本具有[0.34,0.66]的输出,那么即使0.66的概率更高,类1的阈值0.25也将使他进入第1类。

您不使用类来处理ROC曲线,而是使用类中的概要。

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

© www.soinside.com 2019 - 2024. All rights reserved.