I am looking for efficient way of implementing uniform crossover in numpy pandas. Every solution consists of numpy array and a number:
population = pd.DataFrame({
"mask": [get_random_genotype() for _ in range(pop_size)]
"X": [np.random.random() for _ in range(pop_size)]})
I would like to do parallel uniform crossover of chosen sub-population, eg.:
pairs = np.array([[0, 2],[1, 3]])
for male, female in pairs:
mask = random_mask() #[True, False, False, True]
new_male.mask= where(mask, male, female)
new_female.mask = where(mask, female, male)
but in compeletly parallel manner. I have already tried:
temp: pd.DataFrame = population.copy()
draw: np.ndarray = np.random.choice(
a = [True, False],
size = np.stack(temp["mask"][pairs[X, 0]]).shape,
)
population.loc[pairs[X, 0], "mask"] = pd.Series(np.where(draw, np.stack(temp["mask"][pairs[X, 0]]), np.stack(temp["mask"][pairs[X, 1]])).tolist())
population.loc[pairs[X, 1], "mask"] = pd.Series(np.where(draw, np.stack(temp["mask"][pairs[X, 1]]), np.stack(temp["mask"][pairs[X, 0]])).tolist())
but it didn't work, apparently some of my masks became Nan's I have no idea whether I should solve it this way. I think solution that works in the same way on float/int instead of array would be sufficient as well:
X = pd.DataFrame({"x":[x//2 for x in range(10)]})
mask = [True, False, False, True]
X.loc[[1,2,3,4], "x"] = pd.Series(np.where(mask,X.loc[[1,2,3,4], "x"], X.loc[[5,6,7,8], "x"]).tolist(), dtype = int)
Nan's are still appearing.