多阈值混淆矩阵

2024-06-17 12:12:23 发布

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

我正在尝试(高效地)为多个阈值运行sklearn.metrics.confusion_matrix。我需要这样做,这样我就可以告诉客户,在任何给定的百分比的挑战中,人们应该期待什么样的表现。在

目前,我在一个循环中执行,超过所有可能的阈值,但这是缓慢和低效的。有什么方法可以在一行代码中完成,或者类似的操作?在

threshold_list = (np.linspace(1, 0, 1001)).tolist()
for threshold in threshold_list:
    df.loc[df['score'] >= threshold,'prediction'] = '1'
    arr = confusion_matrix(df['true'].astype('int16').values, df['prediction'].astype('int16').values)
    ....
    ....

Tags: 方法dfthreshold客户阈值sklearnmatrixlist
1条回答
网友
1楼 · 发布于 2024-06-17 12:12:23

如果TPr和FPr对你来说足够了。您可以执行以下操作:

y_true=[1,0,0,1,1,0,0]
y_pred=[0.67, 0.48, 0.27, 0.52, 0.63, 0.45, 0.53]
fpr, tpr, thresholds = roc_curve(y_true, y_pred)
res = pd.DataFrame({'FPR': fpr, 'TPR': tpr, 'Threshold': thresholds})
res[['TPR', 'FPR', 'Threshold']]

输出:

^{pr2}$

相关问题 更多 >