对列表按概率采样

  • 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有n 个元素,1-n, 概率 (p = (p[1], ..., p[n])。 我们只有random.random()函数,它会给我们均匀分布的[0,1]上的一个float. 基本思想是分割[0,1]into n segments of length p[1] ... p[n] ( ∑ p[i] = 1) . 如果均匀地在[0,1]上打点,那它在第i个segment上停住的概率就是p[i]. 因此可以用random.random()函数来实现。查看停止的地方在[0,1]的哪个位置,然后返回其所在的那个segment index. python如下实现:

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

对列表按概率采样

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)

weighted choice in python的更多相关文章

  1. Python choice() 函数

    Python choice() 函数  Python 数字 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choice() 方法的语法: import random ...

  2. python之路五

    内建模块 time和datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现 ...

  3. Python 随机数生成总结

    random.uniform(a, b),返回[a,b]之间的浮点数 random.randint(a, b),返回[a,b]之间的整数 random.randrange([start], stop[ ...

  4. Python学习【第十一篇】模块(1)

    模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 简单地说,模块就是一个保 ...

  5. Python中的random模块,来自于Capricorn的实验室

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  6. python学习笔记-(九)模块

    基础知识 1. 定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑----实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块就是test) 包:用 ...

  7. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

  8. python学习之路-day5-模块

    本节内容: 模块详解 1.模块定义 2.os&sys模块 3.time&datetime模块 4.random模块 5.shutil模块 6.shelve模块 7.configpars ...

  9. Windows下python的配置

    Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...

随机推荐

  1. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  2. Hibernate一级缓冲

    Hibernate的一级缓冲 什么是缓冲 缓冲概念: 数据存在数据库中,数据库本身就是一个文件系统,使用流的方式操作文件,但是文件中有很多的内容,用流的操作得效率就低. 解决办法: 把数据存在内存中, ...

  3. c++获取当前进程所在位置

    char buffer[MAX_PATH]; GetModuleFileNameA(NULL, buffer, MAX_PATH ); string::size_type pos = string( ...

  4. 数据库之DML

    1.表的有关操作: 1.1.表的创建格式: CREATE TABLE IF NOT EXISTS 表名(属性1 类型,属性2 类型,....,属性n 类型):# 标记部分表示可以省略 1.2.表的修改 ...

  5. Lesson 2 Thirteen equals one

    vicar 牧师 grocer 杂货铺店主 with a start 由于受到惊吓 Whtaever are you dong up here?你究竟在这上面干什么?whatever用于疑问句中,用以 ...

  6. 解读Nodejs多核处理模块cluste

    http://blog.fens.me/nodejs-core-cluster/ Node.js开发框架Express4.x   http://blog.fens.me/nodejs-express4 ...

  7. 最全的DevOps自动化工具集合

    版本控制&协作开发:GitHub.GitLab.BitBucket.SubVersion.Coding.Bazaar 自动化构建和测试:Apache Ant.Maven .Selenium.P ...

  8. 通过QT查找Word中的关键字,并做高亮或删除操作

    最近由于项目需要,要用QT操作Word文档.具体的工作需求:在指定的Word文档(*.doc文件/*.docx文件)中查找关键字,找到后做高亮操作或者直接删除操作,然后另存为到别的目录(表示这个文件被 ...

  9. offsetWidth clientWidth scrollWidth 的区别

    了解 offsetWidth clientWidth scrollWidth 的区别 最近需要清除区分开元素的width,height及相应的坐标等,当前这篇用来区分offsetWidth clien ...

  10. 在java程序中利用线程

    package 第十一章; import java.awt.Button; import java.awt.Color; import java.awt.Font; import java.awt.F ...