-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseClassifiers.py
389 lines (330 loc) · 14.8 KB
/
BaseClassifiers.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
class BaseClassifiers(object):
"""
Class Wrapper to get an overview of the predictions of base classifiers.
Args:
@predictions: list of lists. Each sublist contains the predictions
of a classifier
@names: list of strings. Each string is the name of the classifier.
@true: list of labels. Each label is the truth label
@vis_flag: boolean flag. If True will also plot bar charts depicting
the information printed. Defaults to False for readability.
"""
def __init__(self, predictions, names, true, vis_flag=False):
N = len(true)
labels = set(true)
if len(predictions) != len(names):
raise AttributeError('Number of classifiers is different than number \
of names. %d != %d.' % (len(predictions), len(names)))
for i, predict in enumerate(predictions):
if len(predict) != N:
raise AttributeError('Number of predictions of classifier %s is different\
then the number of true labels. %d != %d .\
' % (names[i], len(predict), N))
if labels.isdisjoint(set(predict)):
raise AttributeError('Label in predictions of %s not in truth set. \
' % (names[i]))
self.predictions = predictions
self.names = names
self.true = true
self.vis_flag = vis_flag
def get_comparison_report(self):
_, fig = comparison_report(self.predictions, self.names, self.true, self.vis_flag)
if self.vis_flag:
pass
#import plotly.plotly as py
#py.iplot(fig)
return
def get_classification_report(self):
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CLASSIFICATION RESULTS PER MODEL~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
for i, predict in enumerate(self.predictions):
print "============================ Model: %s ==================================+" % str(self.names[i])
acc = accuracy_score(self.true, predict)
conf = confusion_matrix(self.true, predict, labels=sorted(list(set(self.true))))
rep = classification_report(self.true, predict, target_names=sorted(list(set(self.true))))
print('Accuracy : {}'.format(acc))
print('Confusion matrix :\n {}'.format(conf))
print('Classification report :\n {}'.format(rep))
print "============================================================================"
return
def get_mistakes_clustering(self):
mistakes_clustering(self.predictions, self.names, self.true)
return
def mistakes_clustering(predictions, names, true):
pass
def get_per_class_accuracy(self):
print "Per Class Accuracy of the models"
df = per_class_accuracy(self.predictions, self.names, self.true)
return df
def get_per_class_f1(self):
print "Per Class Accuracy of the models"
df = per_class_f1(self.predictions, self.names, self.true)
return df
def help(self):
"""Just a helper function to print the class docstring."""
return self.__doc__
def per_class_accuracy(predictions, names, true):
from pandas import DataFrame
from numpy import array, hstack, zeros
Num_models = len(predictions)
labels = sorted(list(set(true)))
Num_samples = len(labels)
tmp = array(predictions[0]).reshape(-1,1)
for i in xrange(1, len(predictions)):
#print tmp
#print tmp.shape
#print array(predictions[i].reshape(-1,1))
#print array(predictions[i].reshape(-1,1)).shape
tmp = hstack((tmp, array(predictions[i]).reshape(-1,1)))
tmp = hstack((tmp, array(true).reshape(-1,1)))
df = DataFrame(tmp, columns=names + ['true'])
counter = zeros([Num_models, len(labels)])
for i, name in enumerate(names):
df1 = df[df[name] == df['true']]
for j, label in enumerate(labels):
#print df1[df1['true'] == label].shape[0]
#print float(len([lab_ for lab_ in labels if lab_ == label]))
counter[i, j] = 100*df1[df1['true'] == label].shape[0] / float(len([lab_ for lab_ in true if lab_ == label]))
final_df = DataFrame(counter, columns = labels, index = names)
print final_df
return final_df
def per_class_f1(predictions, names, true):
from pandas import DataFrame
from numpy import array, hstack, zeros, vstack
from sklearn.metrics import f1_score
labels = sorted(list(set(true)))
counter = f1_score(predictions[0], true, average=None, labels=labels).reshape(1,-1)
print counter.shape
for i in xrange(1, len(predictions)):
counter = vstack((counter,f1_score(predictions[i], true, average=None, labels=labels).reshape(1,-1)))
print counter.shape
final_df = DataFrame(counter, columns = labels, index = names)
print final_df
return final_df
def comparison_report(predictions, names, true, print_flag=False):
import numpy, pandas
import matplotlib.pyplot as plt
fig1 = None
N = len(true)
L = len(predictions)
num__pairs = L*(L-1)/2
correct = numpy.zeros([L,N])
correct_per = []
for i in xrange(L):
correct[i, :] = numpy.core.defchararray.equal(predictions[i], true)
correct_per.append(numpy.sum(correct[i,:])/float(N))
#print correct.shape
#print
df = pandas.DataFrame(correct.T, columns = names)
#print df.head(20)
# Classifier Performance
print 'Base Accuracies'
acc = []
acc_s = ''
for name in names:
acc.append(df.sum(axis=0)[name]*100/float(N))
acc_s += '%s : %0.2f || ' % (name, acc[-1])
acc_s = acc_s[:-4]
print acc_s
if print_flag:
fig, ax = plt.subplots()
s = plt.bar([i for i in xrange(L)], acc, align='center', alpha=0.4)
plt.xticks([i for i in xrange(L)], names)
plt.ylabel('% Accuracy')
plt.title('Base Classifiers')
autolabel(s.patches, ax)
plt.show()
# Classifier versus the others
count_others = numpy.zeros([L,L])
for i, name1 in enumerate(names):
tmp = df[df.sum(axis=1)==1]
if not(tmp[tmp[name1]>0].empty):
if tmp[tmp[name1]>0].shape[0] == 0:
count_others[0, i] = 1*100/float(N)
else:
count_others[0, i] = tmp[tmp[name1]>0].shape[0]*100/float(N)
pairs_titles = [[] for i in xrange(0,L)]
pairs_titles[0] = names
#print pairs_titles
for i in xrange(0, L):
name1 = names[i]
#print name1
cyclic_names = [name1]+names[names.index(name1)+1:]+names[:names.index(name1)]
tmp = df[df.sum(axis=1)==2]
tmp2 = tmp[tmp[name1]>0]
#tmp2 = df[df[name1]>0]
#N_class = df[df[name1]>0].shape[0]
N_class = tmp2.shape[0]
#print tmp2
#print df[df[name1]>0]
for j in xrange(1, L):
name2 = cyclic_names[j]
#print name2
if not(tmp2[tmp2[name2]>0].empty):
if tmp2[tmp2[name2]>0].shape[0] == 0:
count_others[j, i] = 1*100/float(N_class)
else:
count_others[j, i] = tmp2[tmp2[name2]>0].shape[0]*100/float(N_class)
#if name1 == 'soac':
#print name2
#print tmp2[tmp2[name2]>0]
pairs_titles[j].append(name1+ '-'+name2)
#print count_others[:,i]
#print count_others
count_pd = pandas.DataFrame(count_others.T, index=names, columns=['Only this Model']+ [' %d-model aggree' % i for i in xrange(1,L)])
print 'Models Correct Aggrement Percentages'
print count_pd.astype('float').to_string(float_format= lambda x: '%0.2f'%(x))
if print_flag:
import plotly.graph_objs as go
from matplotlib.pyplot import cm
top_labels = ['Only this Model']+ [' %d-model aggree' % i for i in xrange(1,L)]
colors = list(iter(cm.rainbow(numpy.linspace(0, 1, L))))
x_data = count_others.T.tolist()[::-1]
#print x_data
y_data = ['The course was effectively<br>organized',
'The course developed my<br>abilities and skills ' +
'for<br>the subject', 'The course developed ' +
'my<br>ability to think critically about<br>the subject',
'I would recommend this<br>course to a friend']
y_data = names[::-1]
#print y_data
#print top_labels
traces = []
for i in range(0, len(x_data[0])):
for xd, yd in zip(x_data, y_data):
traces.append(go.Bar(
x=xd[i],
y=yd,
orientation='h',
marker=dict(
color=colors[i],
line=dict(
color='rgb(248, 248, 249)',
width=1)
)
))
layout = go.Layout(
xaxis=dict(
showgrid=False,
showline=False,
showticklabels=False,
zeroline=False,
domain=[0.15, 1]
),
yaxis=dict(
showgrid=False,
showline=False,
showticklabels=False,
zeroline=False,
),
barmode='stack',
paper_bgcolor='rgb(248, 248, 255)',
plot_bgcolor='rgb(248, 248, 255)',
margin=dict(
l=120,
r=10,
t=140,
b=80
),
showlegend=False,
)
annotations = []
for yd, xd in zip(y_data, x_data):
# labeling the y-axis
annotations.append(dict(xref='paper', yref='y',
x=0.14, y=yd,
xanchor='right',
text=str(yd),
font=dict(family='Arial', size=14,
color='rgb(67, 67, 67)'),
showarrow=False, align='right'))
# labeling the first percentage of each bar (x_axis)
annotations.append(dict(xref='x', yref='y',
x=xd[0] / 2, y=yd,
text='%0.2f'%xd[0],
font=dict(family='Arial', size=14,
color='rgb(248, 248, 255)'),
showarrow=False))
# labeling the first Likert scale (on the top)
if yd == y_data[-1]:
annotations.append(dict(xref='x', yref='paper',
x=xd[0] / 2, y=1.1,
text=top_labels[0],
font=dict(family='Arial', size=14,
color='rgb(67, 67, 67)'),
showarrow=False))
space = xd[0]
for i in range(1, len(xd)):
# labeling the rest of percentages for each bar (x_axis)
annotations.append(dict(xref='x', yref='y',
x=space + (xd[i]/2), y=yd,
text='%0.2f ' % xd[i],
font=dict(family='Arial', size=14,
color='rgb(248, 248, 255)'),
showarrow=False))
# labeling the Likert scale
if yd == y_data[-1]:
annotations.append(dict(xref='x', yref='paper',
x=space + (xd[i]/2), y=1.1,
text=top_labels[i],
font=dict(family='Arial', size=14,
color='rgb(67, 67, 67)'),
showarrow=False))
space += xd[i]
layout['annotations'] = annotations
#fig1 = go.Figure(data=traces, layout=layout)
#py.iplot(fig, filename='Results.png')
#py.image.save_as(fig, filename='Results.png')
#from IPython.display import Image
#Image('Results.png')
#py.image.ishow(fig)
# Error Distributions
all_cor = numpy.sum(df[df.apply(lambda x: min(x) == max(x), 1)][names[0]]==1)
all_wro = numpy.sum(df[df.apply(lambda x: min(x) == max(x), 1)][names[0]]==0)
disag = N - all_cor - all_wro
print 'Predictions Distributions'
print 'All correct : %0.2f || Some correct : %0.2f || All wrong: %0.2f ' % \
(100*all_cor/float(N), 100*disag/float(N), 100*all_wro/float(N) )
if print_flag:
fig, ax = plt.subplots()
s = plt.bar([1,2,3], [100*all_cor/float(N), 100*disag/float(N), 100*all_wro/float(N)], align='center', alpha=0.4)
plt.xticks([1,2,3], ['All correct', 'Some correct', 'All wrong'])
plt.ylabel('% Percentage of test dataset')
plt.title('Ensemble Decisions')
autolabel(s.patches, ax)
plt.show()
# Wrong Instances
df_not_correct = df[df.sum(axis=1)!=L]
N_wrong = df_not_correct.shape[0]
counts = [all_wro*100/float(N)]
for i in xrange(1,L):
#print i
if not(df_not_correct[df_not_correct.sum(axis=1)==i].empty):
if df_not_correct[df_not_correct.sum(axis=1)==i].shape[0] == 0:
counts.append(1*100/float(N))
else:
counts.append(df_not_correct[df_not_correct.sum(axis=1)==i].shape[0]*100/float(N))
else:
counts.append(float(0))
#print counts
non_corr_s = '%s : %0.2f || ' % ('None Correct', counts[0])
#print len(counts)
for i in xrange(1,L):
#print i
non_corr_s +='%d correct : %0.2f || ' % (i, counts[i])
print 'Not all Correct Instances Distributions'
print non_corr_s[:-4]
if print_flag:
fig, ax = plt.subplots()
s = plt.bar([i for i in xrange(1,L+1)], counts, align='center', alpha=0.4)
plt.xticks([i for i in xrange(1,L+1)], ['None correct']+['%d correct'% i for i in xrange(1,L)])
plt.ylabel('% Percentage of not all correct instances')
plt.title('Distribution of not all correct instances')
autolabel(s.patches, ax)
plt.show()
return df, fig1
def autolabel(rects, ax):
# attach some text labels
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2. ,1.0*height, '%0.2f' % float(height),ha='center', va='bottom')