简体   繁体   English

numpy.random 与 numpy.random.Generate 之间有什么区别

[英]What's the difference between numpy.random vs numpy.random.Generate

I've been trying to simulate some Monte Carlos simulations lately and came across numpy.random .我最近一直在尝试模拟一些 Monte Carlos 模拟并遇到了numpy.random Checking the documentation of the exponential generator I've noticed that that's a warning in the page, which tells that检查指数生成器的文档我注意到这是页面中的警告,它告诉

Generator.exponential should be used for new code. Generator.exponential 应该用于新代码。

Althought that, numpy.random.exponential still works, but I couldn't run the Generator counterpart.尽管如此, numpy.random.exponential仍然有效,但我无法运行Generator对应物。 I've been getting the following error:我一直收到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-c4cc7e61aa98> in <module>
----> 1 np.random.Generator.exponential(2, 1000)

TypeError: descriptor 'exponential' for 'numpy.random._generator.Generator' objects doesn't apply to a 'int' object

My questions are:我的问题是:

  1. What's the difference between these 2?这2个有什么区别?

  2. How to generate a sample with Generator ?如何使用Generator生成样本?

The Generator referred to in the documentation is a class, introduced in NumPy 1.17: it's the core class responsible for adapting values from an underlying bit generator to generate samples from various distributions.文档中提到的Generator是一个类,在 NumPy 1.17 中引入:它是负责调整来自底层位生成器的值以从各种分布生成样本的核心类。 numpy.random.exponential is part of the (now) legacy Mersenne-Twister-based random framework. numpy.random.exponential是(现在)基于 Mersenne-Twister 的旧式随机框架的一部分。 You probably shouldn't worry about the legacy functions being removed any time soon - doing so would break a huge amount of code, but the NumPy developers recommend that for new code, you should use the new system, not the legacy system.您可能不应该担心很快就会删除遗留函数 - 这样做会破坏大量代码,但 NumPy 开发人员建议,对于代码,您应该使用新系统,而不是遗留系统。

Your best source for the rationale for the change to the system is probably NEP 19: https://numpy.org/neps/nep-0019-rng-policy.html您更改系统的理由的最佳来源可能是 NEP 19: https : //numpy.org/neps/nep-0019-rng-policy.html

To use Generator.exponential as recommended by the documentation, you first need to create an instance of the Generator class.要按照文档的建议使用Generator.exponential ,您首先需要创建Generator类的实例 The easiest way to create such an instance is to use the numpy.random.default_rng() function.创建此类实例的最简单方法是使用numpy.random.default_rng()函数。

So you want to start with something like:所以你想从这样的事情开始:

>>> import numpy
>>> my_generator = numpy.random.default_rng()

At this point, my_generator is an instance of numpy.random.Generator :在这一点上, my_generator是一个实例numpy.random.Generator

>>> type(my_generator)
<class 'numpy.random._generator.Generator'>

and you can use my_generator.exponential to get variates from an exponential distribution.并且您可以使用my_generator.exponential从指数分布中获取变量。 Here we take 10 samples from an exponential distribution with scale parameter 3.2 (or equivalently, rate 0.3125 ):在这里,我们从尺度参数为3.2 (或等效为0.3125 )的指数分布中抽取 10 个样本:

>>> my_generator.exponential(3.2, size=10)
array([6.26251663, 1.59879107, 1.69010179, 4.17572623, 5.94945358,
       1.19466134, 3.93386506, 3.10576934, 1.26095418, 1.18096234])

Your Generator instance can of course also be used to get any other random variates you need:您的Generator实例当然也可用于获取您需要的任何其他随机变量:

>>> my_generator.integers(0, 100, size=3)
array([56, 57, 10])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM