np.random.seed()和np.random.RandomState()之间的区别 [英] Difference between np.random.seed() and np.random.RandomState()

查看:94
本文介绍了np.random.seed()和np.random.RandomState()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道要播种numpy.random的随机性并能够复制它,我应该:

I know that to seed the randomness of numpy.random, and be able to reproduce it, I should us:

import numpy as np
np.random.seed(1234)

但是什么 np.random.RandomState() 会吗?

推荐答案

如果要设置调用np.random...的种子,请使用np.random.seed:

If you want to set the seed that calls to np.random... will use, use 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状态:

Use the class to avoid impacting the global numpy state:

r = np.random.RandomState(1234)
r.uniform(0, 10, 5)
#array([ 1.9151945 ,  6.22108771,  4.37727739,  7.85358584,  7.79975808])

它和以前一样保持状态:

And it maintains the state just as before:

r.rand(2,3)
#array([[ 0.27259261,  0.27646426,  0.80187218],
#       [ 0.95813935,  0.87593263,  0.35781727]])

您可以通过以下方式查看全局"类的状态:

You can see the state of the sort of 'global' class with:

np.random.get_state()

和您自己的类实例,具有:

and of your own class instance with:

r.get_state()

这篇关于np.random.seed()和np.random.RandomState()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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