与默认均匀分布相比,为什么 numpy.random.Generator.choice 在给定均匀分布下提供不同的结果(种子)? [英] Why does numpy.random.Generator.choice provides different results (seeded) with given uniform distribution compared to default uniform distribution?

查看:48
本文介绍了与默认均匀分布相比,为什么 numpy.random.Generator.choice 在给定均匀分布下提供不同的结果(种子)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的测试代码:

pop = numpy.arange(20)
rng = numpy.random.default_rng(1)
rng.choice(pop,p=numpy.repeat(1/len(pop),len(pop))) # yields 10
rng = numpy.random.default_rng(1)
rng.choice(pop) # yields 9

numpy 文档说:

与 a 中每个条目相关联的概率.如果没有给出,样本假设在 a 中的所有条目上均匀分布.

The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a.

我不知道有什么其他方法可以创建均匀分布,但是 numpy.repeat(1/len(pop),len(pop)).

I don't know of any other way to create a uniform distribution, but numpy.repeat(1/len(pop),len(pop)).

numpy 是否在使用其他东西?为什么?

Is numpy using something else? Why?

如果不是,设置分布如何影响种子?

If not, how does setting the distribution affects the seed?

分发和种子不应该是独立的吗?

Shouldn't the distribution and the seed be independent?

我在这里遗漏了什么?

推荐答案

分布不影响种子.详情如下:

The distribution doesn't affect the seed. Details as bellow:

我查看了源代码:numpy随机/_generator.pyx#L669

如果给定了 p,它将使用 rng.random 来获取一个随机值:

If p is given, it will use rng.random to get a random value:

import numpy

pop = numpy.arange(20)
seed = 1
rng = numpy.random.default_rng(seed)

# rng.choice works like bellow
rand = rng.random()
p = numpy.repeat(1/len(pop),len(pop))
cdf = p.cumsum()
cdf /= cdf[-1]
uniform_samples = rand
idx = cdf.searchsorted(uniform_samples, side='right')
idx = numpy.array(idx, copy=False, dtype=numpy.int64) # yields 10
print(idx)

# -----------------------
rng = numpy.random.default_rng(seed)
idx = rng.choice(pop,p=numpy.repeat(1/len(pop),len(pop))) # same as above
print(idx)

如果没有给出p,它将使用rng.integers 来获取一个随机值:

If p is not given, it will use rng.integers to get a random value:

rng = numpy.random.default_rng(seed)
idx = rng.integers(0, pop.shape[0]) # yields 9
print(idx)
# -----------------------
rng = numpy.random.default_rng(seed)
idx = rng.choice(pop) # same as above
print(idx)

您可以使用不同的 seed 值.我不知道 rng.randomrng.integers 中会发生什么,但是您可以看到它们的行为有所不同.这就是为什么你得到了不同的结果.

You can play around using different seed value. I don't know what happens in rng.random and rng.integers, but you could see that they behave differently. That's why you got different results.

这篇关于与默认均匀分布相比,为什么 numpy.random.Generator.choice 在给定均匀分布下提供不同的结果(种子)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆