-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_roc.py
65 lines (50 loc) · 1.8 KB
/
plot_roc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
from sklearn import svm, datasets
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
from numpy import interp
from sklearn.metrics import roc_auc_score
import pickle
import os
fig = plt.figure(dpi=600)
for _f, l in [("test_eval_normal.pkl", "GPND"),
("test_eval.pkl", "GPND+J"),
("test_eval_pz_only.pkl", "GPND-parallel"),
("test_eval_perror_only.pkl", "GPND-perpendicular"),
]:
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(10):
folder = "mnist_results_945/results_0_%d" % i
with open(os.path.join(folder, _f), "rb") as f:
y_scores, y_true = pickle.load(f)
fpr[i], tpr[i], _ = roc_curve(y_true, y_scores)
roc_auc[i] = auc(fpr[i], tpr[i])
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(10)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(10):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
mean_tpr /= 10
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
c = "macro"
lw = 2
plt.plot(fpr[c], tpr[c],
lw=lw, label='%s (area = %0.3f)' % (l, roc_auc[c]))
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0, 0.4])
plt.ylim([0.6, 1])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
fig.savefig("foo.pdf", bbox_inches='tight')