首页 > 解决方案 > 测量系统 Roc 的性能

问题描述

我对机器学习和 python 很陌生。任何帮助,将不胜感激。

通常在 Matlab 中,很容易绘制它。我想绘制 roc 曲线来评估人脸识别系统的性能,我计算两个图像之间的欧几里得距离和余弦相似度,我想将其两个参数的计算应用于数据库(测试列车)。如何在数据库图像上绘制 roc 曲线

以及如何衡量自动编码器的性能?

此代码不起作用:

predictions_prob = your_model.predict_proba(x_test)
false_positive_rate, recall, thresholds = roc_curve(y_test, predictions_prob[:,1])
roc_auc = auc(false_positive_rate, recall)
plt.plot(false_positive_rate, recall, 'g', label = 'AUC %s = %0.2f' % ('model name', roc_auc))
plt.plot([0,1], [0,1], 'r--')
plt.legend(loc = 'lower right')
plt.ylabel('Recall')
plt.xlabel('Fall-out')
plt.title('ROC Curve')

这是预训练模型的权重

所以现在我有两个数组 y_true 如果两个面相似'1'或者不是'0'

y_true [0 1 1 1 0 1 0 1 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]

y_score 数组表示y_score

[0.43031937 0.09115553 0.00650781 0.02242869 0.38608587 0.09407699
 0.40521139 0.08062053 0.37445426 0.73493853 0.7103999  0.72978038
 0.66644344 0.63952136 0.61384821 0.58388719 0.64563826 0.7302449
 0.50854671 0.74351138 0.74457312 0.86807218 0.83802608 0.74165669
 0.74858481 0.76547028 0.73587325 0.78119443 0.59438175 0.74271324
 0.65287331 0.55672997 0.6840947  0.86698833 0.69892132 0.9039218
 0.73688647 0.88281097 0.65161654 0.6082072  0.60127196 0.59740826
 0.63763261 0.60536379 0.642178   0.61151108 0.62726742 0.61947313
 0.67193428 0.7865534  0.65491107 0.6640633  0.68394253 0.63343072
 0.79708609 0.78625438 0.70690271 0.75213048 0.76652744 0.85628764
 0.82893997 0.75122409 0.76786727 0.7644964  0.75824204 0.78366616
 0.65271395 0.75293976 0.72236988 0.56250972 0.72455084 0.9160955
 0.74614334 0.94117467 0.75922103 0.91618422]

当我运行代码时,我得到了这个情节:

阴谋

我应该改变分数标签我失去了任何帮助将不胜感激。

我不知道为什么我在 tpr 和 fpr 和 threshold 中只得到 4 个元素

fpr [0. 0. 0. 1.]
tpr [0.  0.2 1.  1. ]
thresholds [1.99308544 0.99308544 0.90004301 0.        ]

标签: pythonkerasscikit-learnface-recognitionroc

解决方案


假设y_test是一个包含 0 和 1 的 numpy 数组,其中 0 表示两个面不相同(负),1 表示两个面相同(正)。

还假设您verifyFace在预测中使用。假设它的输出是pred,其中包含每对之间的距离。

根据定义,低于阈值的两个面将被视为正面。这与典型的二元分类任务正好相反。

所以这里有一个解决方法:

from sklearn.metrics import roc_curve, auc
import numpy as np
import matplotlib.pyplot as plt

n_samples = 1000
pred = np.random.randn(n_samples)
y_test = np.random.randint(2, size=(n_samples,))

max_dist = max(pred)
pred = np.array([1-e/max_dist for e in pred])
fpr, tpr, thresholds = roc_curve(y_test, pred)
roc_auc = auc(fpr, tpr)
plt.figure()
lw = 2
plt.plot(fpr, tpr, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % roc_auc)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

关键概念是转换pred,使其看起来像一个confidence.

参考: 如何在使用 Scikit-Learn 创建 ROC 曲线时使用预测分数

接收器工作特性 (ROC)


推荐阅读