-
Notifications
You must be signed in to change notification settings - Fork 0
/
scikit_ga.py
359 lines (252 loc) · 12.2 KB
/
scikit_ga.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
# Deap modules
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
from multiprocessing.dummy import Pool
import matplotlib.pyplot as plt
# Data science modules
import numpy as np
# system modules
import random
# sklearn modules
from sklearn.model_selection import cross_val_score
##########################
class GASelection:
def __init__(self, model, X, y, cv, pop_size=5, n_gen=10, n_jobs=1):
self.model = model
self.X = X
self.y=y
self.cv=cv
self.toolbox = None
self.creator = None
self.stats = None
# ga parameters
self.pop_size = pop_size
self.n_gen = n_gen
self.n_jobs=n_jobs
def create_creator(self):
# define fitness, individual CLASSES
# creator.create("FitnessMax", base.Fitness, weights=(1.0,))
# creator.create("Individual", list, fitness=creator.FitnessMax)
creator.create("FitnessMulti", base.Fitness, weights=(1,-1))
creator.create("Individual", list, fitness=creator.FitnessMulti)
return creator
def eval_function(self, individual):
ind = list(map(bool, individual))
acccuracy = cross_val_score(estimator=self.model,
X=self.X[:,ind],
y=self.y,
cv=self.cv,
scoring='accuracy'
).mean().round(4)
size = np.sum(ind)
return (acccuracy, size)
def _init_toolbox(self):
toolbox = base.Toolbox()
# Attribute generator
random.seed(42)
toolbox.register('attr_bool', random.randint, 0, 1)
# Structure initializers
toolbox.register('individual', tools.initRepeat, self.creator.Individual, toolbox.attr_bool, self.X.shape[1])
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
return toolbox
def register_toolbox(self):
toolbox = self._init_toolbox()
toolbox.register('evaluate', self.eval_function)
toolbox.register('mate', tools.cxPartialyMatched) #
toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
# toolbox.register('select', tools.selTournament, tournsize=5)
toolbox.register('select', tools.selNSGA2)
return toolbox
def eaMuPlusLambda(self, population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
stats=None, halloffame=None,dynamic_pb=False, verbose=__debug__):
# Logbook
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals', 'fitness', 'size']
logbook.chapters['fitness'].header = "min", "avg", "max"
logbook.chapters['size'].header = "min", "avg", "max"
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if halloffame is not None:
halloffame.update(population)
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
# Vary the population
if dynamic_pb:
mutpb = gen/(ngen+1)
cxpb = 1-mutpb
offspring = algorithms.varOr(population, toolbox, lambda_, cxpb, mutpb)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Update the hall of fame with the generated individuals
if halloffame is not None:
halloffame.update(offspring)
# Select the next generation population
population[:] = toolbox.select(population + offspring, mu)
# Update the statistics with the new population
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
return population, logbook
def eaMuCommaLambda(self, population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
stats=None, halloffame=None,dynamic_pb=False, verbose=__debug__):
assert lambda_ >= mu, "lambda must be greater or equal to mu."
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if halloffame is not None:
halloffame.update(population)
# Logbook
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals', 'fitness', 'size']
logbook.chapters['fitness'].header = "min", "avg", "max"
logbook.chapters['size'].header = "min", "avg", "max"
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
# Vary the population
if dynamic_pb:
mutpb = gen/(ngen+1)
cxpb = 1-mutpb
offspring = algorithms.varOr(population, toolbox, lambda_, cxpb, mutpb)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# Update the hall of fame with the generated individuals
if halloffame is not None:
halloffame.update(offspring)
# Select the next generation population
population[:] = toolbox.select(offspring, mu)
# Update the statistics with the new population
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
return population, logbook
def eaSimpleWithElitism(self, population, toolbox, cxpb, mutpb, ngen, stats=None,
halloffame=None, verbose=__debug__, dynamic_pb=False):
logbook = tools.Logbook()
# Logbook
logbook.header = ['gen', 'nevals', 'fitness', 'size']
logbook.chapters['fitness'].header = "min", "avg", "max"
logbook.chapters['size'].header = "min", "avg", "max"
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in population if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
if halloffame is None:
raise ValueError("halloffame parameter must not be empty!")
halloffame.update(population)
hof_size = len(halloffame.items) if halloffame.items else 0
record = stats.compile(population) if stats else {}
logbook.record(gen=0, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
# Begin the generational process
for gen in range(1, ngen + 1):
# Select the next generation individuals
offspring = toolbox.select(population, len(population) - hof_size)
if dynamic_pb:
mutpb = gen/(ngen+1)
cxpb = 1-mutpb
# Vary the pool of individuals
offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb)
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
# add the best back to population:
offspring.extend(halloffame.items)
# Update the hall of fame with the generated individuals
halloffame.update(offspring)
# Replace the current population by the offspring
population[:] = offspring
# Append the current generation statistics to the logbook
record = stats.compile(population) if stats else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
if verbose:
print(logbook.stream)
return population, logbook
def run(self):
self.creator = self.create_creator()
self.toolbox = self.register_toolbox()
pool = Pool(self.n_jobs)
self.toolbox.register('map', pool.map)
pop = self.toolbox.population(n=self.pop_size)
hof = tools.HallOfFame(1)
stats_fit = tools.Statistics(lambda ind: ind.fitness.values[0])
stats_size = tools.Statistics(lambda ind: sum(ind))
self.stats = tools.MultiStatistics(fitness=stats_fit, size=stats_size)
# Statistics
self.stats.register("avg", np.mean, axis=0)
self.stats.register("min", np.min, axis=0)
self.stats.register("max", np.max, axis=0)
# Evolution
pop, log = self.eaMuCommaLambda(pop, self.toolbox, mu=self.pop_size, lambda_=self.pop_size*3,
cxpb=0.5, mutpb=0.3, ngen=self.n_gen,
stats=self.stats, halloffame=hof, verbose=True, dynamic_pb=False)
print("Best individual has fitness and size: %s" % ( hof[0].fitness))
print()
fig, axes = plt.subplots(1,2, figsize=(15,7))
for i, (chp_name,chp) in enumerate(log.chapters.items()):
legends = []
for score in chp[0].keys():
if (score != 'gen') and (score != 'nevals'):
axes[i].plot(chp.select('gen'), chp.select(score))
axes[i].set(xlabel='gen', ylabel=chp_name)
legends.append(score)
axes[i].legend(legends)
plt.show()
return pop, log, hof[0]
if __name__ == "__main__":
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import StratifiedShuffleSplit, train_test_split
from sklearn.svm import LinearSVC
from sklearn.datasets import make_classification
# Generating a df with 2 classes, 20 informative features out of 300 (10 of which redundant), 625 total samples
X,y = make_classification(n_classes=2, n_samples=625, n_features=200, n_informative=20, n_redundant=10, shuffle=True, random_state=42)
# indipendent test validation separated
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, shuffle=True, test_size=.2, random_state= 42)
print(f'X_train shape: {X_train.shape}')
print(f'X_test shape: {X_test.shape}')
# defining model
model = Pipeline([ ('scaler', MinMaxScaler()),
('clf', LinearSVC(max_iter=5000))
])
# monte carlo cv for fitness evaluation
oob = StratifiedShuffleSplit(n_splits=10, test_size=.3, random_state=42)
# Setting GASelection class
gas = GASelection(model=model, X=X_train, y=y_train, cv=oob, pop_size=5, n_gen=25, n_jobs=4)
# Running GA
pop, log, best = gas.run()
best_bool = list(map(bool,best))
print()
print()
print('### Test performances ###')
model.fit(X_train, y_train)
print(f'> ALL FEATURES Test partition accuracy: {model.score(X_test, y_test):.4f}')
print()
model.fit(X_train[:, best_bool], y_train)
print(f'> GA SELECTED FEATURES partition accuracy: {model.score(X_test[:, best_bool], y_test):.4f}')