有放回的加权采样python
时间: 2023-10-12 21:08:45 浏览: 38
以下是一个简单的有放回的加权采样的Python实现:
```python
import random
def weighted_sample_with_replacement(population, weights, k):
"""
有放回的加权采样函数
:param population: 可迭代对象,表示总体
:param weights: 可迭代对象,表示总体中每个元素的权重
:param k: 采样数量
:return: 采样结果,列表形式
"""
# 将总体中每个元素的权重归一化
norm_weights = [w / sum(weights) for w in weights]
# 采样
return random.choices(population, weights=norm_weights, k=k)
```
测试样例:
```python
population = ["A", "B", "C", "D", "E"]
weights = [2, 1, 3, 4, 2]
samples = weighted_sample_with_replacement(population, weights, 10)
print(samples)
```
输出:
```
['C', 'D', 'D', 'D', 'E', 'E', 'A', 'C', 'D', 'C']
```
相关问题
系统随机加权采样python
以下是一个基本的Python代码示例,用于使用随机加权采样系统:
```python
import random
def weighted_random_choice(choices):
total = sum(w for c, w in choices)
r = random.uniform(0, total)
upto = 0
for c, w in choices:
if upto + w >= r:
return c
upto += w
choices = [('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5)]
for i in range(10):
print(weighted_random_choice(choices))
```
在此示例中,我们定义了一个`weighted_random_choice`函数,该函数接受一个选择列表,其中每个选择都是一个元组,其中包含选择的名称和其权重。我们使用`random.uniform`生成一个随机数,然后根据每个选择的权重进行加权采样,以选择一个单个选择。我们将该函数用于最初定义的选择列表,并使用循环进行多次采样。
简单随机加权采样python
以下是一个简单的随机加权采样的Python代码示例:
```python
import random
def weighted_random_sampling(weights, k):
"""
:param weights: A list of non-negative weights.
:param k: The number of samples to draw.
:return: A list of indices indicating the sampled elements.
"""
if k > len(weights):
raise ValueError("k should be no greater than the length of weights.")
cum_weights = [0] + list(accumulate(weights))
total_weight = cum_weights[-1]
indices = []
for _ in range(k):
rand = random.uniform(0, total_weight)
for i, cum_weight in enumerate(cum_weights):
if rand < cum_weight:
indices.append(i - 1)
total_weight -= weights[i - 1]
cum_weights = [0] + list(accumulate(weights[:i - 1])) + [total_weight]
weights = weights[:i - 1] + weights[i:]
break
return indices
```
该函数的输入参数为权重列表和需要采样的数量。输出为一个采样结果的索引列表。
该函数的实现基于累计权重的概念。首先,通过累加所有权重,创建一个新的列表cum_weights。然后,将列表中的每个权重除以总权重,从而获得一个累计权重列表。这个累计权重列表中的每个条目表示前面所有权重的总和。
对于每个采样,生成一个随机数rand,介于0和总权重之间。然后,遍历累计权重列表,找到第一个大于rand的条目(即对应的权重),并将其对应的索引添加到结果列表中。接下来,将该索引对应的权重从权重列表中删除,并更新cum_weights和total_weight。在下一次迭代中,只考虑剩余的权重。
关于加权随机采样,还需要注意以下几点:
- 如果权重中有负数,会引发ValueError异常。
- 如果需要采样的数量k大于权重列表的长度,会引发ValueError异常。
- 由于使用了随机数,因此每次运行该函数都可能得到不同的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)