-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilabel_evaluation.py
467 lines (283 loc) · 14.3 KB
/
multilabel_evaluation.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import numpy as np
import pandas as pd
class MultilabelPredictionEvaluater(object):
"""
Class for evaluation and comparison of multi-label classifiers.
Most methods of this class take an ndarray of predictions as input and compare them (in some way) to the
correct multi-labels which the constructor method stores as self.y.
Methods:
__init__: Constructor method.
correct_predictions: Gives an ndarray indicating correctness of the predictions.
correct_predictions_per_sample: Returns an ndarray indicating the number of correct predictions per sample.
correct_predictions_per_label: Returns an ndarray indicating how many times each label was correctly predicted.
num_correct_predictions: Returns the total number of correct predictions.
strict_accuracy: Strict evaluation measure for multi-label classifiers.
false_positives: Returns an ndarray indicating the presence of false positives among the predictions.
false_positives_per_sample: Indicates the number of false positives per sample.
false_positives_per_label: Returns an ndarray indicating the number of false positives per label.
num_false_positives: Gives the total number of false positive predictions.
false_negatives: Returns an ndarray indicating presence of false negatives among the predictions.
false_negatives_per_sample: Indicates how many false negative predictions there are per sample.
false_negatives_per_label: Indicates how many false negative predictions were made for each label.
num_false_negatives: The total number of false negative predictions.
accuracy: Standard evaluation measure for multi-label classifiers.
preds_per_label: Returns the number of times each label was predicted.
comparison_table: Returns a table comparing predictions performed by different classifiers.
"""
def __init__(self, y):
"""
Constructor method.
:param y: Correct multi-labels.
:type y: ndarray, shape = (num_samples, num_labels).
"""
self.y = y
def correct_predictions(self, x):
"""
Returns an ndarray where entry i,j is True if the corresponding prediction was correct.
:param x: Predictions
:return: ndarray indicating correct predictions.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype : ndarray, shape = (num_samples, nun_labels) = self.y.shape = x.shape
"""
comparison = np.equal(x, self.y)
return comparison
def correct_predictions_per_sample(self, x):
"""
Returns an ndarray where the i'th entry is the number of correct predictions in sample i
:param x: Predictions.
:return: Array indicating the number of correct predictions per sample.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_samples,)
"""
return np.sum(self.correct_predictions(x), axis=1)
def correct_predictions_per_label(self, x):
"""
Returns an ndarray where the i'th entry is the number of times the corresponding label was correctly predicted.
:param x: Predictions.
:return ndarray indicating the number of correct predictions per label.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_labels)
"""
return np.sum(self.correct_predictions(x), axis=0)
def num_correct_predictions(self, x):
"""
Returns the total number of all correct predictions.
:param x: Predictions.
:return: The total number of correct predictions.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: int
"""
return int(np.sum(self.correct_predictions_per_sample(x), axis=0))
def strict_accuracy(self, x):
"""
A strict evaluation measure. Only the samples where every label was correctly predicted contribute to the score.
:param x: Predictions.
:return: The sum of all samples that were labeled correctly divided by the total number of samples.
:type x: ndarray, shape = (num_samples, num_labels)
:rtype: float
"""
comparison = self.correct_predictions(x).astype(int)
correct = np.ones(self.y.shape[0])
for i in range(self.y.shape[1]):
correct *= comparison[:, i]
return np.sum(correct, axis=0) / self.y.shape[0]
def false_positives(self, x):
"""
Returns an ndarray where the i,j'th entry is True if the corresponding prediction was a false positive.
:param x: Predictions.
:return ndarray indicating false predictions.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_samples, num_labels) = x.shape = self.y.shape
"""
ones = np.ones(shape=self.y.shape)
fp = np.equal((x - self.y), ones)
return fp
def false_positives_per_sample(self, x):
"""
Returns an ndarray where the i'th entry is the number of false positives predicted for sample i.
:param x: Predictions.
:return ndarray indicating the number of false positives per sample.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_samples,)
"""
return np.sum(self.false_positives(x), axis=1)
def false_positives_per_label(self, x):
"""
Returns an ndarray indicating the number of false positives per label.
The i'th entry of the returned ndarray is the number of times the corresponding label was a false positive
among the predictions.
:param x: Predictions.
:return: ndarray indicating false positives per label.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_labels,)
"""
return np.sum(self.false_positives(x), axis=0)
def num_false_positives(self, x):
"""
Returns the total number of all false positive predictions.
:param x: Predictions.
:return: Total number of false positive predictions.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: int
"""
return int(np.sum(self.false_positives_per_sample(x), axis=0))
def false_negatives(self, x):
"""
Returns an ndarray where the i,j'th entry is True if the corresponding prediction was a false negative.
:param x: Predictions.
:return: ndarray indicating false negatives.
:type x: ndarray, shape = (num_samples, num_labels)
:rtype: ndarray, shape = (num_samples, num_labels) = x.shape = self.y.shape
"""
minus_ones = -1 * np.ones(shape=self.y.shape)
fn = np.equal((x - self.y), minus_ones)
return fn
def false_negatives_per_sample(self, x):
"""
Returns an ndarray where the i'th entry is the number of false negatives predicted for sample i.
:param x: Predictions.
:return: ndarray indicating false predictions per sample.
:type x: ndarray, shape = (num_samples, num_labels)
:rtype: ndarray, shape = (num_samples,)
"""
return np.sum(self.false_negatives(x), axis=1)
def false_negatives_per_label(self, x):
"""
Returns an ndarray indicating the number of false positives among the labels.
The i'th entry of the returned ndarray is the number of times the corresponding label was identified as
a false negative among the predictions.
:param x: Predictions.
:return: ndarray indicating false negatives per label.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: ndarray, shape = (num_labels,)
"""
return np.sum(self.false_negatives(x), axis=0)
def num_false_negatives(self, x):
"""
Returns the total number of false negative predictions.
:param x: Predictions.
:return The total number of false negatives among the predictions.
:type x: ndarray, shape = (num_samples, num_labels) = self.y.shape
:rtype: int
"""
return int(np.sum(self.false_negatives_per_sample(x), axis=0))
def accuracy(self, x, lenience=None):
"""
Standard evaluation measure for multi-label classification problems.
This evaluation measure can for instance be found in the paper 'Classifier chains for
multi-label classification' by Jessee Read et.al.
:param x: Predictions.
:param lenience: optional, default = None. If set to 'false positives' (resp. 'false negatives' )
we are more lenient towards false positive (resp. false negative) predictions.
:return sum of terms between 0.0 and 1/num_samples where each sample with at least one correctly predicted
label contributes to the sum.
:type x: ndarray, shape = (num_samples, num_labels)
:type lenience: str
:rtype: float
"""
correct = self.correct_predictions_per_sample(x)
num_labels_vector = self.y.shape[1]*np.ones(self.y.shape[0])
fp = self.false_positives_per_sample(x)
fn = self.false_negatives_per_sample(x)
denominator = num_labels_vector + fp + fn
if lenience == 'false positives':
denominator -= fp
elif lenience == 'false negatives':
denominator -= fn
return np.sum(correct/denominator, axis=0) / self.y.shape[0]
@staticmethod
def preds_per_label(x):
"""
Returns an ndarray where the i'th entry is the number of times the corresponding label was predicted.
:param x: Predictions.
:return: ndarray indicating predictions per label.
:type x: ndarray, shape = (num_samples, num_labels)
:rtype: ndarray, shape = (num_labels,)
"""
return np.sum(x, axis=0)
def comparison_table(self, predictions, labels):
"""
Returns a table comparing predictions performed by different classifiers.
More precisely the method creates a pandas DataFrame with columns: strict accuracy, accuracy, false positives,
false negatives, most false positives and most false negatives. The i'th row corresponds to the i'th element
in the input list of predictions.
:param predictions: List of ndarray's of shape = (num_samples, num_labels)
:param labels: List of length num_labels where every entry is a string.
:return: comparison table
:type predictions: list
:type labels: list
:rtype: pandas DataFrame
"""
columns = ['strict accuracy', 'accuracy', 'false positives ', 'false negatives',
'most false positives', 'most false negatives']
# We will fill in the values in the pandas DataFrame by applying functions to each column. Let us first
# set create a DataFrame of shape (len(predictions), len(columns)) where all entries of the i'th row
# is the integer i.
temp_list = list(np.arange(len(predictions)))
temp_list = len(columns) * [temp_list]
temp_array = np.array(temp_list).T
temp_array = temp_array.astype(int)
df = pd.DataFrame(temp_array, index=list(range(len(predictions))), columns=columns)
# We now create the functions that are to be applied to each column of our DataFrame respectively.
def f_1(x):
return self.strict_accuracy(predictions[x])
def f_2(x):
return self.accuracy(predictions[x])
def f_3(x):
return self.num_false_positives(predictions[x])
def f_4(x):
return self.num_false_negatives(predictions[x])
def f_5(x):
return labels[int(np.argmax(self.false_positives_per_label(predictions[x])))]
def f_6(x):
return labels[int(np.argmax(self.false_negatives_per_label(predictions[x])))]
functions_list = [f_1, f_2, f_3, f_4, f_5, f_6]
column_function_dict = {key: value for (key, value) in zip(columns, functions_list)}
# We iterate over the columns in df where column_series is the Series corresponding to column
# in the list columns. For each of these columns we apply the function corresponding to column and update
# the values in df.
for column, column_series in df.iteritems():
df.update(column_series.apply(column_function_dict[column]))
return df
class MaskCreater(object):
"""
Class for extracting several interesting sub-sets from a given data set.
Methods:
__init__: Constructor method.
__call__: Creates a mask that can be used to extract the sub-set where certain labels (don't) appear.
"""
def __init__(self, y):
"""
Constructor method.
:param y: Multi-labels
:type y: ndarray, shape = (num_samples, num_labels).
"""
self.y = y
def __call__(self, col_ones=None, col_zeros=None):
"""
Call method.
This method constructs a mask that can be used to extract the subset of our data set of samples labeled with
a given set of labels and not labeled with another given set of labels.
:param col_ones: List of indices corresponding to the columns corresponding to the labels that should appear.
:param col_zeros: List of indices corresponding to the columns corresponding to the labels that we do not
want to appear.
:return: Mask to extract the desired sub-data set.
:type col_ones: list
:type col_zeros: list
:rtype: ndarray, shape = (num_labels,)
"""
ones = np.ones(shape=self.y.shape)
zeros = np.zeros(shape=self. y.shape)
comp_ones = np.equal(self.y, ones)
comp_zeros = np.equal(self.y, zeros)
one_mask = np.ones(self.y.shape[0]).astype(bool)
if col_ones is None:
col_ones = []
for index in col_ones:
one_mask *= comp_ones[:, index]
zero_mask = np.ones(self.y.shape[0]).astype(bool)
if col_zeros is None:
col_zeros = []
for index in col_zeros:
zero_mask *= comp_zeros[:, index]
return one_mask * zero_mask