Skip to content

Instantly share code, notes, and snippets.

@matanox
Last active September 28, 2018 21:29
Show Gist options
  • Save matanox/16035e3bcb478b7e7601a19623679277 to your computer and use it in GitHub Desktop.
Save matanox/16035e3bcb478b7e7601a19623679277 to your computer and use it in GitHub Desktop.
example of sampling from a distribution vector
def sample_from_distribution(distribution_vector):
''' samples from a provided distribution (e.g. for choosing an action from a VW returned distribution vector) '''
probabilities = list(map(float, distribution_vector))
actions = range(1,len(distribution_vector)+1) # VW actions are 1-indexed not zero indexed
assert(len(actions) == len(probabilities))
s = choices(actions, probabilities) # https://docs.python.org/3/library/random.html#random.choices
action = s[0]
probability = probabilities[actions.index(action)]
return (action, probability)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment