gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:45:33 28 4
gpt4 key购买 nike

我对 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]))

最佳答案

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

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中找到它

关于python - 计算 AUC 曲线时如何创建阈值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51388711/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com