2024-05-28 08:07:59 发布
网友
我知道要种下numpy.random的随机性,并且能够复制它,我应该:
import numpy as np np.random.seed(1234)
但是 np.random.RandomState() 是吗?
np.random.RandomState()
random.seed是填充random.RandomState容器的方法。
来自numpy docs:
numpy.random.seed(seed=None)
Seed the generator.This method is called when RandomState is initialized. It can be called again to re-seed the generator. For details, see RandomState.
Seed the generator.
This method is called when RandomState is initialized. It can be called again to re-seed the generator. For details, see RandomState.
class numpy.random.RandomState
Container for the Mersenne Twister pseudo-random number generator.
如果要设置调用np.random...将使用的种子,请使用np.random.seed:
np.random...
np.random.seed
np.random.seed(1234) np.random.uniform(0, 10, 5) #array([ 1.9151945 , 6.22108771, 4.37727739, 7.85358584, 7.79975808]) np.random.rand(2,3) #array([[ 0.27259261, 0.27646426, 0.80187218], # [ 0.95813935, 0.87593263, 0.35781727]])
使用该类可避免影响全局numpy状态:
r = np.random.RandomState(1234) r.uniform(0, 10, 5) #array([ 1.9151945 , 6.22108771, 4.37727739, 7.85358584, 7.79975808])
它保持着和以前一样的状态:
r.rand(2,3) #array([[ 0.27259261, 0.27646426, 0.80187218], # [ 0.95813935, 0.87593263, 0.35781727]])
您可以使用以下命令查看“global”类的状态:
np.random.get_state()
以及您自己的类实例:
r.get_state()
构造一个随机数生成器。它对np.random中的独立函数没有任何影响,但必须显式使用:
np.random
>>> rng = np.random.RandomState(42) >>> rng.randn(4) array([ 0.49671415, -0.1382643 , 0.64768854, 1.52302986]) >>> rng2 = np.random.RandomState(42) >>> rng2.randn(4) array([ 0.49671415, -0.1382643 , 0.64768854, 1.52302986])
random.seed是填充random.RandomState容器的方法。
来自numpy docs:
如果要设置调用
np.random...
将使用的种子,请使用np.random.seed
:使用该类可避免影响全局numpy状态:
它保持着和以前一样的状态:
您可以使用以下命令查看“global”类的状态:
以及您自己的类实例:
构造一个随机数生成器。它对
np.random
中的独立函数没有任何影响,但必须显式使用:相关问题 更多 >
编程相关推荐