机器学习性能度量(2):错误接受率 (FAR), 错误拒绝率(FRR),EER计算方法,python实现

上一篇博文中讨论了两种常用的性能度量查准率(precision)查全率(recall,也叫召回率)对应的P-R图与真正例率(TPR),假正例率(FPR)对应的ROC图。详情请看https://blog.csdn.net/qq_18888869/article/details/84848689。今天介绍另一种常用的人的度量方法FAR,FRR,此方法在识别身份,人脸识别等方面运用较多。

1.概念

               FAR = \frac{FP}{FP+TN},错误接受率为不该接受的样本里你接受的比例

               FRR=\frac{FN}{TP + FN}.错误拒绝率为不该拒绝的样本里你拒绝的比例

从这里对比TPR与FPR,可以发现FRR = 1 - TPR,FPR=FAR。(1)

等错误率 (EER-Equal Error Rate) 
取一组0到1之间的等差数列(yuzh),分别作为识别模型的判别界限,既坐标x轴,画出FFR和FAR的坐标图,交点就是EER值(FAR与FRR相等的点)。

2.实现

首先根据前边的代码得到fpr,tpr,根据公式(1)计算相对应的far,frr,最后画出far-frr图。

fpr, tpr, thresholds = metrics.roc_curve(labels, scores, pos_label=1,drop_intermediate = False)

plt.figure()
plt.plot(1-tpr, fpr, color = 'green', marker = 'o',label = 'ROC')
plt.legend()
plt.xlim([0,1])
plt.ylim([0,1])
plt.xlabel('frr')
plt.ylabel('far')
plt.title('ROC')

 如图:

寻找eer: 

plt.figure()
plt.plot(1-tpr, thresholds,marker = '*',label = 'far')
plt.plot(fpr, thresholds, marker = 'o',label = 'fpr')
plt.legend()
plt.xlim([0,1])
plt.ylim([0,1])
plt.xlabel('thresh')
plt.ylabel('far/fpr')
plt.title(' find eer')

3.介绍一种求出EER近似值的方法 

 先介绍几个scipy库中的函数。

class scipy.interpolate.interp1d(xykind='linear'axis=-1copy=Truebounds_error=Nonefill_value=nanassume_sorted=False):插值函数

x,y是arrays 值近似函数y=f(x)。返回一个函数,调用方法使用插值寻找新点的值。

>>> import matplotlib.pyplot as plt
>>> from scipy import interpolate
>>> x = np.arange(0, 10)
>>> y = np.exp(-x/3.0)
>>> f = interpolate.interp1d(x, y)
>>> xnew = np.arange(0, 9, 0.1)
>>> ynew = f(xnew)   # use interpolation function returned by `interp1d`
>>> plt.plot(x, y, 'o', xnew, ynew, '-')
>>> plt.show()

 scipy.optimize.brentq(fabargs=()xtol=2e-12rtol=8.881784197001252e-16maxiter=100full_output=Falsedisp=True):作用是使用Brent方法在括号间隔内找到一个函数的根

f:函数,a,b间隔两端点。

>>> def f(x):
...     return (x**2 - 1)
>>> root = optimize.brentq(f, -2, 0)
>>> root
-1.0

 以下是求EER值的代码

def func(x):
    return 1. - x - interp1d(fpr, tpr)(x) # 1-roc-x,减去x是为了把x,y相等的点转化为y=0,然后通过求root的方法求出

x_dis = numpy.arange(0,1, 0.02)
y_dis = func(x_dis)
plt.figure()
plt.plot(x_dis,y_dis,label = 'f')
plt.legend()
plt.xlabel('x v')
plt.ylabel('y v')
root = brentq(f, 0,1)
print('root', root)

func的作用是将far,frr相等的地方转化为y=0,那么就可以用求根的方法来求出这个值。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值