了解 ROC 曲线

Understanding ROC curve

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) 的 return 值:

fpr : array, shape = [>2]

Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i].

tpr : array, shape = [>2]

Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].

thresholds : array, shape = [n_thresholds]

Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1.

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

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

您不使用 classes 在 ROC 曲线上工作,但有可能处于 class。

我希望它能回答这个问题(如果没有,抱歉,如果需要我会更准确)

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

True positive: Sick people correctly identified as sick

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

因此,如果阈值为 0.1,则所有概率大于 0.1 的样本都将 class 确定为阳性。预测样本的概率是固定的,阈值会变化。

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