How to incrementally sample without replacement?

前端 未结 13 1604
别跟我提以往
别跟我提以往 2020-12-05 00:38

Python has my_sample = random.sample(range(100), 10) to randomly sample without replacement from [0, 100).

Suppose I have sampled n

13条回答
  •  情深已故
    2020-12-05 01:16

    You can implement a shuffling generator, based off Wikipedia's "Fisher--Yates shuffle#Modern method"

    def shuffle_gen(src):
        """ yields random items from base without repetition. Clobbers `src`. """
        for remaining in xrange(len(src), 0, -1):
            i = random.randrange(remaining)
            yield src[i]
            src[i] = src[remaining - 1]
    

    Which can then be sliced using itertools.islice:

    >>> import itertools
    >>> sampler = shuffle_gen(range(100))
    >>> sample1 = list(itertools.islice(sampler, 10))
    >>> sample1
    [37, 1, 51, 82, 83, 12, 31, 56, 15, 92]
    >>> sample2 = list(itertools.islice(sampler, 80))
    >>> sample2
    [79, 66, 65, 23, 63, 14, 30, 38, 41, 3, 47, 42, 22, 11, 91, 16, 58, 20, 96, 32, 76, 55, 59, 53, 94, 88, 21, 9, 90, 75, 74, 29, 48, 28, 0, 89, 46, 70, 60, 73, 71, 72, 93, 24, 34, 26, 99, 97, 39, 17, 86, 52, 44, 40, 49, 77, 8, 61, 18, 87, 13, 78, 62, 25, 36, 7, 84, 2, 6, 81, 10, 80, 45, 57, 5, 64, 33, 95, 43, 68]
    >>> sample3 = list(itertools.islice(sampler, 20))
    >>> sample3
    [85, 19, 54, 27, 35, 4, 98, 50, 67, 69]
    

提交回复
热议问题