weighted choice in python
对列表按概率采样
- 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的更多相关文章
- Python choice() 函数
Python choice() 函数 Python 数字 描述 choice() 方法返回一个列表,元组或字符串的随机项. 语法 以下是 choice() 方法的语法: import random ...
- python之路五
内建模块 time和datetime 在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素.由于Python的time模块实现 ...
- Python 随机数生成总结
random.uniform(a, b),返回[a,b]之间的浮点数 random.randint(a, b),返回[a,b]之间的整数 random.randrange([start], stop[ ...
- Python学习【第十一篇】模块(1)
模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 简单地说,模块就是一个保 ...
- Python中的random模块,来自于Capricorn的实验室
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- python学习笔记-(九)模块
基础知识 1. 定义 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑----实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块就是test) 包:用 ...
- Python自动化 【第五篇】:Python基础-常用模块
目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...
- python学习之路-day5-模块
本节内容: 模块详解 1.模块定义 2.os&sys模块 3.time&datetime模块 4.random模块 5.shutil模块 6.shelve模块 7.configpars ...
- Windows下python的配置
Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...
随机推荐
- zabbix添加主机后无法显示解决
第一次添加主机后显示正常,后来删除了主机,重新添加了一下主机再也无法显示主机,很苦恼,原来需要点击重设,
- c++ 加载资源文件
int _tmain(int argc, _TCHAR* argv[]) { HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(IDR_DATA1), ...
- 求一个集合S中m个元素的所有排列以及一个数组A的全排列—递归实现版完整代码
说明,本文全文代码均用dart语言实现. 求一个集合S中m个元素的所有排列情况,并打印,非常适合用递归的思路实现.本文给出了两种实现方法,一种是给定的填充排列数组长度是固定的,一种是可变长度的.两种方 ...
- rename批量修改文件名
批量改名: 如文件,批量修改,把hello去掉[root@localhost wang]# ll-rw-r--r-- 1 root root 0 5月 14 02:36 a.hello.txt-rw- ...
- SpringBoot 单元测试junit test
pom引用 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- 防抖和节流 lodash插件
lodash.debounce lodash.debounce(function(){ },1000) 函数防抖原理 调用函数时,马上清理定时器.然后再设置一个定时器包含函数
- 五、JVM — 类加载器
回顾一下类加载过程 类加载器总结 双亲委派模型 双亲委派模型介绍 双亲委派模型实现源码分析 双亲委派模型的好处 如果我们不想要双亲委派模型怎么办? 自定义类加载器 推荐 回顾一下类加载过程 类加载过程 ...
- @Transactional实现原理
Transactional是spring中定义的事务注解,在方法或类上加该注解开启事务.主要是通过反射获取bean的注解信息,利用AOP对编程式事务进行封装实现.AOP对事务的封装可以看我的这篇文章的 ...
- linux基本命令vi和vim使用详细介绍
vi使用方法详细介绍 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版 ...
- arcgis server10.2自带打印模板路径
找到arcgis server10.2安装目录路径,我的安装路径为C盘,如下: C:\Program Files\ArcGIS\Server\Templates\ExportWebMapTemplat ...