weighted choice in python

On the list by probability sampling

  • Input: a collection C of elements and a probability distribution p over C;
  • Output: an element chosen at random from C according to p.

C has n elements, 1-n, the probability (p = (p [1], ..., p [n]). We only random.random () function, it will give us a uniform distribution [0,1 a float on]. the basic idea is to split [0,1] into n segments of length p [1] ... p [n] (Σ p [i] = 1). If uniformly in [0, 1] on the dot, the probability that it stopped in the i-th segment is p [i]. Thus can be implemented by random.random () function. Check stop place [0,1] which position, then return it that segment index where the python implemented as follows:

ref: https://scaron.info/blog/python-weighted-choice.html

On the list by probability sampling

import random
import collections

def weighted_choice(seq, weights):
    assert len(weights) == len(seq)
    assert abs(1. - sum(weights)) < 1e-6

    x = random.random()
    for i, elmt in enumerate(seq):
        if x <= weights[i]:
            return elmt
        x -= weights[i]

def gen_weight_list(seq, gt_set, incline_ratio):
    '''
    :param seq:
    :param gt_list:
    :param incline_ratio:
    :return:
    seqe = [1,2,3,4,5]
    gt_list = [3,5,7]
    # incline_ratio = 0.9   # allocate this num of prob for random select gt's in sequence
    '''
    len_seq = len(seq)
    # programmatic gen the prob list:
    prob_list = []
    gts_in_seq = [i for i in seq if i in gt_set]
    len_gts_in_seq = len(gts_in_seq)
    # item_ngt_in_seq = [i for i in seqe if i not in gt_list]
    if len_gts_in_seq > 0:
        prob_gt = incline_ratio/len_gts_in_seq
        prob_ngt = (1-incline_ratio)/(len_seq - len_gts_in_seq)
    else:
        prob_gt = 0
        prob_ngt = 1/len_seq

    for idx in range(len_seq):
        if seq[idx] in gts_in_seq:
            # prob_list[idx] = prob_gt
            prob_list.append(prob_gt)
        else:
            # prob_list[idx] = prob_ngt
            prob_list.append(prob_ngt)

    return prob_list

# add prob incline ratio for allocate heavier weight udr some conditions:
seqe = [1,2,3,4,5]
gt_set = set([3,5,7])    # conditions, if item in seq is also in this list, will be allocated higher weight.
inc_ratio = 0.8   # allocate this num of prob for random select gt's in sequence

prob = gen_weight_list(seqe, gt_set, inc_ratio)
select_seq = []

for i in range(10000):
    select_seq.append(weighted_choice(seqe, prob))

# count the item in select_seq:
select_seq.sort(reverse=True)     #optional?
item_Count = collections.Counter(select_seq)
print(item_Count)

Guess you like

Origin www.cnblogs.com/sonictl/p/11652848.html