Skip to main content
 首页 » 编程设计

python之我如何重新采样 "roc_curve"(fpr,tpr)

2025年05月04日10dyllove98

我希望对“roc_curve”(sklearn) 输出重新采样。

当我在 Ipython 中绘制 fpr,tpr 时很好,但有时我想导出它(主要是为客户端),但很难理解,因为它不是线性的。

例如

fpr =[0,0.1,0.4,0.9,1] 
tpr =[0,0.3,0.4,0.5,1] 

我如何将 fpr 重新采样为每 5% ex 线性:

[0,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.,0.85,0.9,0.95,1] 

和tpr:

[0,0.15,0.3,0.3167,0.333,0.35,0.3667,0.383,0.4,0.41,0.42,0.43,0.44,0.45,0.46,0.47,0.48,0.49,0.5,0.75,1] 

我该如何继续?

请您参考如下方法:

我认为您正在寻找的是分段常数插值。

import numpy as np 
from scipy.interpolate import spline 
 
fpr =[0,0.1,0.4,0.9,1] 
tpr =[0,0.3,0.4,0.5,1] 
 
n = 20 
x_interp = np.linspace(0,1,n+1) 
y_interp = spline(fpr, tpr, x_interp, order=0) 

x_interp 是 fpr 值

[ 0.    0.05  0.1   0.15  0.2   0.25  0.3   0.35  0.4   0.45  0.5   0.55 
  0.6   0.65  0.7   0.75  0.8   0.85  0.9   0.95  1.  ] 

y_interp 是对应的tpr 值

[ 0.   0.   0.3  0.3  0.3  0.3  0.3  0.3  0.4  0.4  0.4  0.4  0.4  0.4  0.4 
  0.4  0.4  0.4  0.5  0.5  0.5]