I would like to select a random subset of indices from a numpy array with the caveat that I need each random selection to contain at least three indices in a row.
For example, if I have an array that contains 25 items
a = np.arange(0,25)
I want to make sure that no index is selected without including at least two neighboring indices. So, for example, if I was looking for a subset of length 12, the following two options both fulfill this.
rand_subset_1 = [0,1,2,9,10,11,12,13,18,19,20,21]
rand_subset_2 = [3,4,5,6,7,8,14,15,16,22,23,24]
Attempted Answer
I tried to figure this out initially by dividing a
into lists of three.
a_mod = np.array([0,1,2],[3,4,5],[6,7,8],...[21,22,23])
and then using np.random.choice(a_mod, subset_length/3, replace=False)
However this doesn't solve my problem, for two reasons.
- I want to be able to input arrays with lengths that don't have to be divisible by three.
- I don't mind if the subset indices are in cluster sizes that also aren't divisible by three. I just need the cluster to have at least three consecutive indices.
Thanks in advance for any help with this problem!
Aucun commentaire:
Enregistrer un commentaire