-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensembles.py
286 lines (242 loc) · 11.8 KB
/
ensembles.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
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import KFold
import math
import numpy as np
from scipy import stats
import copy
import random
import operator
import time
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
class Chromossome:
def __init__(self, genotypes_pool, random_state=None):
self.genotypes_pool = genotypes_pool
self.classifier = None
self.mutate()
self.fitness = 0
self.random_state = random_state
def fit(self, X, y):
is_fitted = True
self.classifier.fit(X, y)
def predict(self, X):
return self.classifier.predict(X)
def mutate(self, n_positions=None):
change_classifier = random.randint(0, len(self.genotypes_pool))
if self.classifier is None or change_classifier != 0:
param = {}
classifier_algorithm = random.choice(list(self.genotypes_pool.keys()))
else:
param = self.classifier.get_params()
classifier_algorithm = self.classifier.__class__
if not n_positions or n_positions>len(self.genotypes_pool[classifier_algorithm]):
n_positions = len(self.genotypes_pool[classifier_algorithm])
mutation_positions = random.sample(range(0, len(self.genotypes_pool[classifier_algorithm])), n_positions)
i=0
for hyperparameter, h_range in self.genotypes_pool[classifier_algorithm].items():
if i in mutation_positions or classifier_algorithm != self.classifier.__class__:
if isinstance(h_range[0], str):
param[hyperparameter] = random.choice(h_range)
elif isinstance(h_range[0], float):
param[hyperparameter] = random.uniform(h_range[0], h_range[1]+1)
else:
param[hyperparameter] = random.randint(h_range[0], h_range[1]+1)
i+= 1
self.classifier = classifier_algorithm(**param)
try:
self.classifier.set_param(random_state=self.random_state)
except:
pass
class DiversityEnsembleClassifier:
def __init__(self, algorithms, population_size = 100, max_epochs = 100, random_state=None):
self.population_size = population_size
self.max_epochs = max_epochs
self.population = []
self.algorithms = algorithms
self.random_state = random_state
random.seed(self.random_state)
for i in range(0, population_size):
self.population.append(Chromossome(genotypes_pool=algorithms, random_state=random_state))
def generate_offspring(self, parents, children):
if not parents:
parents = [x for x in range(0, self.population_size)]
children = [x for x in range(self.population_size, 2*self.population_size)]
for i in range(0, len(children)):
new_chromossome = copy.deepcopy(self.population[parents[i%len(parents)]])
new_chromossome.mutate()
try:
self.population[children[i]] = new_chromossome
except:
self.population.append(new_chromossome)
def fit_predict_population(self, not_fitted, predictions, kfolds, X, y):
for i in not_fitted:
chromossome = self.population[i]
##print (i, chromossome.classifier.__class__, end=' ')
for train, val in kfolds.split(X):
chromossome.fit(X[train], y[train])
predictions[i][val] = np.equal(chromossome.predict(X[val]), y[val])
return predictions
def diversity_selection(self, predictions, selection_threshold):
#print(-1)
distances = np.zeros(2*self.population_size)
pop_fitness = predictions.sum(axis=1)/predictions.shape[1]
target_chromossome = np.argmax(pop_fitness)
selected = [target_chromossome]
self.population[target_chromossome].fitness = pop_fitness[target_chromossome]
diversity = np.zeros(2*self.population_size)
mean_fitness = pop_fitness[target_chromossome]
distances[pop_fitness < selection_threshold] = float('-inf')
#print(distances)
for i in range(0, self.population_size-1):
#print(i)
#print('.' ,end='')
distances[target_chromossome] = float('-inf')
d_i = np.logical_xor(predictions, predictions[target_chromossome]).sum(axis=1)
distances += d_i
target_chromossome = np.argmax(distances)
if distances[target_chromossome] == float('-inf'):
break
diversity += d_i/predictions.shape[1]
mean_fitness += pop_fitness[target_chromossome]
##print(pop_fitness[target_chromossome])
selected.append(target_chromossome)
self.population[target_chromossome].fitness = pop_fitness[target_chromossome]
return selected, (diversity[selected]/self.population_size).mean(), mean_fitness/(self.population_size)
def fit(self, X, y):
diversity_values, fitness_values = [], []
##print('Starting genetic algorithm...')
kf = KFold(n_splits=5, random_state=self.random_state)
start_time = int(round(time.time() * 1000))
random.seed(self.random_state)
selected, not_selected = [], []
predictions = np.empty([2*self.population_size, y.shape[0]])
frequencies = np.unique(y, return_counts=True)[1]
selection_threshold = max(frequencies)/np.sum(frequencies)
for epoch in range(self.max_epochs):
#print('-' * 60)
#print('Epoch', epoch)
#print('-' * 60)
not_selected = np.setdiff1d([x for x in range(0, 2*self.population_size)], selected)
#print('Generating offspring...', end='')
aux = int(round(time.time() * 1000))
self.generate_offspring(selected, not_selected)
#print('done in',int(round(time.time() * 1000)) - aux, 'ms')
#print('Fitting and predicting population...', end='')
aux = int(round(time.time() * 1000))
predictions = self.fit_predict_population(not_selected, predictions, kf, X, y)
#print('done in',int(round(time.time() * 1000)) - aux, 'ms')
#print('Applying diversity selection...', end='')
aux = int(round(time.time() * 1000))
selected, diversity, fitness = self.diversity_selection(predictions, selection_threshold)
#print('done in',int(round(time.time() * 1000)) - aux, 'ms')
diversity_values.append(diversity)
fitness_values.append(fitness)
#print('New population diversity measure:', diversity)
#print('-' * 60, '\nFinished genetic algorithm in ', int(round(time.time() * 1000)) - start_time, 'ms')
self.population = [self.population[x] for x in selected]
for chromossome in self.population:
chromossome.fit(X, y)
return [diversity_values, fitness_values]
def predict(self, X):
predictions = np.empty((self.population_size, len(X)))
y = np.empty(len(X))
for chromossome in range(0, self.population_size):
predictions[chromossome] = self.population[chromossome].predict(X)
for i in range(0, len(X)):
pred = {}
for j in range(0, self.population_size):
if predictions[j][i] in pred:
pred[predictions[j][i]] += self.population[j].fitness
else:
pred[predictions[j][i]] = self.population[j].fitness
y[i] = max(pred.items(), key=operator.itemgetter(1))[0]
return y
class GeneticEnsembleClassifier:
def __init__(self, algorithms, population_size = 100, max_epochs = 100, random_state=None):
self.population_size = population_size
self.max_epochs = max_epochs
self.population = []
self.random_state = random_state
random.seed(self.random_state)
for i in range(0, population_size):
self.population.append(Chromossome(genotypes_pool=algorithms, random_state=random_state))
def generate_offspring(self, parents, children):
if not parents:
parents = [x for x in range(0, self.population_size)]
children = [x for x in range(self.population_size, 2*self.population_size)]
for i in range(0, self.population_size):
new_chromossome = copy.deepcopy(self.population[parents[i]])
new_chromossome.mutate(1)
try:
self.population[children[i]] = new_chromossome
except:
self.population.append(new_chromossome)
def fit_predict_population(self, not_fitted, predictions, kfolds, X, y):
for i in not_fitted:
chromossome = self.population[i]
for train, val in kfolds.split(X):
chromossome.fit(X[train], y[train])
predictions[i][val] = np.equal(chromossome.predict(X[val]), y[val])
return predictions
def fitness_selection(self, predictions):
pop_fitness = predictions.sum(axis=1)
target_chromossome = np.argmax(pop_fitness)
selected = [target_chromossome]
self.population[target_chromossome].fitness = pop_fitness[target_chromossome]
for i in range(0, self.population_size-1):
pop_fitness[target_chromossome] = -1
target_chromossome = np.argmax(pop_fitness)
selected.append(target_chromossome)
self.population[target_chromossome].fitness = pop_fitness[target_chromossome]
return selected
def fit(self, X, y):
diversity_values = []
kf = KFold(n_splits=5, random_state=self.random_state)
random.seed(self.random_state)
selected, not_selected = [], []
predictions = np.empty([2*self.population_size, y.shape[0]])
for epoch in range(self.max_epochs):
not_selected = np.setdiff1d([x for x in range(0, 2*self.population_size)], selected)
self.generate_offspring(selected, not_selected)
predictions = self.fit_predict_population(not_selected, predictions, kf, X, y)
selected = self.fitness_selection(predictions)
self.population = [self.population[x] for x in selected]
for chromossome in self.population:
chromossome.fit(X, y)
def predict(self, X):
predictions = np.empty((self.population_size, len(X)))
y = np.empty(len(X))
for chromossome in range(0, self.population_size):
predictions[chromossome] = self.population[chromossome].predict(X)
for i in range(0, len(X)):
pred = {}
for j in range(0, self.population_size):
if predictions[j][i] in pred:
pred[predictions[j][i]] += self.population[j].fitness
else:
pred[predictions[j][i]] = self.population[j].fitness
y[i] = max(pred.items(), key=operator.itemgetter(1))[0]
return y
class RandomClassifier:
def __init__(self, algorithms, random_state=None):
self.classifier = Chromossome(genotypes_pool=algorithms, random_state=random_state)
self.random_state=random_state
def fit(self, X, y):
self.classifier.fit(X, y)
def predict(self, X):
return self.classifier.predict(X)
class MajorityClassifier:
def __init__(self):
self.majority = None
def fit(self, X, y):
self.majority = stats.mode(y)[0][0]
def predict(self, X):
return np.full(X.shape[0], self.majority)