This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.py
executable file
·73 lines (57 loc) · 2.32 KB
/
funcs.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
#!/usr/bin/env python
import numpy as np
import pandas as pd
import multiprocessing
from sklearn.model_selection import cross_val_score
from deap import tools
from deap.benchmarks.tools import hypervolume
from datetime import datetime
def load_sonar_dataset(filepath):
"""Loads sonar data and returns a tuple with
sonar_data
sonar_target
sonar_target_classes
sonar_column_names
"""
sonar_columns = [ f'b{x:02d}' for x in np.arange(60) ]
sonar_columns.append('target')
# Read dataset
sonar_df = pd.read_csv(filepath, names=sonar_columns)
# Create a numpy representation
sonar_data = sonar_df.values[:, :-1].astype('float')
sonar_target = sonar_df.values[:, -1].astype(str)
sonar_target_classes = np.array(['Mine', 'Rock'])
return sonar_data, sonar_target, sonar_target_classes, sonar_columns
def feasible(individual):
return np.sum(individual) > 0
def evaluate_ml_classifier(individual, data, target, classifier, cross_val):
"""Evaluates the fitness of a individual (accuracy) given input parameters
data
target
classifier
validation shuffling
Returns the tuple with individual values"""
subdata = data[:, np.flatnonzero(individual)]
cv_scores = cross_val_score(classifier, subdata, target, cv=cross_val, verbose=0)
return cv_scores.mean(),-sum(individual)
def now_timestamp(individual):
return datetime.now().strftime('%Y%m%d%H%M%S')
def fitness_values(individual):
"""Return the fitness values of a given individual"""
return individual.fitness.values
def features(individual):
"""Return the fitness values of a given individual"""
return individual
def evaluate_population_fitness(toolbox, population):
"""Evaluates population fitness updating each individual fitness.
Returns the number of invalid population updated"""
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
return len(invalid_ind)
def pareto_dominance(ind1, ind2):
"""Returns a bool value regarding ind1 dominates ind2"""
return tools.emo.isDominated(ind1.fitness.values, ind2.fitness.values)
def evaluate_hypervolume(data):
return hypervolume(data, [1, 60])