• 算法题---按概率抽样获取相应的样本


    """
    按概率抽样
    [0.1, 0.5, 0.4] 给出这个概率数组,对每个概率打上label
    0 - 0.1
    1 - 0.5
    2 - 0.4
    求按照上述比例,获取相应概率的label
    """
    import random
    import logging
    import bisect
    
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S',)
    
    
    class Sampling(object):
        def __init__(self, array_prob):
            self.source_array = array_prob
    
        def output_label(self):
            tmp = 0
            new_array = []
            for num in self.source_array:
                tmp += num
                new_array.append(tmp)
            new_array.insert(0, 0)
            rand_num = random.random()
            logging.info("random num is %s" % rand_num)
            length = len(new_array)
            for index in range(0, length-1):
                left = new_array[index]
                right = new_array[index + 1]
                if left <= rand_num <= right:
                    return index
    
        def output_label2(self):
            tmp = 0
            new_array = []
            for num in self.source_array:
                tmp += num
                new_array.append(tmp)
            new_array.insert(0, 0)
            ret = bisect.bisect_left(new_array, random.random())
            return ret
    
    
    if __name__ == "__main__":
        s = Sampling([0.1, 0.5, 0.4])
        print(s.output_label())
  • 相关阅读:
    第03组 Alpha冲刺(2/4)
    第03组 Alpha冲刺
    第09组 Beta版本演示
    第09组 Beta冲刺(4/4)
    第09组 Beta冲刺(3/4)
    第09组 Beta冲刺(2/4)
    第09组 Beta冲刺(1/4)
    第09组 Alpha事后诸葛亮
    第09组 Alpha冲刺(4/4)
    第09组 Alpha冲刺(3/4)
  • 原文地址:https://www.cnblogs.com/syw-home/p/13930151.html
Copyright © 2020-2023  润新知