-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenetic_algorithm.py
252 lines (188 loc) · 6.87 KB
/
genetic_algorithm.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
# -*- coding: utf-8 -*-
"""genetic algorithm
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1iEUkK89Q0AlITXIBp4u4QEhfKmj8Y3qC
"""
from google.colab import drive
drive.mount('/content/drive')
#import libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('/content/drive/MyDrive/Colab Notebooks/data.csv')
data.head()
data.shape
data.diagnosis.value_counts()
data.isna().any()
data.drop(['id','Unnamed: 32'],axis=1,inplace=True)
data.shape
data
y = data['diagnosis']
x = data.drop('diagnosis',axis =1)
y.replace(to_replace='M',value= 1,inplace=True)
y.replace(to_replace='B',value = 0,inplace=True)
"""# Genetic Algorithm Code"""
import math
import random
import statistics
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import cross_val_score
def genetic_algo(data,features,target,population_size,tol_level,top_number):
def init_population(population_size,c,top_number):
population = []
for i in range(population_size):
individual = [0]*c
j = 0
while(j<top_number):
p = random.uniform(0,1)
position = random.randrange(c)
if(p>=0.5 and individual[position]==0):
individual[position]=1
j=j+1
#edge case if all genes are 0 then we will make any one gene as 1
if(sum(individual)==0):
position = random.randrange(c)
individual[position] = 1
population.append(individual)
print('population is ')
print(population)
print('------------------')
return population
def calculate_fitness(features,target):
model = MLPClassifier()
scores = cross_val_score(model,features,target,scoring='f1_macro',n_jobs=-1,cv=10) #using f1_score as it is an imbalanced dataset
print(scores.mean())
return scores.mean()
def get_fitness(population,data):
fitness_values = []
for individual in population:
df = data
i=0
for column in data:
if(individual[i]==0):
df = df.drop(column,axis=1)
i=i+1
features = df
individual_fitness = calculate_fitness(features,target)
fitness_values.append(individual_fitness)
return fitness_values
def select_parents(population,fitness_values):
parents = []
total = sum(fitness_values)
norm_fitness_values = [x/total for x in fitness_values]
#find cumulative fitness values for roulette wheel selection
cumulative_fitness = []
start = 0
for norm_value in norm_fitness_values:
start+=norm_value
cumulative_fitness.append(start)
population_size = len(population)
for count in range(population_size):
random_number = random.uniform(0, 1)
individual_number = 0
for score in cumulative_fitness:
if(random_number<=score):
parents.append(population[individual_number])
break
individual_number+=1
return parents
#high probability crossover
def two_point_crossover(parents,probability):
random.shuffle(parents)
#count number of pairs for crossover
no_of_pairs = round(len(parents)*probability/2)
chromosome_len = len(parents[0])
crossover_population = []
for num in range(no_of_pairs):
length = len(parents)
parent1_index = random.randrange(length)
parent2_index = random.randrange(length)
while(parent1_index == parent2_index):
parent2_index = random.randrange(length)
start = random.randrange(chromosome_len)
end = random.randrange(chromosome_len)
if(start>end):
start,end = end, start
parent1 = parents[parent1_index]
parent2 = parents[parent2_index]
child1 = parent1[0:start]
child1.extend(parent2[start:end])
child1.extend(parent1[end:])
child2 = parent2[0:start]
child2.extend(parent1[start:end])
child2.extend(parent2[end:])
parents.remove(parent1)
parents.remove(parent2)
crossover_population.append(child1)
crossover_population.append(child2)
#to append remaining parents which are not undergoing crossover process
if(len(parents)>0):
for remaining_parents in parents:
crossover_population.append(remaining_parents)
return crossover_population
#low probability mutation
#mutation_probability is generally low to avoid a lot of randomness
def mutation(crossover_population):
#swapping of zero with one to retain no of features required
for individual in crossover_population:
index_1 = random.randrange(len(individual))
index_2 = random.randrange(len(individual))
while(index_2==index_1 or individual[index_1] == individual[index_2]):
index_2 = random.randrange(len(individual))
#swapping the bits
temp = individual[index_1]
individual[index_1] = individual[index_2]
individual[index_2] = temp
return crossover_population
c = data.shape[1] #length of the chromosome
population= init_population(population_size,c,top_number)
fitness_values = get_fitness(population,data)
parents = select_parents(population,fitness_values)
crossover_population = two_point_crossover(parents,0.78)
population = crossover_population
p = random.uniform(0,1)
if(p<=0.001):
mutated_population = mutation(crossover_population)
population = mutated_population
fitness_values = get_fitness(population,data)
variance_of_population = statistics.variance(fitness_values)
print("variance is",variance_of_population)
gen = 1
#repeating algorithm til stopping criterion is met
while(variance_of_population > tol_level):
print('generation-',gen)
parents = select_parents(population,fitness_values)
crossover_population = two_point_crossover(parents,0.78)
population = crossover_population
p = random.uniform(0,1)
if(p<=0.001): #mutation prob here
mutated_population = mutation(crossover_population)
population = mutated_population
fitness_values = get_fitness(population,data)
variance_of_population = statistics.variance(fitness_values)
print("variance is",variance_of_population)
gen+=1
best_features = []
best_f1_score = 0
optimal_fitness = sum(fitness_values)/len(fitness_values)
print('avg fitness is: ',optimal_fitness)
for index,fitness_value in enumerate(fitness_values):
error = abs((fitness_value - optimal_fitness)/optimal_fitness)
if(error <= 0.01):
best_features = population[index]
best_f1_score = fitness_value
print(best_features)
return best_features,best_f1_score
#running the algorithm
top_features, best_f1_score = genetic_algo(x,x,y,40,0.000005,25)
#printing top features selected through genetic algorithm
i = 0
list_of_features= []
for i in range(len(top_features)):
if(top_features[i]==1):
list_of_features.append(x.columns[i])
print(top_features)
print(list_of_features)
print(best_f1_score)