forked from scikit-learn-contrib/DESlib
/
diversity.py
307 lines (226 loc) · 10.2 KB
/
diversity.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
# coding=utf-8
# Author: Rafael Menelau Oliveira e Cruz <rafaelmenelau@gmail.com>
#
# License: BSD 3 clause
import sys
import numpy as np
"""
This file contains the implementation of key diversity measures found in the ensemble literature:
- Double Fault
- Negative Double fault
- Q-statistics
- Ratio of errors
- Agreement/Disagreement
- Classifier Correlation
The implementation are made according to the specifications from the book "Combining Pattern Classifiers" based on
Oracle outputs, i.e., taking into account if the pair of classifiers made the correct/incorrect prediction:
N00 : represents samples that both classifiers made a wrong prediction
N10 : represents samples that only classifier 2 predicts the wrong label.
N10 : represents samples that only classifier 1 predicts the wrong label.
N11 : represents samples that both classifiers predicts the correct label.
References
----------
Kuncheva, Ludmila I. Combining pattern classifiers: methods and algorithms. John Wiley & Sons, 2004.
Shipp, Catherine A., and Ludmila I. Kuncheva. "Relationships between combination methods and measures of diversity
in combining classifiers." Information fusion 3.2 (2002): 135-148.
Giacinto, Giorgio, and Fabio Roli. "Design of effective neural network ensembles for image classification purposes.
" Image and Vision Computing 19.9 (2001): 699-707.
Aksela, Matti. "Comparison of classifier selection methods for improving committee performance.
" Multiple Classifier Systems (2003): 159-159.
"""
def _process_predictions(y, y_pred1, y_pred2):
"""Pre-process the predictions of a pair of base classifiers for the computation of the diversity measures
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
N00 : Percentage of samples that both classifiers predict the wrong label.
N10 : Percentage of samples that only classifier 2 predicts the wrong label.
N10 : Percentage of samples that only classifier 1 predicts the wrong label.
N11 : Percentage of samples that both classifiers predicts the correct label.
"""
size_y = len(y)
if size_y != len(y_pred1) or size_y != len(y_pred2):
raise ValueError('The vector with class labels must have the same size.')
N00, N10, N01, N11 = 0.0, 0.0, 0.0, 0.0
for index in range(size_y):
if y_pred1[index] == y[index] and y_pred2[index] == y[index]:
N11 += 1.0
elif y_pred1[index] == y[index] and y_pred2[index] != y[index]:
N10 += 1.0
elif y_pred1[index] != y[index] and y_pred2[index] == y[index]:
N01 += 1.0
else:
N00 += 1.0
return N00/size_y, N10/size_y, N01/size_y, N11/size_y
def double_fault(y, y_pred1, y_pred2):
"""Calculates the double fault (df) measure. This measure represents the probability that both classifiers makes the
wrong prediction. A lower value of df means the base classifiers are less likely to make the same error.
This measure must be minimized to increase diversity.
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
df : The double fault measure between two classifiers
References
----------
Giacinto, Giorgio, and Fabio Roli. "Design of effective neural network ensembles for image classification purposes."
Image and Vision Computing 19.9 (2001): 699-707.
"""
N00, _, _, _ = _process_predictions(y, y_pred1, y_pred2)
df = N00
return df
def negative_double_fault(y, y_pred1, y_pred2):
"""The negative of the double fault measure. This measure should be maximized for a higher diversity.
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
df : The negative double fault measure between two classifiers
References
----------
Giacinto, Giorgio, and Fabio Roli. "Design of effective neural network ensembles for image classification purposes."
Image and Vision Computing 19.9 (2001): 699-707.
"""
return -double_fault(y, y_pred1, y_pred2)
def Q_statistic(y, y_pred1, y_pred2):
"""Calculates the Q-statistics diversity measure between a pair of classifiers. The Q value is in a range [-1, 1].
Classifiers that tend to classify the same object correctly will have positive values of Q, and
Q = 0 for two independent classifiers.
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
Q : The q-statistic measure between two classifiers
"""
N00, N10, N01, N11 = _process_predictions(y, y_pred1, y_pred2)
Q = ((N11*N00) - (N01*N10)) / ((N11 * N00) + (N01 * N10))
return Q
def ratio_errors(y, y_pred1, y_pred2):
"""Calculates Ratio of errors diversity measure between a pair of classifiers. A higher value means that the base
classifiers are less likely to make the same errors. The ratio must be maximized for a higher diversity.
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
ratio : The q-statistic measure between two classifiers
References
----------
Aksela, Matti. "Comparison of classifier selection methods for improving committee performance."
Multiple Classifier Systems (2003): 159-159.
"""
N00, N10, N01, N11 = _process_predictions(y, y_pred1, y_pred2)
if N00 == 0:
ratio = sys.float_info.max
else:
ratio = (N01 + N10) / N00
return ratio
def disagreement_measure(y, y_pred1, y_pred2):
"""Calculates the disagreement measure between a pair of classifiers. This measure is calculated by the frequency
that only one classifier makes the correct prediction.
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
disagreement : The frequency at which both classifiers disagrees
"""
_, N10, N01, _ = _process_predictions(y, y_pred1, y_pred2)
disagreement = N10 + N01
return disagreement
def agreement_measure(y, y_pred1, y_pred2):
"""Calculates the agreement measure between a pair of classifiers. This measure is calculated by the frequency
that both classifiers either obtained the correct or incorrect prediction for any given sample
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
agreement : The frequency at which both classifiers agrees
"""
N00, _, _, N11 = _process_predictions(y, y_pred1, y_pred2)
agreement = N00 + N11
return agreement
def correlation_coefficient(y, y_pred1, y_pred2):
"""Calculates the correlation between two classifiers using oracle outputs.
coefficient is a value in a range [-1, 1].
Parameters
----------
y : array of shape = [n_samples]:
class labels of each sample.
y_pred1 : array of shape = [n_samples]:
predicted class labels by the classifier 1 for each sample.
y_pred2 : array of shape = [n_samples]:
predicted class labels by the classifier 2 for each sample.
Returns
-------
rho : The correlation coefficient measured between two classifiers
"""
N00, N10, N01, N11 = _process_predictions(y, y_pred1, y_pred2)
tmp = (N11 * N00) - (N10 * N01)
rho = tmp/np.sqrt((N11 + N01) * (N10 + N00) * (N11 + N10) * (N01 + N00))
return rho
def compute_pairwise_diversity(targets, prediction_matrix, diversity_func):
"""Computes the pairwise diversity matrix.
Parameters
----------
targets : array of shape = [n_samples]:
Class labels of each sample in X.
prediction_matrix : array of shape = [n_samples, n_classifiers]:
Predicted class labels for each classifier in the pool
diversity_func : Function used to estimate the pairwise diversity
Returns
-------
diversity : array of shape = [n_classifiers]
The average pairwise diversity matrix calculated for the pool of classifiers
"""
n_classifiers = prediction_matrix.shape[1]
diversity = np.zeros(n_classifiers)
for clf_index in range(n_classifiers):
for clf_index2 in range(clf_index + 1, n_classifiers):
this_diversity = diversity_func(targets,
prediction_matrix[:, clf_index],
prediction_matrix[:, clf_index2])
diversity[clf_index] += this_diversity
diversity[clf_index2] += this_diversity
return diversity