1. fpr tpr等五个指标记录
def calculate_fpr_tpr_tnr_f1score_accuracy(y_true, y_pred):
'''
y_true 和 y_pred均是ndarray类型。
'''
y_true = y_true.flatten()
y_pred = y_pred.flatten()
Tp = 0
Fp = 0
Tn = 0
Fn = 0
for label, pred in zip(y_true, y_pred):
if (label == 0) and (pred == 0):
Tp = Tp + 1
elif (label == 1) and (pred == 0):
Fp = Fp + 1
elif (label == 1) and (pred == 1):
Tn = Tn + 1
elif (label == 0) and (pred == 1):
Fn = Fn + 1
else:
print('something weird with labels')
return -1
# sys.exit()
# calculate precision, recall, accuracy, f1
# it's possible for division by zero in some of these cases, so do a try/except
try:
precision = Tp / (Tp + Fp)
except:
precision = 0
try:
recall = Tp / (Tp + Fn)
except:
recall = 0
try:
accuracy = (Tn + Tp) / (Tn + Tp + Fn + Fp)
except:
accuracy = 0
try:
f1Score = 2 * precision * recall / (precision + recall)
except:
f1Score = 0
try:
fpr = Fp / (Fp + Tn)
except:
fpr = 0
try:
tpr = Tp / (Tp + Fn)
except:
tpr = 0
try:
tnr = Tn / (Tn + Fp)
except:
tnr = 0
return (fpr, tpr, tnr, f1Score, accuracy)
(fpr, tpr, tnr, f1Score, accuracy) = \
calculate_fpr_tpr_tnr_f1score_accuracy(test_label, prediction)
2. ROC曲线画图
ef compute_metrics(probs, yprobs):
'''
Returns
fpr : Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i]
tpr : Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].
thresholds : Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1
auc : Area Under the ROC Curve metric
'''
# calculate AUC
auc = roc_auc_score(yprobs, probs)
# calculate roc curve
fpr, tpr, thresholds = roc_curve(yprobs, probs)
return fpr, tpr, thresholds, auc
def find_nearest(array, value):
idx = np.searchsorted(array, value, side="left")
if idx > 0 and (idx == len(array) or math.fabs(value - array[idx - 1]) < math.fabs(value - array[idx])):
return array[idx - 1], idx - 1
else:
return array[idx], idx
def draw_roc(fpr, tpr, thresholds):
# find threshold
targetfpr = 1e-3
_, idx = find_nearest(fpr, targetfpr)
threshold = thresholds[idx]
recall = tpr[idx]
# plot no skill
plt.plot([0, 1], [0, 1], linestyle='--')
# plot the roc curve for the model
plt.plot(fpr, tpr, marker='.')
plt.title('AUC: {0:.3f}\nSensitivity : {2:.1%} @FPR={1:.0e}\nThreshold={3})'.format(auc, targetfpr, recall,
abs(threshold)))
# show the plot
plt.show()