[英]np.random.permutation with seed?
I want to use a seed with np.random.permutation
, like我想使用带有np.random.permutation
的种子,比如
np.random.permutation(10, seed=42)
I get the following error:我收到以下错误:
"permutation() takes no keyword arguments"
How can I do that else?我还能怎么做? Thanks.谢谢。
If you want it in one line, you can create a new RandomState
, and call the permutation
on that:如果你想在一行中,你可以创建一个新的RandomState
,并调用它的permutation
:
np.random.RandomState(seed=42).permutation(10)
This is better than just setting the seed of np.random
, as it will have only a localized effect.这比仅仅设置np.random
的种子np.random
,因为它只会产生局部效果。
np.random.seed(42)
np.random.permutation(10)
If you want to call np.random.permutation(10)
multiple times and get identical results, you also need to call np.random.seed(42)
every time you call permutation()
.如果您想多次调用np.random.permutation(10)
并获得相同的结果,您还需要在每次调用permutation()
时调用np.random.seed(42)
permutation()
。
For instance,例如,
np.random.seed(42)
print(np.random.permutation(10))
print(np.random.permutation(10))
will produce different results:会产生不同的结果:
[8 1 5 0 7 2 9 4 3 6]
[0 1 8 5 3 4 7 9 6 2]
while同时
np.random.seed(42)
print(np.random.permutation(10))
np.random.seed(42)
print(np.random.permutation(10))
will give the same output:将给出相同的输出:
[8 1 5 0 7 2 9 4 3 6]
[8 1 5 0 7 2 9 4 3 6]
Set the seed in the previous line在上一行设置种子
np.random.seed(42)
np.random.permutation(10)
You can break it down into:您可以将其分解为:
import numpy as np
np.random.seed(10)
np.random.permutation(10)
By initializing the random seed first, this will guarantee that you get the same permutation.通过首先初始化随机种子,这将保证您获得相同的排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.