dimanche 3 mai 2020

Draw specified number of sample from list. Use all list elements

I have a list of elements. Now I want to specify the number of draws/samples I take from this list. However, I must ensure that all samples together include all original elements. Also, the sample sizes should not be the same for each sample.

Example:

list = [1,2,3,4,5,6,7,8,9,10]
draws = 4
samples = some_function(draws,list)
set(tuple(row) for row in sample) == set(list) # must be true

samples =[[1,2,3],[4,5],[6,7,8],[9,10]] # 4 draws, together include all elements, two different sample sizes

Question: is there an easy way to do this using e.g. numpy.random?**

Here is one attempt using np.random.permutation and np.random.choice. However, this approach does not always have all list elements in the final samples.

srch_list = list(range(100))
draws = 10
mid = round(len(srch_list)/draws)
n_leafs = range(mid-2,mid+3)

rnd_list = np.random.permutation(srch_list)
leafs = []
for i in range(draws):
    idx = np.random.choice(n_leafs)
    leafs.append(rnd_list[:idx])
    rnd_list = rnd_list[idx:]





Aucun commentaire:

Enregistrer un commentaire