Python(包括其包Numpy)中包含了了许多概率算法,包括基础的随机采样以及许多经典的概率分布生成。我们这个系列介绍几个在机器学习中常用的概率函数。先来看最基础的功能——随机采样。

1. random.choice

如果我们只需要从序列里采一个样本(所有样本等概率被采),只需要使用random.choice即可:

import random
res1 = random.choice([0, 1, 2, 3, 4])
print(res1) # 3

2. random.choices(有放回)

当然,很多时候我们不只需要采一个数,而且我们需要设定序列中每一项被采的概率不同。此时我们可以采用random.random.choices函数, 该函数用于有放回的(即一个数据项可以被重复采多次)对一个序列进行采样。其函数原型如下:


random.choices(population, weights=None, *, cum_weights=None, k=1)

population: 欲采样的序列
weights: 每个样本被赋予的权重(又称相对权重),决定每个样本被采的概率,如[10, 0, 30, 60, 0]
cum_weights: 累积权重,相对权重[10, 0, 30, 60, 0]相当于累积权重[10, 10, 40, 100, 100]


我们从[0, 1, 2, 3, 4]中按照相对权重采样3个样本如下:

res2 = random.choices([0, 1, 2, 3, 4], weights=[10, 0, 30, 60, 0], k=3)
# 注意population不是关键字参数,在函数调用时不能写成population=[0,1,2,3,4]来传参
# 关于关键字参数和位置参数,可以参看我的博客《Python技法2:函数参数的进阶用法》https://www.cnblogs.com/orion-orion/p/15647408.html
print(res2) # [3, 3, 2]

[0, 1, 2, 3, 4]中按照累积权重采样3和样本如下:

res3 = random.choices([0, 1, 2, 3, 4], cum_weights=[10, 10, 40, 100, 100], k=3)
print(res3) # [0, 3, 3]

注意,相对权重weights和累计权重cum_weights不能同时传入,否则会报TypeError异常'Cannot specify both weights and cumulative weights'

3. numpy.sample(无放回)

random.sample是无放回,如果我们需要无放回采样(即每一项只能采一次),那我们需要使用random.sample。需要注意的是,如果使用该函数,将无法定义样本权重。该函数原型如下:


random.sample(population, k, *, counts=None)¶

population: 欲采样的序列
k: 采样元素个数
counts: 用于population是可重复集合的情况,定义集合元素的重复次数。sample(['red', 'blue'], counts=[4, 2], k=5)等价于sample(['red', 'red', 'red', 'red', 'blue', 'blue'], k=5)


我们无放回地对序列[0, 1, 2, 3, 4]采样3次如下:

res3 = random.sample([0, 1, 2, 3, 4], k=3)
print(res3) # [3, 2, 1]

无放回地对可重复集合[0, 1, 1, 2, 2, 3, 3, 4]采样3次如下:

res4 = random.sample([0, 1, 2, 3, 4], k=3, counts=[1, 2, 2, 2, 1])
print(res4) # [3, 2, 2]

如果counts长度和population序列长度不一致,会抛出异常ValueError:"The number of counts does not match the population"

4.rng.choicesrng.sample

还有一种有放回采样实现方法是我在论文[1]的代码[2]中学习到的。即先定义一个随机数生成器,再调用随机数生成器的choices方法或sample方法,其使用方法和random.choice/random.sample函数相同。

rng_seed = 1234
rng = random.Random(rng_seed)
res5 = rng.choices(
     population=[0,1,2,3,4],
     weights=[0.1, 0, 0.3, 0.6, 0],
     k=3,
)
print(res5) # [3, 3, 0]

res6 = rng.sample(
     population=[0, 1, 2, 3, 4],
     k=3,
)
print(res6) # [4, 0, 2]

这两个函数在论文[1]的实现代码[2]中用来随机选择任务节点client

 def sample_clients(self):
        """
        sample a list of clients without repetition

        """
        rng_seed = (seed if (seed is not None and seed >= 0) else int(time.time()))
        self.rng = random.Random(rng_seed)

        if self.sample_with_replacement:
            self.sampled_clients = 
                self.rng.choices(
                    population=self.clients,
                    weights=self.clients_weights,
                    k=self.n_clients_per_round,
                )
        else:
            self.sampled_clients = self.rng.sample(self.clients, k=self.n_clients_per_round)

5. numpy.random.choices

从序列中按照权重分布采样也可以采用numpy.random.choice实现。其函数原型如下:

random.choice(a, size=None, replace=True, p=None)

a: 1-D array-like or int   如果是1-D array-like,那么样本会从其元素中抽取。如果是int,那么样本会从np.arange(a)中抽取;
size: int or tuple of ints, optional   为输出形状大小,如果给定形状为((m, n, k)),那么(mtimes ntimes k)的样本会从中抽取。默认为None,即返回一个单一标量。
replace: boolean, optional   表示采样是又放回的还是无放回的。若replace=True,则为又放回采样(一个值可以被采多次),否则是无放回的(一个值只能被采一次)。
p: 1-D array-like, optional   表示a中每一项被采的概率。如果没有给定,则我们假定a中各项被采的概率服从均匀分布(即每一项被采的概率相同)。

[0,1,2,3,4,5]中重复/不重复采样3次如下:

import numpy as np
res1 = np.random.choice(5, 3, replace=True)
print(res1) # [1 1 4]

res2 = np.random.choice(5, 3, replace=False)
print(res2) # [2 1 4]

同样是[0,1,2,3,4,5]中重复/不重复采样3次,现在来看我们为每个样本设定不同概率的情况:

res3 = np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
print(res3)  # [2 3 3]

res4 = np.random.choice(5, 3, replace=False, p=[0.1, 0, 0.3, 0.6, 0])
print(res4) # [3 2 0]

参考文献

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/orion-orion/p/15647408.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!