Python中的加权随机

Python实现加权随机选择
本文介绍了在Python中实现加权随机选择的多种方法,包括简单线性法、加速搜索、去掉临时变量、预计算累积权重和使用`itertools.accumulate`函数。详细对比了不同方法的效率,并给出了优化建议。

我们平时比较多会遇到的一种情景是从一堆的数据中随机选择一个, 大多数我们使用random就够了, 但是假如我们要选取的这堆数据分别有自己的权重, 也就是他们被选择的概率是不一样的, 在这种情况下, 就需要使用加权随机来处理这些数据

1. 简单线性方法

下面是一种简单的方案, 传入权重的列表(weights), 然后会返回随机结果的索引值(index), 比如我们传入[2, 3, 5], 那么就会随机的返回0(概率0.2), 1(概率0.3), 2(概率0.5)

简单的思路就是把所有的权重加和, 然后随机一个数, 看看落在哪个区间

import random

def weighted_choice(weights):
    totals = []
    running_total = 0

    for w in weights:
        running_total += w
        totals.append(running_total)

    rnd = random.random() * running_total
    for i, total in enumerate(totals):
        if rnd < total:
            return i

2. 加速搜索

上面这个方法看起来非常简单, 已经可以完成我们所要的加权随机, 然是最后的这个for循环貌似有些啰嗦, Python有个内置方法bisect可以帮我们加速这一步

import random
import bisect

def weighted_choice(weights):
    totals = []
    running_total = 0

    for w in weights:
        running_total += w
        totals.append(running_total)

    rnd = random.random() * running_total
    return bisect.bisect_right(totals, rnd)

bisect方法可以帮我们查找rndtotals里面应该插入的位置, 两个方法看起来差不多, 但是第二个会更快一些, 取决于weights这个数组的长度, 如果长度大于1000, 大约会快30%左右

3. 去掉临时变量

其实在这个方法里面totals这个数组并不是必要的, 我们调整下策略, 就可以判断出weights中的位置

def weighted_choice(weights):
  rnd = random.random() * sum(weights)
  for i, w in enumerate(weights):
      rnd -= w
      if rnd < 0:
          return i

这个方法比第二种方法竟然快了一倍, 当然, 从算法角度角度, 复杂度是一样的, 只不过我们把赋值临时变量的功夫省下来了, 其实如果传进来的weights是已经按照从大到小排序好的话, 速度会更快, 因为rnd递减的速度最快(先减去最大的数)

4. 更多的随机数

如果我们使用同一个权重数组weights, 但是要多次得到随机结果, 多次的调用weighted_choice方法, totals变量还是有必要的, 提前计算好它, 每次获取随机数的消耗会变得小很多

class WeightedRandomGenerator(object):
    def __init__(self, weights):
        self.totals = []
        running_total = 0

        for w in weights:
            running_total += w
            self.totals.append(running_total)

    def next(self):
        rnd = random.random() * self.totals[-1]
        return bisect.bisect_right(self.totals, rnd)

    def __call__(self):
        return self.next()

在调用次数超过1000次的时候, WeightedRandomGenerator的速度是weighted_choice的100倍

所以我们在对同一组权重列表进行多次计算的时候选择方法4, 如果少于100次, 则使用方法3

5. 使用accumulate

python3.2之后, 提供了一个itertools.accumulate方法, 可以快速的给weights求累积和

>>>> from itertools import accumulate
>>>> data  = [2, 3, 5, 10]
>>>> list(accumulate(data))
[2, 5, 10, 20]

如果你有更好的方法, 欢迎在留言区讨论

参考文章: Weighted random generation in Python

本文发表在致趣技术团队博客, 加入致趣

### Python中实现加权随机选择 在Python中,可以采用同方式来实现加权随机选择。一种常见的方式是通过计算累积权重并利用`random.uniform()`函数生成一个介于0和总权重之间的随机数来进行选择[^1]。 ```python import random def weighted_random_choice(choices): # 计算累积权重 cum_weights = [] cum_weight = 0 for _, weight in choices: cum_weight += weight cum_weights.append(cum_weight) # 获总的权重值用于后续比较 total_cum_weight = cum_weight # 生成一个位于0至总权重间的随机浮点数 random_num = random.uniform(0, total_cum_weight) # 查找第一个累加权重超过此随机数值的位置即为目标选项 for (choice, _), cum_weight in zip(choices, cum_weights): if random_num < cum_weight: return choice # 若未找到匹配项则返回None作为默认处理 return None ``` 另一种更简洁的方法是在较新的Python版本(3.2及以上)使用内置库`itertools.accumulate`配合二分查找算法加快定位过程[^4]: ```python from itertools import accumulate import bisect import random def weighted_random_pick(options, weights): cumulative_weights = list(accumulate(weights)) rand_val = random.random() * cumulative_weights[-1] index_picked = bisect.bisect_left(cumulative_weights, rand_val) return options[index_picked] if __name__ == '__main__': items = ['apple', 'banana', 'cherry'] item_weights = [5, 2, 3] picked_item = weighted_random_pick(items, item_weights) print(f"Picked Item: {picked_item}") ``` 上述两种方法均能有效地完成基于给定权重的概率分布下的元素选操作。前者适用于所有支持的Python环境,而后者借助了新特性使得代码更加紧凑高效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值