forked from XifengGuo/DEC-keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
200 lines (135 loc) · 6.25 KB
/
metrics.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import numpy as np
import sklearn.metrics
from sklearn.metrics import normalized_mutual_info_score, adjusted_rand_score
import seaborn as sns
import matplotlib
import time
matplotlib.use('Agg')
import pdb
import matplotlib.pyplot as plt
nmi = normalized_mutual_info_score
ari = adjusted_rand_score
print(plt.get_backend())
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
def acc(y_true, y_pred, dataset, to_log, m,n):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = y_true.astype(np.int64)
assert y_pred.size == y_true.size
timestr = time.strftime("%Y%m%d-%H%M%S")
#Saving the y_true and y_pred for later
y_true_orig = y_true
y_pred_orig = y_pred
#Metrics Excluding the 0th class
ind_nonzero = []
for i in range(len(y_true)):
if (y_true[i] > 0):
ind_nonzero.append(i)
# y_true_new = [y_true[i] for i in range(len(y_true)) if y_true[i] > 0]
# y_pred = [y_pred[i] for i in range(len(y_true)) if y_true[i] > 0]
# y_true = y_true_new
# # pdb.set_trace()
# y_true = np.array(y_true)
# y_pred = np.array(y_pred)
# # D = max(y_pred.max(), y_true.max()) + 1
# # D = max(y_pred.max(), y_true.max())
# # w = np.zeros((D, D), dtype=np.int64)
# # for i in range(y_pred.size):
# # w[y_pred[i], y_true[i]] += 1
# true_classes = np.unique(y_true)
# pred_classes = np.unique(y_pred)
# print("Without Zeros")
# print(true_classes)
# print(pred_classes)
# D = max(len(true_classes), len(pred_classes))
# w = np.zeros((D, D), dtype=np.int64)
# # pdb.set_trace()
# for p in range(len(true_classes)):
# for q in range(len(pred_classes)):
# indices_true = [k for (k, val) in enumerate(y_true) if (true_classes[p] == val)]
# indices_pred = [l for (l, val1) in enumerate(y_pred) if (pred_classes[q] == val1)]
# w[p,q] = len(intersection(indices_pred, indices_true))
# from sklearn.utils.linear_assignment_ import linear_assignment
# ind = linear_assignment(- w) #also try w.max - w
# print(ind)
# y_pred_new = np.zeros(y_pred.size)
# for i in range(y_pred.size):
# y_pred_new[i] = ind[y_pred[i],1 ]
true_classes = np.unique(y_true_orig)
pred_classes = np.unique(y_pred_orig)
# print("With Zeros")
D = max(len(true_classes), len(pred_classes))
w = np.zeros((D, D), dtype=np.int64)
# pdb.set_trace()
for p in range(len(true_classes)):
for q in range(len(pred_classes)):
indices_true = [k for (k, val) in enumerate(y_true_orig) if (true_classes[p] == val)]
indices_pred = [l for (l, val1) in enumerate(y_pred_orig) if (pred_classes[q] == val1)]
w[p,q] = len(intersection(indices_pred, indices_true))
from sklearn.utils.linear_assignment_ import linear_assignment
ind = linear_assignment(w.max()-w) #also try w.max - w
# print(ind)
y_pred_new = np.zeros(y_pred_orig.size)
for i in range(y_pred_orig.size):
y_pred_new[i] = ind[y_pred_orig[i],1 ]
if (to_log == 1):
y_true_new = [y_true[i] for i in range(len(y_true)) if y_true[i] > 0]
y_pred_no_zero = [y_pred_new[i] for i in range(len(y_true)) if y_true[i] > 0]
#Plot of confusion matrix
sns.set(font_scale=3)
confusion_matrix = sklearn.metrics.confusion_matrix(y_true_new, y_pred_no_zero)
timestr = time.strftime("%Y%m%d-%H%M%S")
np.save('confusion_mat/'+timestr+'_' + dataset+'_confusion_mat', confusion_matrix)
#Predicted Maps
fig = plt.figure(figsize=(16, 14))
sns.heatmap(confusion_matrix, annot=True, fmt="d", annot_kws={"size": 20});
plt.title("Confusion matrix", fontsize=30)
plt.ylabel('True label', fontsize=25)
plt.xlabel('Clustering label', fontsize=25)
fig.savefig("confusion_mat/{}_{}_confusion_mat_fig.png" .format(timestr,dataset))
# y_true1 = y_true_orig.reshape((m,n))
# cmap = plt.cm.jet
# norm = plt.Normalize(vmin=y_true1.min(), vmax=y_true1.max())
# image1 = cmap(norm(y_true1))
# plt.imsave("spatial_results/{}_{}_fig_gt.png" .format(timestr,dataset), image1)
y_pred1 = np.zeros(y_true_orig.size)
for i in range(len(ind_nonzero)):
y_pred1[ind_nonzero[i]] = y_pred_no_zero[i]
y_pred1 = y_pred1.reshape((m,n))
cmap = plt.cm.jet
norm = plt.Normalize(vmin=y_pred1.min(), vmax=y_pred1.max())
image = cmap(norm(y_pred1))
plt.imsave("spatial_results/{}_{}_fig_predicted.png" .format(timestr,dataset), image)
#Metrics Including the 0th class
acc = sum([w[i, j] for i, j in ind]) * 1.0 / y_pred_orig.size
if (to_log == 1):
#Plot of confusion matrix
sns.set(font_scale=3)
confusion_matrix = sklearn.metrics.confusion_matrix(y_true_orig, y_pred_new)
timestr = time.strftime("%Y%m%d-%H%M%S")
np.save('confusion_mat_with_zero/'+timestr+'_' + dataset+'_confusion_mat', confusion_matrix)
#Predicted Maps
fig = plt.figure(figsize=(16, 14))
sns.heatmap(confusion_matrix, annot=True, fmt="d", annot_kws={"size": 20});
plt.title("Confusion matrix", fontsize=30)
plt.ylabel('True label', fontsize=25)
plt.xlabel('Clustering label', fontsize=25)
fig.savefig("confusion_mat_with_zero/{}_{}_confusion_mat_fig.png" .format(timestr,dataset))
# y_true1 = y_true_orig.reshape((m,n))
# cmap = plt.cm.jet
# norm = plt.Normalize(vmin=y_true1.min(), vmax=y_true1.max())
# image1 = cmap(norm(y_true1))
# plt.imsave("spatial_results/{}_{}_fig_gt.png" .format(timestr,dataset), image1)
y_pred_new = y_pred_new.reshape((m,n))
cmap = plt.cm.jet
norm = plt.Normalize(vmin=y_pred_new.min(), vmax=y_pred_new.max())
image = cmap(norm(y_pred_new))
plt.imsave("spatial_results/{}_{}_{}_fig_predicted.png" .format(timestr,dataset,acc), image)
return sum([w[i, j] for i, j in ind]) * 1.0 / y_pred_new.size