通过ROC曲线计算TPR,手动与Scikit-Learn

发布于 2025-02-13 02:48:44 字数 909 浏览 1 评论 0原文

因此,给出Scikit-learn函数 roc_curve 我接收到这样的值的两个阵列,

fpr, tpr, thresholds = roc_curve(self.real_values_discrete, self.predictions_discrete)

我会收到这样的值:

>>> [0.         0.63888889 1.        ]
>>> [0.         0.54330709 1.        ]
>>> [2 1 0]

不过,使用公式和Scikit-learn函数手动计算FPR和TPR Confusion_matrix

confusion_matrix = confusion_matrix(self.real_values_discrete, self.predictions_discrete)
print(confusion_matrix)
_tp = confusion_matrix[0, 0]
_fn = confusion_matrix[0, 1]
_fp = confusion_matrix[1, 0]
_tn = confusion_matrix[1, 1]
_tpr = _tp / (_tp + _fn)
_fpr = _fp / (_tn + _fp)
print(_fpr)
print(_tpr)

我得到了这两个值,

>>> 0.4566929133858268
>>> 0.3611111111111111

我不明白为什么手工计算的值,以及上面的数组中的中间值不同。

这些值是不同的,还是我不了解某些东西/在某个地方有错误?

So, giving the scikit-learn function roc_curve two arrays of True and False values

fpr, tpr, thresholds = roc_curve(self.real_values_discrete, self.predictions_discrete)

I receive values like this:

>>> [0.         0.63888889 1.        ]
>>> [0.         0.54330709 1.        ]
>>> [2 1 0]

Though, calculating FPR and TPR by hand, using the formulas, and scikit-learn function confusion_matrix:

confusion_matrix = confusion_matrix(self.real_values_discrete, self.predictions_discrete)
print(confusion_matrix)
_tp = confusion_matrix[0, 0]
_fn = confusion_matrix[0, 1]
_fp = confusion_matrix[1, 0]
_tn = confusion_matrix[1, 1]
_tpr = _tp / (_tp + _fn)
_fpr = _fp / (_tn + _fp)
print(_fpr)
print(_tpr)

I get these two values

>>> 0.4566929133858268
>>> 0.3611111111111111

I don't understand why values calculated by hand, and the middle values from the arrays above differ.

Are these values meant to be different or I don't understand something/have an error somewhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2025-02-20 02:48:44

roc_curve()在得分上操作(例如prective> prective_proba()),而不是预测。正确使用的是,它应该为每个可能的分类阈值返回TPR和FPR值(唯一得分计数 + 1分)。

Confusion_Matrix()在预测上操作,因此假设默认阈值为0.5。

roc_curve() operates on scores (e.g. the result of predict_proba()), not predictions. Used properly, it should return the TPR and FPR values for every possible classification threshold (unique score count + 1 points).

confusion_matrix() operates on predictions, thus assuming a default threshold of 0.5.

迟月 2025-02-20 02:48:44

我认为,当将其值感应到输出时,错误是在Conduson矩阵的索引中。通常, scikit-learn 中的混淆矩阵具有以下结构:

 [[tn, fp],
  [fn, tp]]

鉴于此,应使用以下方式进行计算:

tn = confusion_matrix[0, 0]
fp = confusion_matrix[0, 1]
fn = confusion_matrix[1, 0]
tp = confusion_matrix[1, 1]

使用可重复的示例< /strong>

查看这个简单的示例,可以很好地理解该机制:

我应用了相同的代码,但是 纠正了索引 在其中分配值 混乱矩阵的输出

from sklearn import metrics

real_values_discrete = [0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
predictions_discrete = [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0]
    
fpr, tpr, thresholds = metrics.roc_curve(real_values_discrete, predictions_discrete)
print(fpr)
print(tpr)
print(thresholds)
    
confusion_matrix = metrics.confusion_matrix(real_values_discrete, predictions_discrete)
print(confusion_matrix)
    
tn = confusion_matrix[0, 0]
fp = confusion_matrix[0, 1]
fn = confusion_matrix[1, 0]
tp = confusion_matrix[1, 1]
    
tpr_manual = tp / (tp + fn)
fpr_manual = fp / (fp + tn)
    
print(fpr_manual)
print(tpr_manual)

这是结果:

[0.         0.42857143 1.        ]
[0.  0.5 1. ]
[inf  1.  0.]
[[4 3]
 [4 4]]
0.42857142857142855
0.5

我们清楚地看到,应用修改时,我向您展示了相同的结果(0.4285 AS FRP,而0.5则使用TPR使用两者功能)。

但是,当应用您的代码时,我们将获得结果:(

[0.         0.42857143 1.        ]
[0.  0.5 1. ]
[inf  1.  0.]
[[4 3]
 [4 4]]
0.5
0.5714285714285714

不幸的是,在这里我们有不同的结果)。

结论

在Scikit-learn中,混乱矩阵就像是

[[tn, fp],
 [fn, tp]]

时,请注意在为TP,FP,FN和TN分配值时。

应用这种修改的混淆矩阵索引,我敢肯定,您会使用roc_curve函数或Confusion_matrix函数获得相同的结果。

等待您的反馈!祝你好运!

I think the error is in the indices of the confuson matrix when affection its values to the outputs. Typically, the confusion matrix in scikit-learn has the following structure:

 [[tn, fp],
  [fn, tp]]

Given this, the calculation should be done using :

tn = confusion_matrix[0, 0]
fp = confusion_matrix[0, 1]
fn = confusion_matrix[1, 0]
tp = confusion_matrix[1, 1]

Explanation using a reproducible example

Look at this simple example to understand the mechanism well:

I applied the same code but corrected the indices in the part where assigning values from the confusion matrix's output:

from sklearn import metrics

real_values_discrete = [0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
predictions_discrete = [0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0]
    
fpr, tpr, thresholds = metrics.roc_curve(real_values_discrete, predictions_discrete)
print(fpr)
print(tpr)
print(thresholds)
    
confusion_matrix = metrics.confusion_matrix(real_values_discrete, predictions_discrete)
print(confusion_matrix)
    
tn = confusion_matrix[0, 0]
fp = confusion_matrix[0, 1]
fn = confusion_matrix[1, 0]
tp = confusion_matrix[1, 1]
    
tpr_manual = tp / (tp + fn)
fpr_manual = fp / (fp + tn)
    
print(fpr_manual)
print(tpr_manual)

Here are the results:

[0.         0.42857143 1.        ]
[0.  0.5 1. ]
[inf  1.  0.]
[[4 3]
 [4 4]]
0.42857142857142855
0.5

We see clearly that when applying the modification I showed you, you have the same results (0.4285 as frp and 0.5 as tpr using both functions).

But when applying your code, we will have as results:

[0.         0.42857143 1.        ]
[0.  0.5 1. ]
[inf  1.  0.]
[[4 3]
 [4 4]]
0.5
0.5714285714285714

(unfortunately, here we have different results from the two functions).

Conclusion :

In scikit-learn, the confusion matrix is like

[[tn, fp],
 [fn, tp]]

Consequetly, pay attention when assigning values to tp, fp, fn and tn.

Apply this modification of the confusion matrix indices, and I'm sure you'll get the same results using either the roc_curve function or the confusion_matrix function.

Waiting for your feedback! Good Luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文