# Genetic Algorithm for Machine learning in Python

In this tutorial, we will learn about the Genetic Algorithm for machine learning in Python. It is an algorithm mostly used in Machine Learning. It is basically on the evolution theory that we learned in our biology textbook. Curious? Let’s find out how. But before that first let us revise the Evolution theory, which gave us the term “Survival of the fittest”.

According, to the evolution theory, the individual who can face the changing environment, as well as biological situation, will survive. Let us find out how to relate them.

## Background of the genetic algorithm:

So, in a genetic algorithm what we do is that we simply start with creating a random solution to a given problem (the individual), and provide a fitness score the solution. Now, this fitness score is served as an ability to survive, and we keep on mutating solutions. On continuous mutation, selection, and rejection of solutions, the one with the best fitness score survives (survival of the fittest).

## An example use of the above algorithm:

Here, we will use the Genetic Algorithm to develop a given target string after many successful mutations. We will start with random results and will reach the target result. We have used the following terms to understand the algorithm and its implementation better. Here, alphabets and symbols are called as genetic material (Gen_Mat) and string formed are called as Individual.

Here, we have used fitness score as the measure of the characters which are different from the target string.

### Python Code: Genetic Algorithm for Machine learning

We have started the code with variables namely, POPULATION_SIZE, GENES, and TARGET to represent no. of individuals in each generation, characters, and our target string to be generated.

import random POPUL_Size = 100 Gen_Mat = '''ABCDEFGHIJKLMNOP QRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890, .-;:_!"#%&/()=?@${[]}''' TARGET = "Learn at codespeedy"

Now, we will create a class named “Individual” to represent an individual in the population. Different functions have been generated. We used ‘mutated_genes’ to create random genes for mutation, ‘create_gnome’ for creating chromosome or string of genes, and ‘mate’ to perform mating and producing new offspring. We have also used ‘cal_fitness’ to calculate the fitness score which compares the given chromosome with TARGET string.

In ‘mate’ function, we have used variable child_chromosome to symbolize chromosome of off spring(new string). We have used a probability measure 0.45 as limit for insertion of parent 1 and 0.45 to 0.90 for parent 2 for mating.

class Individual(object): def __init__(self, chromosome): self.chromosome = chromosome self.fitness = self.cal_fitness() @classmethod def mutated_genes(self): global Gen_Mat gene = random.choice(Gen_Mat) return gene @classmethod def create_gnome(self): global TARGET gnome_len = len(TARGET) return [self.mutated_genes() for _ in range(gnome_len)] def mate(self, par2): child_chromosome = [] for gp1, gp2 in zip(self.chromosome, par2.chromosome): prob = random.random() if prob < 0.45: child_chromosome.append(gp1) elif prob < 0.90: child_chromosome.append(gp2) else: child_chromosome.append(self.mutated_genes()) return Individual(child_chromosome) def cal_fitness(self): global TARGET fitness = 0 for gs, gt in zip(self.chromosome, TARGET): if gs != gt: fitness+= 1 return fitness

Now we have created an array named population for storing population (strings). Then using append function inserted the strings.

After it, we have sorted the population(string) in increasing order of their fitness score. Fitness Score=0 is used as the symbol of termination of the loop. Otherwise new population (string) is generated and is appended to the new_generation variable.

We have performed a check, where one with 10% of the fittest population goes to the next generation and 50% of the fittest population will be used for mating.

Then finally we have printed the results.

def main(): global POPUL_Size generation = 1 found = False population = [] for _ in range(POPUL_Size): gnome = Individual.create_gnome() population.append(Individual(gnome)) while not found: population = sorted(population, key = lambda x:x.fitness) if population[0].fitness <= 0: found = True break new_generation = [] s = int((10*POPUL_Size)/100) new_generation.extend(population[:s]) s = int((90*POPUL_Size)/100) for _ in range(s): parent1 = random.choice(population[:50]) parent2 = random.choice(population[:50]) child = parent1.mate(parent2) new_generation.append(child) population = new_generation print("Gen: {}\tSolution: {}\tFitness Score: {}". format(generation, "".join(population[0].chromosome), population[0].fitness)) generation += 1 print("Gen: {}\tSolution: {}\tFitness Score: {}". format(generation, "".join(population[0].chromosome), population[0].fitness)) if __name__ == '__main__': main()

#### Output:

Gen: 1 Solution: U8 #ZXOt?;a2ys0e9,Q Fitness Score: 16 Gen: 2 Solution: 9Ks)nZYbe"ode88b)y Fitness Score: 15 Gen: 3 Solution: LKsjnOIbV"ode88()e; Fitness Score: 14 Gen: 4 Solution: LKsjnOIbV"ode88()e; Fitness Score: 14 Gen: 5 Solution: L-RjwKIkV"ode$feei0 Fitness Score: 13 Gen: 6 Solution: L0s%wKIb?code80eei4 Fitness Score: 12 Gen: 7 Solution: L!X:n 06 coSe_Cb)dD Fitness Score: 11 Gen: 8 Solution: L!X:n 06 coSe_Cb)dD Fitness Score: 11 Gen: 9 Solution: L!X:n 06 coSe_Cb)dD Fitness Score: 11 Gen: 10 Solution: L-s%n Ik codeo0Qe]4 Fitness Score: 10 Gen: 11 Solution: L0s,n I8 code$0eedb Fitness Score: 8 Gen: 12 Solution: L0s,n I8 code$0eedb Fitness Score: 8 Gen: 13 Solution: L0s,n I8 code$0eedb Fitness Score: 8 Gen: 14 Solution: L0s,n I8 code$0eedb Fitness Score: 8 Gen: 15 Solution: Lpsrn o codeu0eedL Fitness Score: 7 Gen: 16 Solution: Lpsrn o codeu0eedL Fitness Score: 7 Gen: 17 Solution: Lpsrn o codeu0eedL Fitness Score: 7 Gen: 18 Solution: L0!rn I4 code$peed; Fitness Score: 6 Gen: 19 Solution: L0!rn I4 code$peed; Fitness Score: 6 Gen: 20 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 21 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 22 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 23 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 24 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 25 Solution: LYarn 54 code$peedD Fitness Score: 5 Gen: 26 Solution: Learn I4 codeopRedy Fitness Score: 4 Gen: 27 Solution: Learn I4 codeopRedy Fitness Score: 4 Gen: 28 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 29 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 30 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 31 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 32 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 33 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 34 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 35 Solution: L,arn a4 code$peedy Fitness Score: 3 Gen: 36 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 37 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 38 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 39 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 40 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 41 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 42 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 43 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 44 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 45 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 46 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 47 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 48 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 49 Solution: Learn aH code(peedy Fitness Score: 2 Gen: 50 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 51 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 52 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 53 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 54 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 55 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 56 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 57 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 58 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 59 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 60 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 61 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 62 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 63 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 64 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 65 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 66 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 67 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 68 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 69 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 70 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 71 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 72 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 73 Solution: Learn a4 codespeedy Fitness Score: 1 Gen: 74 Solution: Learn at codespeedy Fitness Score: 0

## Leave a Reply