A weighted version of random.choice

后端 未结 25 1869
闹比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 07:01

    Another way of doing this, assuming we have weights at the same index as the elements in the element array.

    import numpy as np
    weights = [0.1, 0.3, 0.5] #weights for the item at index 0,1,2
    # sum of weights should be <=1, you can also divide each weight by sum of all weights to standardise it to <=1 constraint.
    trials = 1 #number of trials
    num_item = 1 #number of items that can be picked in each trial
    selected_item_arr = np.random.multinomial(num_item, weights, trials)
    # gives number of times an item was selected at a particular index
    # this assumes selection with replacement
    # one possible output
    # selected_item_arr
    # array([[0, 0, 1]])
    # say if trials = 5, the the possible output could be 
    # selected_item_arr
    # array([[1, 0, 0],
    #   [0, 0, 1],
    #   [0, 0, 1],
    #   [0, 1, 0],
    #   [0, 0, 1]])
    

    Now let's assume, we have to sample out 3 items in 1 trial. You can assume that there are three balls R,G,B present in large quantity in ratio of their weights given by weight array, the following could be possible outcome:

    num_item = 3
    trials = 1
    selected_item_arr = np.random.multinomial(num_item, weights, trials)
    # selected_item_arr can give output like :
    # array([[1, 0, 2]])
    

    you can also think number of items to be selected as number of binomial/ multinomial trials within a set. So, the above example can be still work as

    num_binomial_trial = 5
    weights = [0.1,0.9] #say an unfair coin weights for H/T
    num_experiment_set = 1
    selected_item_arr = np.random.multinomial(num_binomial_trial, weights, num_experiment_set)
    # possible output
    # selected_item_arr
    # array([[1, 4]])
    # i.e H came 1 time and T came 4 times in 5 binomial trials. And one set contains 5 binomial trails.
    

提交回复
热议问题