计算AUC曲线时如何创建阈值?

2024-04-23 10:16:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道在python的scikit-learn中如何生成阈值。对于下面的示例,将生成四个阈值,其中当我将pred中的第三个值更改为0.6时,阈值的数量将降至3。有人能解释为什么会这样吗?你知道吗

#Example 1
import numpy as np
from sklearn import metrics
y = np.array([0, 0, 1, 1])
pred = np.array([0.1, 0.4, 0.3, 0.8])  #Please note the thord value here is `0.3`
fpr, tpr, thresholds = metrics.roc_curve(y, pred, pos_label=1)
fpr, tpr, thresholds 


(array([0. , 0.5, 0.5, 1. ]),
 array([0.5, 0.5, 1. , 1. ]),
 array([0.8, 0.4, 0.3, 0.1]))

#Example 2
y = np.array([0, 0, 1, 1])
pred = np.array([0.1, 0.4, 0.6, 0.8])
fpr, tpr, thresholds = metrics.roc_curve(y, pred, pos_label=1)
fpr, tpr, thresholds 

(array([0., 0., 1.]), 
array([0.5, 1. , 1. ]), 
array([0.8, 0.6, 0.1]))

Tags: posimportexamplenp阈值scikitarraylabel
1条回答
网友
1楼 · 发布于 2024-04-23 10:16:03

有一个默认为True的关键字参数drop_intermediate

drop_intermediate : boolean, optional (default=True) Whether to drop some suboptimal thresholds which would not appear on a plotted ROC curve. This is useful in order to create lighter ROC curves. New in version 0.17: parameter drop_intermediate.

因此,将代码更改为:

fpr, tpr, thresholds = metrics.roc_curve(y, pred, pos_label=1, drop_intermediate=False)
fpr, tpr, thresholds 

给予

(array([0. , 0. , 0.5, 1. ]),
 array([0.5, 1. , 1. , 1. ]),
 array([0.8, 0.6, 0.4, 0.1]))

你可以在documentation中找到这个

相关问题 更多 >