Python Code of ANNCGA
# Prepare Iris Dataset: 4 predictors, 1 target with 3 equal classes available, 150 instances
import numpy as np
import pandas as pdfrom deap import algorithms
# Genetic Algorithm constants:
POPULATION_SIZE = 10
P_CROSSOVER = 0.9 # probability for crossover
P_MUTATION = 0.2 # probability for mutating an individual
MAX_GENERATIONS = 40
HALL_OF_FAME_SIZE = 4
replication = 10
kfold = model_selection.KFold(n_splits=5, random_state=42,shuffle=True)
testing_acc=[]
training_acc=[]
comp_time=[]
best_dv=[]
convergencemax=[]
convergenceavg=[]
for i in range(replication):
# create initial population (generation 0):
population = toolbox.populationCreator(n=POPULATION_SIZE)
# prepare the statistics object:
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("max", np.max)
stats.register("avg", np.mean)
# define the hall-of-fame object:
hof = tools.HallOfFame(HALL_OF_FAME_SIZE)
start = time.time()
# perform the Genetic Algorithm flow with hof feature added:
population, logbook = algorithms.eaSimple(population,
toolbox,
cxpb=P_CROSSOVER,
mutpb=P_MUTATION,
ngen=MAX_GENERATIONS,
stats=stats,
halloffame=hof)
end=time.time()
# record all information
best_dv.append(getParams(hof.items[0]))
testing_acc.append(hof.items[0].fitness.values[0])
training_acc.append(getAccuracyTraining(hof.items[0]))
comp_time.append(end-start)
convergencemax.append(logbook.select('max'))
convergenceavg.append(logbook.select('avg'))
# print best solution found:
print("- Best solution is: ")
print("The best parameters and features = ", best_dv[np.argmax(testing_acc)])
print("Avg Testing Accuracy:", np.mean(testing_acc), "Best Testing Accuracy:", np.max(testing_acc), "Std Testing Accuracy:", np.std(testing_acc))
print("Avg Training Accuracy:", np.mean(training_acc), "Best Training Accuracy:", np.max(training_acc), "Std Training Accuracy:", np.std(training_acc))
print("Avg Computational Time", np.mean(comp_time))
convergencemax_history=np.mean(convergencemax,axis=0)
convergenceavg_history=np.mean(convergenceavg,axis=0)
# prepare dataset ---------------------------------------------------------------------> CHANGE!
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
data = pd.read_csv(url,header=None)
X = data.iloc[:, :-1] #--------------> CHANGE!
y = data.iloc[:, -1] #--------------> CHANGE!
# Normalize predictors
from sklearn.preprocessing import MinMaxScaler
X=MinMaxScaler().fit_transform(X)
# Prepare y input data into one-got encoded
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)
from tensorflow.keras.utils import to_categorical
y=to_categorical(y)
# Prepare Chromosomes
from deap import base
from deap import creator
from deap import tools
import random
# define a single objective, maximizing fitness strategy:
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
# create the Individual class based on list:
creator.create("Individual", list, fitness=creator.FitnessMax)
# Creating the individual
toolbox = base.Toolbox()
# fill in the with random floats for each type of hyper parameter
toolbox.register("h1", random.randint,300,600)
toolbox.register("h2", random.uniform,0.0001,0.4)
toolbox.register("h3", random.uniform,0,1)
toolbox.register("h4", random.uniform,2,len(X[0])) # Determine hidden nodes based on number of features
toolbox.register("h5", random.uniform,0,1)
toolbox.register("h6", random.uniform,0,1)
# feature ---------------------------------------------------------------------> CHANGE!
toolbox.register("f1", random.uniform,0,1)
toolbox.register("f2", random.uniform,0,1)
toolbox.register("f3", random.uniform,0,1)
toolbox.register("f4", random.uniform,0,1)
toolbox.register("IndividualCreator",tools.initCycle,creator.Individual,
(toolbox.h1,toolbox.h2,toolbox.h3,toolbox.h4,toolbox.h5,toolbox.h6,toolbox.f1,\
toolbox.f2,toolbox.f3,toolbox.f4),n=1)
# Converting The Chromosome to Hyper Parameter Form
def getParams(individual):
n_epochs = round(individual[0])
lrate = individual[1]
hid_lay = [1, 2][round(individual[2])]
hid_nodes = round(individual[3])
act_func1 = ['sigmoid', 'relu'][round(individual[4])]
act_func2 = ['sigmoid', 'relu'][round(individual[5])]
selected_feature=[]
for i in range(len(individual[6:])):
selected_feature.append(round(individual[i+6]))
selected_feature=np.array(selected_feature)
return n_epochs, lrate, hid_lay, hid_nodes, act_func1, act_func2,selected_feature
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
def create_model():
# Initialize NN with input layer + 1 hidden layer
NN_classification = Sequential()
NN_classification.add(Dense(units = hid_nodes, kernel_initializer = 'uniform', activation = act_func1, input_dim = sum(selected_feature)))
if hid_lay == 2:
# Add new hidden layer
NN_classification.add(Dense(units = hid_nodes, kernel_initializer = 'uniform', activation = act_func2))
# Add output layer
NN_classification.add(Dense(units = len(y[0]), kernel_initializer = 'uniform', activation = 'softmax'))
# Define the optimizer and the loss function
opt = Adam(lr=lrate)
NN_classification.compile(optimizer = opt, loss = 'categorical_crossentropy',metrics=['accuracy'])
return NN_classification
def getAccuracy(individual):
global n_epochs, lrate, hid_lay, hid_nodes, act_func1, act_func2,selected_feature
n_epochs, lrate, hid_lay, hid_nodes, act_func1, act_func2,selected_feature = getParams(individual)
X_selected=X[:,np.where(selected_feature==1)[0]]
model = KerasClassifier(build_fn=create_model, epochs=n_epochs, batch_size=32, verbose=0)
results = cross_validate(model, X_selected, y, cv=kfold,n_jobs=-1)
return results.get('test_score').mean()
def getAccuracyTraining(individual):
global n_epochs, lrate, hid_lay, hid_nodes, act_func1, act_func2,selected_feature
n_epochs, lrate, hid_lay, hid_nodes, act_func1, act_func2,selected_feature = getParams(individual)
X_selected=X[:,np.where(selected_feature==1)[0]]
model = KerasClassifier(build_fn=create_model, epochs=n_epochs, batch_size=32, verbose=0)
results = cross_validate(model, X_selected, y, cv=kfold,return_train_score=1,n_jobs=-1)
return results.get('train_score').mean()
# create the population operator to generate a list of individuals:
toolbox.register("populationCreator", tools.initRepeat, list, toolbox.IndividualCreator)
# fitness calculation
def classificationAccuracy(individual):
return getAccuracy(individual),
toolbox.register("evaluate", classificationAccuracy)
# bound for each decision variable -----------------------------------------------------------------> CHANGE interval!
low=[300,0.0001,0,2,0,0,0,0,0,0]
up=[600,0.4,1,len(X[0]),1,1,1,1,1,1]
# genetic operators:
toolbox.register("mate", tools.cxSimulatedBinaryBounded,eta=0.5,low=low,up=up)
toolbox.register("mutate", tools.mutPolynomialBounded,eta=0.7,low=low,up=up,indpb=0.5)
toolbox.register("select", tools.selTournament, tournsize=4)
from deap import algorithms
# Genetic Algorithm constants:
POPULATION_SIZE = 10
P_CROSSOVER = 0.9 # probability for crossover
P_MUTATION = 0.2 # probability for mutating an individual
MAX_GENERATIONS = 40
HALL_OF_FAME_SIZE = 4
replication = 10
kfold = model_selection.KFold(n_splits=5, random_state=42,shuffle=True)
testing_acc=[]
training_acc=[]
comp_time=[]
best_dv=[]
convergencemax=[]
convergenceavg=[]
for i in range(replication):
# create initial population (generation 0):
population = toolbox.populationCreator(n=POPULATION_SIZE)
# prepare the statistics object:
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("max", np.max)
stats.register("avg", np.mean)
# define the hall-of-fame object:
hof = tools.HallOfFame(HALL_OF_FAME_SIZE)
start = time.time()
# perform the Genetic Algorithm flow with hof feature added:
population, logbook = algorithms.eaSimple(population,
toolbox,
cxpb=P_CROSSOVER,
mutpb=P_MUTATION,
ngen=MAX_GENERATIONS,
stats=stats,
halloffame=hof)
end=time.time()
# record all information
best_dv.append(getParams(hof.items[0]))
testing_acc.append(hof.items[0].fitness.values[0])
training_acc.append(getAccuracyTraining(hof.items[0]))
comp_time.append(end-start)
convergencemax.append(logbook.select('max'))
convergenceavg.append(logbook.select('avg'))
# print best solution found:
print("- Best solution is: ")
print("The best parameters and features = ", best_dv[np.argmax(testing_acc)])
print("Avg Testing Accuracy:", np.mean(testing_acc), "Best Testing Accuracy:", np.max(testing_acc), "Std Testing Accuracy:", np.std(testing_acc))
print("Avg Training Accuracy:", np.mean(training_acc), "Best Training Accuracy:", np.max(training_acc), "Std Training Accuracy:", np.std(training_acc))
print("Avg Computational Time", np.mean(comp_time))
convergencemax_history=np.mean(convergencemax,axis=0)
convergenceavg_history=np.mean(convergenceavg,axis=0)