Algorithm 2 Genetic |
Input: X, y, X_train, X_test, y_train, y_test, new_model Output: Gene, score Begin new_model = ML Function initilization_of_population(size, n_feat) population = Empty list For i in range from 1 to size chromosome = Boolean array of size n_feat initialized to True For j in range from 0 to int(0.3 * n_feat) chromosome[j] = False Endfor Randomly shuffle the elements in chromosome Append chromosome to the population Endfor Return population Endfunction Function fitness_score (population, X_train, X_test, y_train, y_test) scores = Empty list For each chromosome in population Create a model (new_model) Train the model on X_train using the features specified by the chromosome Predict class labels on X_test with the model Calculate accuracy score by comparing predictions with y_test Append the score to scores Endfor Sort scores in descending order while maintaining the correspondence with the population Return scores, sorted population Endfunction Function selection (pop_after_fit, n_parents) population_nextgen = Empty list For i in range from 1 to n_parents Append pop_after_fit[i] to population_nextgen Endfor Return population_nextgen Endfunction Function crossover (pop_after_sel) pop_nextgen = Copy pop_after_sel For i in range from 0 to the size of pop_after_sel - 1 with a step of 2 new_par = Empty list child_1, child_2 = pop_nextgen[i], pop_nextgen[i+1] new_par = Concatenate the first half of child_1 and the second half of child_2. Append new_par to pop_nextgen Endfor Return pop_nextgen Endfunction Function mutation (pop_after_cross, mutation_rate, n_feat) mutation_range = Integer rounded down of mutation_rate times n_feat pop_next_gen = Empty list For each individual in pop_after_cross chromosome < = Copy the individual rand_posi < = Empty list For i in range from 1 to mutation_range pos < = Random integer between 0 and n_feat - 1 Append pos to rand_posi Endfor For each position in rand_posi Invert the value of the gene corresponding to that position in chromosome Endfor Append chromosome to pop_next_gen Endfor Return pop_next_gen Endfunction Function generations (df, label, size, n_feat, n_parents, mutation_rate, n_gen, X_train, X_test, y_train, y_test) best_chromo = Empty list best_score = Empty list population_nextgen = Call initilization_of_population with size and n_feat For each generation i from 1 to n_gen scores, pop_after_fit = Call fitness_score with population_nextgen, X_train, X_test, y_train, y_test Print the best score in generation i pop_after_sel = Call selection with pop_after_fit and n_parents pop_after_cross = Call crossover with pop_after_sel population_nextgen = Call mutation with pop_after_cross, mutation_rate, n_feat Append the best individual from generation i to best_chromo Append the best score from generation i to best_score Endfor Return best_chromo, best_score Endfunction Calling the function Gene,score = generations (X, y, size = 80, n_feat = X.shape[1], n_parents = 64, mutation_rate = 0.20, n_gen = 5, X_train = X_train, X_test = X_test, y_train = y_train, y_test = y_test) |