对列表按概率采样

  • 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. mysql 5.5 安装教程

    (转自:https://www.cnblogs.com/solargen/p/6835399.html) 1. 官网下载mysql5.5 下载地址: http://dev.mysql.com/down ...

  2. 13 oracle数据库坏块-逻辑坏块(模拟/修复)

    13 oracle数据库坏块-逻辑坏块 逻辑数据坏块的场景1)oracle bug也可能导致逻辑坏块的产生. 特别是parallel dml. 例如:Bug 5621677 Logical corru ...

  3. 【SSH】---【Struts2、Hibernate5、Spring4集成开发】【SSH框架整合笔记】

    Struts2.Hibernate5.Spring4集成开发步骤: 一.导入Jar包(基本的大致有41个,根据实际项目的需求自己添加) antlr-2.7.7.jar aopalliance.jar ...

  4. Java基础/时间日期格式

    Java时间日期格式转换 一.Date转String和String转Date 参考博客:https://www.cnblogs.com/sharpest/p/7879377.html public s ...

  5. python深度学习培训概念整理

    对于公司组织的人工智能学习,每周日一天课程共计五周,已经上了三次,一天课程下来讲了两本书的知识.发现老师讲的速度太快,深度不够,而且其他公司学员有的没有接触过python知识,所以有必要自己花时间多看 ...

  6. [Git] 023 Re:从零开始的 rebase 命令

    1. 开门见山 我新建了一个本地仓库,并进行了一些操作 当前情况 查看(直观但不明了) 上图的第二条 "log" 命令详见 [Git] 024 log 命令的补充 的 " ...

  7. B - 卿学姐与基本法 (离散化+成段更新+区间求和)

    卿学姐与基本法 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...

  8. 我国三大常用坐标系:北京54、西安80和WGS-84

    转自:http://blog.sina.com.cn/s/blog_6dbe2d780100mwr5.html 我国三大常用坐标系:北京54.西安80和WGS-84 1.北京54坐标系(BJZ54)北 ...

  9. 【React -- 5/100】 组件复用

    组件复用 React组件复用概述 思考:如果两个组件中的部分功能相似或相同,该如何处理? 处理方式:复用相似的功能 复用什么? state 操作state的方法 两种方式: render props模 ...

  10. HTML回顾之表单和列表

    FORM  HTML 表单 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中输入内容,比如:文本域(textarea).下拉列表.单选框(radio-buttons).复选框(checkbo ...