1

** 大家好!我是一名正在从事最后一年项目的本科生。这是我自己搭建的离线签名验证系统。这是我查找 FAR 和 FRR 的代码。**

def compute_accuracy_roc(predictions, labels, plot_far_frr =False):
    '''
    Compute ROC accuracy with a range of thresholds on distances.
    Plot FAR-FRR and P-R curves to measure performance on input set
    '''
    dmax = np.max(predictions)
    dmin = np.min(predictions)
    nsame = np.sum(labels == 1) #similar
    ndiff = np.sum(labels == 0) #different
    step = 0.00001
    max_acc = 0
    best_thresh = -1
    frr_plot = []
    far_plot = []
    pr_plot = []
    re_plot = []
    ds = []
    for d in np.arange(dmin, dmax+step, step):
        idx1 = predictions.ravel() <= d #guessed genuine
        idx2 = predictions.ravel() > d #guessed forged
        tp = float(np.sum(labels[idx1] == 1))
        tn = float(np.sum(labels[idx2] == 0))
        fp = float(np.sum(labels[idx1] == 0))
        fn = float(np.sum(labels[idx2] == 1))
#         print(tp, tn, fp, fn)
        tpr = float(np.sum(labels[idx1] == 1)) / nsame       
        tnr = float(np.sum(labels[idx2] == 0)) / ndiff
        
        
        acc = 0.5 * (tpr + tnr)
        pr = tp / (tp + fp)
        re = tp / (tp + fn)
#       print ('ROC', acc, tpr, tnr)
       
        if (acc > max_acc):
            max_acc, best_thresh = acc, d
        
        #if (fp+tn) != 0.0 and (fn+tp) != 0.0:
        far = fp / (fp + tn)
        frr = fn / (fn + tp)
        frr_plot.append(frr)
        pr_plot.append(pr)
        re_plot.append(re)
        far_plot.append(far)
        ds.append(d)
            
    
    if plot_far_frr:
        fig = plt.figure(figsize = (10,10))
        ax = fig.add_subplot(121)
        ax.plot(ds, far_plot, color = 'red')
        ax.plot(ds, frr_plot, color = 'blue')
        ax.set_title('Error rate')
        ax.legend(['FAR', 'FRR'])
        ax.set(xlabel = 'Thresholds', ylabel = 'Error rate')
        
        ax1 = fig.add_subplot(122)
        ax1.plot(ds, pr_plot, color = 'green')
        ax1.plot(ds, re_plot, color = 'magenta')
        ax1.set_title('P-R curve')
        ax1.legend(['Precision', 'Recall'])
        ax.set(xlabel = 'Thresholds', ylabel = 'Error rate')
        
        plt.show()
    return max_acc, best_thresh


** 在这里我调用我对 FAR 和 FRR 的定义来绘制图表**

threshold = []
    for i in range(100):
        num = 0

        for x in far:
                if x>i:
                        num+=1
        #print(i,num)
        
        threshold = np.append(threshold, i)
        far = np.append(far, num)

far = np.array(far)
print('FAR: ',far)
print('-----------------------------------------------------------')

threshold = []
    for i in range(100):
        num = 0

        for x in frr:
                if x<i:
                        num+=1
        #print(i,num)
        threshold = np.append(threshold, i)
        frr = np.append(frr, num)


frr = np.array(frr)
print('FRR: ',frr)
print('-----------------------------------------------------------')

**这些是结果**

FAR:  [[[[[[[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
        0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
        0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
        0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
        0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]]]]]]]
-----------------------------------------------------------
FRR:  [[[[[[[[[  0   0   2   2   4   4   6   6   8   8  10  10  12  12
          14  14  16  16  18  18  20  20  22  22  24  24  26  26
          28  28  30  30  32  32  34  34  36  36  38  38  40  40
          42  42  44  44  46  46  48  48  50  50  52  52  54  54
          56  56  58  58  60  60  62  62  64  64  66  66  68  68
          70  70  72  72  74  74  76  76  78  78  80  80  82  82
          84  84  86  86  88  88  90  90  92  92  94  94  96  96
          98  98 100]]]]]]]]]
-----------------------------------------------------------


当我绘制它时,我得到一个错误

plt.plot(threshold,frr,'--b')
plt.xlabel('threshold')
plt.title('FRR')
plt.axis([0, 100, 0, 100])
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-129-4e0ccdb5acbe> in <module>
----> 1 plt.plot(threshold,frr,'--b')
      2 plt.xlabel('threshold')
      3 plt.title('FRR')
      4 plt.axis([0, 100, 0, 100])
      5 plt.show()

~\Anaconda3\envs\Surf\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2794     return gca().plot(
   2795         *args, scalex=scalex, scaley=scaley, **({"data": data} if data
-> 2796         is not None else {}), **kwargs)
   2797 
   2798 

~\Anaconda3\envs\Surf\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1663         """
   1664         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
-> 1665         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1666         for line in lines:
   1667             self.add_line(line)

~\Anaconda3\envs\Surf\lib\site-packages\matplotlib\axes\_base.py in __call__(self, *args, **kwargs)
    223                 this += args[0],
    224                 args = args[1:]
--> 225             yield from self._plot_args(this, kwargs)
    226 
    227     def get_next_color(self):

~\Anaconda3\envs\Surf\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    389             x, y = index_of(tup[-1])
    390 
--> 391         x, y = self._xy_from_xy(x, y)
    392 
    393         if self.command == 'plot':

~\Anaconda3\envs\Surf\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
    271         if x.ndim > 2 or y.ndim > 2:
    272             raise ValueError("x and y can be no greater than 2-D, but have "
--> 273                              "shapes {} and {}".format(x.shape, y.shape))
    274 
    275         if x.ndim == 1:

ValueError: x and y can be no greater than 2-D, but have shapes (1,) and (1, 1, 1, 1, 1, 1, 1, 1, 101)


我需要帮助。我对这些东西很陌生。我哪里错了?

4

0 回答 0