我有一个索引列表,例如0 ... 365,并且我想选择此列表的连续子区域,这些索引是随机选择的,无需替换。
index = [i+1 for i in range(365) ]
#n could be 3
for i in range(n):
exclusion_regions.append( get_random_contiguous_region(index) )
是否有人对实现get_random_contiguous_region()有建议?
我们需要一个while循环来确保我们不会重叠,并且您可以使用list comp来检查切片的长度是否符合任何其他条件,因此您无法指定其他条件:如果您希望随机切片的比例为5%到15%列表大小的总和样本量的30%左右:
from random import choice
from numpy import arange
index = [i + 1 for i in range(365)]
choices = []
seen = set()
ar = arange(0.05,.16, .01)
ln = len(index)
sample_size = 0
while sample_size < ln * .30:
perc = choice(ar) # get random 5, 10, 15 percent slices
size = int(ln * perc)
ch = choice(index[:-size+1]) # avoid falling off the side
rn = index[ch:ch+size]
if len(rn) == size and not seen.intersection(rn):
seen.update(rn)
choices.append(rn)
sample_size += len(rn)
print(choices)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句