vendredi 25 mai 2018

Python 3: Selecting random pair elements from a numpy array without repetition

I want to select random pair elements from a population array in each loop without repetition.

In my code, I create a population array then I pass it to the selection function where I will generate two random index numbers in each loop, and based on it I will select the pair elements from the population array. so if I have a population size 5, then my result size will be 10, because in each loop I will get two elements.

import random
import numpy as np
population_size = 5
dimension= 2
upper = 1
lower = 0
pair_loop = 1
pair_size = 2 

def select (pop
    parents = np.zeros((0, dimension), dtype=np.float_)

    for i in range (population_size):

        for ii in range (pair_loop): 

            random_index= np.random.randint (population_size, size=(pair_size))
            parents = np.vstack((parents, population[random_index,]))
            print (i ,"random_index", random_index)
            print (parents)

    return (parents)

population = np.random.choice(np.linspace(lower, upper, 10), size=(population_size,dimension), replace=True)    
parents = select(population)

I want to get two different elements, where I don't want to repeat the same index number,

for example, if I have: [2, 4] I don't want to see the: [2, 4] or [4,2] again

Any advice would be much appreciated




Aucun commentaire:

Enregistrer un commentaire