如何在python中draw samples
如何在python中draw samples
https://stackoverflow.com/questions/10803135/weighted-choice-short-and-simple/15907274
[Youtube videos tutorial]https://www.youtube.com/watch?v=KzqSDvzOFNA&pbjreload=101&ab_channel=CoreySchafer
Since Python 3.6 there is a method
choices
from the
random
module.
import random
random.choices(population = [['a','b'], ['b','a'], ['c','b']],
weights = [0.2, 0.2, 0.6],
k = 10)
import random
random.choices(['one', 'two', 'three'], [0.2, 0.3, 0.5], k=10)
也可以用numpy模块
numpy.random.choice(items, trials, p = probs)
Since
numpy version 1.7 you can use
numpy.random.choice()
:
elements = ['one', 'two', 'three']
weights = [0.2, 0.3, 0.5]
from numpy.random import choice
print(choice(elements, p = weights))
通过这个语句可以按照一定的概率选取一个或几个元素,用来控制强化学习任务中的输钱和赢钱的
random.choices(population = ['H', 'T'], weights = [0.3, 0.7], k = 1)
Note that random.choices
will sample with replacement, per the
docs:
https://pynative.com/python-random-choice/
Use random.choice() to Randomly select an item from a list
import random
movie_list = ['The Godfather', 'The Wizard of Oz', 'Citizen Kane', 'The Shawshank Redemption', 'Pulp Fiction']
moview_item = random.choice(movie_list)
print ("Randomly selected item from list is - ", moview_item)
https://pynative.com/python-weighted-random-choices-with-probability/
random.choices()
Python 3.6 introduced a new function
choices() in the
random module. By using random.choices()
we can make a weighted random choice with replacement. You can also call it a weighted
random sample with replacement. Let’s have a look into the syntax of this function.