首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scipy.stats种子?

scipy.stats种子?
EN

Stack Overflow用户
提问于 2013-04-15 21:51:09
回答 4查看 17.1K关注 0票数 38

我正在尝试用不同的种子生成scipy.stats.pareto.rvs(b,loc=0,scale=1,size=1)。

在numpy中,我们可以使用numpy.random.seed(seed=233423)作为种子。

有没有办法将scipy统计数据生成的随机数植入种子?

注意:我没有使用numpy pareto,因为我想为scale提供不同的值。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-04-15 23:23:23

scipy.stats只是使用numpy.random来生成它的随机数,所以numpy.random.seed()在这里也可以工作。例如,

代码语言:javascript
复制
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)

将打印[ 9.7758784 10.78405752 4.19704602 1.19256849 1.02750628]两次。

票数 46
EN

Stack Overflow用户

发布于 2018-04-26 10:33:02

对于那些在四年后读到这篇文章的人来说,Scipy确实提供了一种将np.random.RandomState对象传递给它的随机变量类的方法,请参阅rv_continuousrv_discrete了解更多详细信息。scipy文档是这样说的:

种子:无、int或numpy.random.RandomState实例,可选

此参数定义用于绘制随机变量的RandomState对象。如果为None (或np.random),则使用全局np.random状态。如果为integer,则用于为本地RandomState实例提供种子。默认值为None。

不幸的是,在连续/离散rvs子类rv_continuousrv_discrete之后,该参数似乎不可用。然而,random_state属性确实属于子类,这意味着我们可以在实例化之后使用np.random.RandomState的实例来设置种子,如下所示:

代码语言:javascript
复制
import numpy as np
import scipy.stats as stats

alpha_rv = stats.alpha(3.57)
alpha_rv.random_state = np.random.RandomState(seed=342423)
票数 18
EN

Stack Overflow用户

发布于 2020-09-20 23:44:03

对于那些在7年后偶然发现这个问题的人来说,numpy随机状态生成器函数有了一个重大的变化。根据文档herehereRandomState类被替换为Generator类。RandomState保证与旧版本/代码兼容,但是它不会收到任何实质性的更改,包括为Generator保留的算法改进。

为了阐明如何在同一实验中将现有的基于Numpy的随机流传递给Scipy函数,下面给出了一些示例和推理,对于哪些情况是可取的,以及为什么。

代码语言:javascript
复制
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.
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16016959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档