|
| 1 | +import numpy as np |
| 2 | +from sklearn.metrics import roc_auc_score |
| 3 | +import pickle |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +def get_f1(true_positive, false_positive, false_negative): |
| 8 | + if true_positive == 0: |
| 9 | + return 0.0 |
| 10 | + precision = true_positive / (true_positive + false_positive) |
| 11 | + recall = true_positive / (true_positive + false_negative) |
| 12 | + return 2.0 * precision * recall / (precision + recall) |
| 13 | + |
| 14 | + |
| 15 | +def evaluate(logger, percentage_of_outliers, inliner_classes, prediction, threshold, gt_inlier): |
| 16 | + y = np.greater(prediction, threshold) |
| 17 | + |
| 18 | + gt_outlier = np.logical_not(gt_inlier) |
| 19 | + |
| 20 | + true_positive = np.sum(np.logical_and(y, gt_inlier)) |
| 21 | + true_negative = np.sum(np.logical_and(np.logical_not(y), gt_outlier)) |
| 22 | + false_positive = np.sum(np.logical_and(y, gt_outlier)) |
| 23 | + false_negative = np.sum(np.logical_and(np.logical_not(y), gt_inlier)) |
| 24 | + total_count = true_positive + true_negative + false_positive + false_negative |
| 25 | + |
| 26 | + accuracy = 100 * (true_positive + true_negative) / total_count |
| 27 | + |
| 28 | + y_true = gt_inlier |
| 29 | + y_scores = prediction |
| 30 | + |
| 31 | + try: |
| 32 | + auc = roc_auc_score(y_true, y_scores) |
| 33 | + except: |
| 34 | + auc = 0 |
| 35 | + |
| 36 | + logger.info("Percentage %f" % percentage_of_outliers) |
| 37 | + logger.info("Accuracy %f" % accuracy) |
| 38 | + f1 = get_f1(true_positive, false_positive, false_negative) |
| 39 | + logger.info("F1 %f" % get_f1(true_positive, false_positive, false_negative)) |
| 40 | + logger.info("AUC %f" % auc) |
| 41 | + |
| 42 | + # return dict(auc=auc, f1=f1) |
| 43 | + |
| 44 | + # inliers |
| 45 | + X1 = [x[1] for x in zip(gt_inlier, prediction) if x[0]] |
| 46 | + |
| 47 | + # outliers |
| 48 | + Y1 = [x[1] for x in zip(gt_inlier, prediction) if not x[0]] |
| 49 | + |
| 50 | + minP = min(prediction) - 1 |
| 51 | + maxP = max(prediction) + 1 |
| 52 | + |
| 53 | + ################################################################## |
| 54 | + # FPR at TPR 95 |
| 55 | + ################################################################## |
| 56 | + fpr95 = 0.0 |
| 57 | + clothest_tpr = 1.0 |
| 58 | + dist_tpr = 1.0 |
| 59 | + for threshold in np.arange(minP, maxP, 0.2): |
| 60 | + tpr = np.sum(np.greater_equal(X1, threshold)) / np.float(len(X1)) |
| 61 | + fpr = np.sum(np.greater_equal(Y1, threshold)) / np.float(len(Y1)) |
| 62 | + if abs(tpr - 0.95) < dist_tpr: |
| 63 | + dist_tpr = abs(tpr - 0.95) |
| 64 | + clothest_tpr = tpr |
| 65 | + fpr95 = fpr |
| 66 | + |
| 67 | + logger.info("tpr: %f" % clothest_tpr) |
| 68 | + logger.info("fpr95: %f" % fpr95) |
| 69 | + |
| 70 | + ################################################################## |
| 71 | + # Detection error |
| 72 | + ################################################################## |
| 73 | + error = 1.0 |
| 74 | + for threshold in np.arange(minP, maxP, 0.2): |
| 75 | + tpr = np.sum(np.less(X1, threshold)) / np.float(len(X1)) |
| 76 | + fpr = np.sum(np.greater_equal(Y1, threshold)) / np.float(len(Y1)) |
| 77 | + error = np.minimum(error, (tpr + fpr) / 2.0) |
| 78 | + |
| 79 | + logger.info("Detection error: %f" % error) |
| 80 | + |
| 81 | + ################################################################## |
| 82 | + # AUPR IN |
| 83 | + ################################################################## |
| 84 | + auprin = 0.0 |
| 85 | + recallTemp = 1.0 |
| 86 | + for threshold in np.arange(minP, maxP, 0.2): |
| 87 | + tp = np.sum(np.greater_equal(X1, threshold)) |
| 88 | + fp = np.sum(np.greater_equal(Y1, threshold)) |
| 89 | + if tp + fp == 0: |
| 90 | + continue |
| 91 | + precision = tp / (tp + fp) |
| 92 | + recall = tp / np.float(len(X1)) |
| 93 | + auprin += (recallTemp - recall) * precision |
| 94 | + recallTemp = recall |
| 95 | + auprin += recall * precision |
| 96 | + |
| 97 | + logger.info("auprin: %f" % auprin) |
| 98 | + |
| 99 | + ################################################################## |
| 100 | + # AUPR OUT |
| 101 | + ################################################################## |
| 102 | + minP, maxP = -maxP, -minP |
| 103 | + X1 = [-x for x in X1] |
| 104 | + Y1 = [-x for x in Y1] |
| 105 | + auprout = 0.0 |
| 106 | + recallTemp = 1.0 |
| 107 | + for threshold in np.arange(minP, maxP, 0.2): |
| 108 | + tp = np.sum(np.greater_equal(Y1, threshold)) |
| 109 | + fp = np.sum(np.greater_equal(X1, threshold)) |
| 110 | + if tp + fp == 0: |
| 111 | + continue |
| 112 | + precision = tp / (tp + fp) |
| 113 | + recall = tp / np.float(len(Y1)) |
| 114 | + auprout += (recallTemp - recall) * precision |
| 115 | + recallTemp = recall |
| 116 | + auprout += recall * precision |
| 117 | + |
| 118 | + logger.info("auprout: %f" % auprout) |
| 119 | + |
| 120 | + with open(os.path.join("results.txt"), "a") as file: |
| 121 | + file.write( |
| 122 | + "Class: %s\n Percentage: %d\n" |
| 123 | + "Error: %f\n F1: %f\n AUC: %f\nfpr95: %f" |
| 124 | + "\nDetection: %f\nauprin: %f\nauprout: %f\n\n" % |
| 125 | + ("_".join([str(x) for x in inliner_classes]), percentage_of_outliers, error, f1, auc, fpr95, error, auprin, auprout)) |
| 126 | + |
| 127 | + return dict(auc=auc, f1=f1, fpr95=fpr95, error=error, auprin=auprin, auprout=auprout) |
| 128 | + # return auc, f1, fpr95, error, auprin, auprout |
0 commit comments