ash*_*hok 19 python random numpy scipy python-2.7
我试图用不同的种子生成scipy.stats.pareto.rvs(b,loc = 0,scale = 1,size = 1).
在numpy中我们可以使用numpy.random.seed(seed = 233423)播种.
有没有办法播种scipy统计数据生成的随机数.
注意:我没有使用numpy pareto,因为我想给出不同的比例值.
Kel*_*sey 22
scipy.stats
只是用来numpy.random
生成它的随机数,所以numpy.random.seed()
也可以在这里工作.例如,
import numpy as np
from scipy.stats import pareto
b = 0.9
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
Run Code Online (Sandbox Code Playgroud)
会打印[ 9.7758784 10.78405752 4.19704602 1.19256849 1.02750628]
两次.
Abh*_*nav 11
对于那些在 7 年后偶然发现这个问题的人来说,numpy 随机状态生成器函数发生了重大变化。根据此处和此处的文档,RandomState
该类将替换为Generator
该类。RandomState
保证与旧版本/代码兼容,但是它不会收到任何实质性的变化,包括算法改进,这是为Generator
.
为了说明如何在同一实验中将现有的基于 Numpy 的随机流传递给 Scipy 函数,下面给出了一些示例和推理,说明哪些情况是可取的,以及为什么。
from numpy.random import Generator, PCG64
from scipy.stats import binom
n, p, size, seed = 10, 0.5, 10, 12345
# Case 1 : Scipy uses some default Random Generator
numpy_randomGen = Generator(PCG64(seed))
scipy_randomGen = binom
print(scipy_randomGen.rvs(n, p, size))
print(numpy_randomGen.binomial(n, p, size))
# prints
# [6 6 5 4 6 6 8 6 6 4]
# [4 4 6 6 5 4 5 4 6 7]
# NOT DESIRABLE as we don't have control over the seed of Scipy random number generation
# Case 2 : Scipy uses same seed and Random generator (new object though)
scipy_randomGen.random_state=Generator(PCG64(seed))
numpy_randomGen = Generator(PCG64(seed))
print(scipy_randomGen.rvs(n, p, size))
print(numpy_randomGen.binomial(n, p, size))
# prints
# [4 4 6 6 5 4 5 4 6 7]
# [4 4 6 6 5 4 5 4 6 7]
# This experiment is using same sequence of random numbers, one is being used by Scipy
# and other by Numpy. NOT DESIRABLE as we don't want repetition of some random
# stream in same experiment.
# Case 3 (IMP) : Scipy uses an existing Random Generator which can being passed to Scipy based
# random generator object
numpy_randomGen = Generator(PCG64(seed))
scipy_randomGen.random_state=numpy_randomGen
print(scipy_randomGen.rvs(n, p, size))
print(numpy_randomGen.binomial(n, p, size))
# prints
# [4 4 6 6 5 4 5 4 6 7]
# [4 8 6 3 5 7 6 4 6 4]
# This should be the case which we mostly want (DESIRABLE). If we are using both Numpy based and
#Scipy based random number generators/function, then not only do we have no repetition of
#random number sequences but also have reproducibility of results in this case.
Run Code Online (Sandbox Code Playgroud)
添加到 user5915738 的答案,我认为这是一般的最佳答案,我想指出 imho 最方便的方法来播种scipy.stats
分布的随机生成器。
您可以在使用该rvs
方法生成分布时设置种子,方法是将种子定义为整数,用于np.random.RandomState
内部播种:
uni_int_seed = scipy.stats.uniform(-.1, 1.).rvs(10, random_state=12)
Run Code Online (Sandbox Code Playgroud)
或直接定义np.random.RandomState
:
uni_state_seed = scipy.stats.uniform(-.1, 1.).rvs(
10, random_state=np.random.RandomState(seed=12))
Run Code Online (Sandbox Code Playgroud)
这两种方法是等价的:
np.all(uni_int_seed == uni_state_seed)
# Out: True
Run Code Online (Sandbox Code Playgroud)
这种方法在其分配到的优点random_state
的rv_continuous
或者rv_discrete
是,你总是有明确的控制你的随机状态rvs
,而与my_dist.random_state = np.random.RandomState(seed=342423)
种子的每次调用丢失后rvs
,可能导致非重复性的结果失去分布的轨道时.
同样根据Python 之禅:
- 显式优于隐式。
:)
对于四年后发生此问题的人员,Scipy DOES提供了一种将np.random.RandomState
对象传递给其随机变量类的方法,有关更多详细信息,请参见rv_continuous和rv_discrete。scipy文档说:
seed:None或int或numpy.random.RandomState实例,可选
此参数定义用于绘制随机变量的RandomState对象。如果为None(或np.random),则使用全局np.random状态。如果为整数,则用于播种本地RandomState实例。默认为无。
不幸的是,在连续/离散rvs子类rv_continuous
或之后,此参数似乎不可用rv_discrete
。但是,该random_state
属性确实属于sublass,这意味着我们可以使用np.random.RandomState
after实例化实例来设置种子,如下所示:
import numpy as np
import scipy.stats as stats
alpha_rv = stats.alpha(3.57)
alpha_rv.random_state = np.random.RandomState(seed=342423)
Run Code Online (Sandbox Code Playgroud)