对于二分类问题,我们经常通过ROC曲线及FPR95来判断分类器的好坏。这里提供两种方法。
- 一种是sklearn.metrics中的roc_curve包,可直接用于计算在不同阈值下,TPR和FPR对应的值,进而可以得出TPR=0.95时,FPR的值。
"""
label=1表示正样本,scores为预测概率,数值越大,越有可能是正样本
"""
from sklearn.metrics import roc_curve,auc
fpr,tpr,thresh = roc_curve(labels,scores)
另一种方法:
def ErrorRateAt95Recall1(labels, scores):
recall_point = 0.95
labels = np.asarray(labels)
scores = np.asarray(scores)
# Sort label-score tuples by the score in descending order.
indices = np.argsort(scores)[::-1] #降序排列
sorted_labels = labels[indices]
sorted_scores = scores[indices]
n_match = sum(sorted_labels)
n_thresh = recall_point * n_match
thresh_index = np.argmax(np.cumsum(sorted_labels) >= n_thresh)
FP = np.sum(sorted_labels[:thresh_index] == 0)
TN = np.sum(sorted_labels[thresh_index:] == 0)
return float(FP) / float(FP + TN)
错误写法:
"""
此代码为错误写法,存在一定的误差
"""
def ErrorRateAt95Recall(labels, scores):
recall_point = 0.95
# Sort label-score tuples by the score in descending order.
sorted_scores = zip(labels, scores)
sorted_scores.sort(key=operator.itemgetter(1), reverse=True)
#print(sorted_scores)
# Compute error rate
n_match = sum(1 for x in sorted_scores if x[0] == 1)
n_thresh = recall_point * n_match
tp = 0
count = 0
for label, score in sorted_scores:
#print(score)
count += 1
if label == 1:
tp += 1
if tp >= n_thresh:
break
return float(count - tp) / count
参考文献:
tpr fpr roc详解