I'm trying to make my code more efficient. I'm not super familiar with vectorization. I can do simple ones but I'm having trouble wrapping my head around this one
def mate(self, parent1, parent2):
length = parent1.size
parent1 *= 1e6
parent2 *= 1e6
parent1 = parent1.astype(int)
parent2 = parent2.astype(int)
child1 = []
child2 = []
for i in range(length):
hold1 = parent1[i]
hold2 = parent2[i]
hold1 = np.binary_repr(hold1, 30)
hold2 = np.binary_repr(hold2, 30)
pivot = np.random.randint(0, length)
childGene1 = hold1[:pivot] + hold2[pivot:]
childGene2 = hold2[:pivot] + hold1[pivot:]
childGene1 = int(childGene1, 2) / 1e6
childGene2 = int(childGene2, 2) / 1e6
child1.append(childGene1)
child2.append(childGene2)
return [child1, child2]
It's a mating function I made for a genetic algorithm. How should I go about vectorizing this loop?