A weighted version of random.choice

后端 未结 25 1934
闹比i
闹比i 2020-11-21 06:29

I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with:



        
25条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:43

    Since Python 3.6 there is a method choices from the random module.

    Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.0.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: import random
    
    In [2]: random.choices(
    ...:     population=[['a','b'], ['b','a'], ['c','b']],
    ...:     weights=[0.2, 0.2, 0.6],
    ...:     k=10
    ...: )
    
    Out[2]:
    [['c', 'b'],
     ['c', 'b'],
     ['b', 'a'],
     ['c', 'b'],
     ['c', 'b'],
     ['b', 'a'],
     ['c', 'b'],
     ['b', 'a'],
     ['c', 'b'],
     ['c', 'b']]
    

    Note that random.choices will sample with replacement, per the docs:

    Return a k sized list of elements chosen from the population with replacement.

    Note for completeness of answer:

    When a sampling unit is drawn from a finite population and is returned to that population, after its characteristic(s) have been recorded, before the next unit is drawn, the sampling is said to be "with replacement". It basically means each element may be chosen more than once.

    If you need to sample without replacement, then as @ronan-paixão's brilliant answer states, you can use numpy.choice, whose replace argument controls such behaviour.

提交回复
热议问题