-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenetic.py
More file actions
147 lines (126 loc) · 5.57 KB
/
genetic.py
File metadata and controls
147 lines (126 loc) · 5.57 KB
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
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_boston
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression
SEED = 2018
random.seed(SEED)
np.random.seed(SEED)
#==============================================================================
# Data
#==============================================================================
dataset = load_boston()
X, y = dataset.data, dataset.target
features = dataset.feature_names
#==============================================================================
# CV MSE before feature selection
#==============================================================================
est = LinearRegression()
score = -1.0 * cross_val_score(est, X, y, cv=5, scoring="neg_mean_squared_error")
print("CV MSE before feature selection: {:.2f}".format(np.mean(score)))
#==============================================================================
# Class performing feature selection with genetic algorithm
#==============================================================================
class GeneticSelector():
def __init__(self, estimator, n_gen, size, n_best, n_rand,
n_children, mutation_rate):
# Estimator
self.estimator = estimator
# Number of generations
self.n_gen = n_gen
# Number of chromosomes in population
self.size = size
# Number of best chromosomes to select
self.n_best = n_best
# Number of random chromosomes to select
self.n_rand = n_rand
# Number of children created during crossover
self.n_children = n_children
# Probablity of chromosome mutation
self.mutation_rate = mutation_rate
if int((self.n_best + self.n_rand) / 2) * self.n_children != self.size:
raise ValueError("The population size is not stable.")
def initilize(self):
population = []
for i in range(self.size):
chromosome = np.ones(self.n_features, dtype=np.bool)
mask = np.random.rand(len(chromosome)) < 0.3
chromosome[mask] = False
population.append(chromosome)
return population
def fitness(self, population):
X, y = self.dataset
scores = []
for chromosome in population:
score = -1.0 * np.mean(cross_val_score(self.estimator, X[:,chromosome], y,
cv=5,
scoring="neg_mean_squared_error"))
scores.append(score)
scores, population = np.array(scores), np.array(population)
inds = np.argsort(scores)
return list(scores[inds]), list(population[inds,:])
def select(self, population_sorted):
population_next = []
for i in range(self.n_best):
population_next.append(population_sorted[i])
for i in range(self.n_rand):
population_next.append(random.choice(population_sorted))
random.shuffle(population_next)
return population_next
def crossover(self, population):
population_next = []
for i in range(int(len(population)/2)):
for j in range(self.n_children):
chromosome1, chromosome2 = population[i], population[len(population)-1-i]
child = chromosome1
mask = np.random.rand(len(child)) > 0.5
child[mask] = chromosome2[mask]
population_next.append(child)
return population_next
def mutate(self, population):
population_next = []
for i in range(len(population)):
chromosome = population[i]
if random.random() < self.mutation_rate:
mask = np.random.rand(len(chromosome)) < 0.05
chromosome[mask] = False
population_next.append(chromosome)
return population_next
def generate(self, population):
# Selection, crossover and mutation
scores_sorted, population_sorted = self.fitness(population)
population = self.select(population_sorted)
population = self.crossover(population)
population = self.mutate(population)
# History
self.chromosomes_best.append(population_sorted[0])
self.scores_best.append(scores_sorted[0])
self.scores_avg.append(np.mean(scores_sorted))
return population
def fit(self, X, y):
self.chromosomes_best = []
self.scores_best, self.scores_avg = [], []
self.dataset = X, y
self.n_features = X.shape[1]
population = self.initilize()
for i in range(self.n_gen):
population = self.generate(population)
return self
@property
def support_(self):
return self.chromosomes_best[-1]
def plot_scores(self):
plt.plot(self.scores_best, label='Best')
plt.plot(self.scores_avg, label='Average')
plt.legend()
plt.ylabel('Scores')
plt.xlabel('Generation')
plt.show()
sel = GeneticSelector(estimator=LinearRegression(),
n_gen=7, size=200, n_best=40, n_rand=40,
n_children=5, mutation_rate=0.05)
sel.fit(X, y)
sel.plot_scores()
score = -1.0 * cross_val_score(est, X[:,sel.support_], y, cv=5, scoring="neg_mean_squared_error")
print("CV MSE after feature selection: {:.2f}".format(np.mean(score)))