Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python np.随机.带种子的置换?_Python_Numpy_Random_Permutation - Fatal编程技术网

Python np.随机.带种子的置换?

Python np.随机.带种子的置换?,python,numpy,random,permutation,Python,Numpy,Random,Permutation,我想使用一个带有np.random.permutation的种子,比如 np.random.permutation(10, seed=42) 我得到以下错误: "permutation() takes no keyword arguments" 我还能怎么做呢?谢谢。在前一行设置种子 np.random.seed(42) np.random.permutation(10) 如果要多次调用np.random.permutation(10)并获得相同的结果,那么每次调用permutation(

我想使用一个带有np.random.permutation的种子,比如

np.random.permutation(10, seed=42)
我得到以下错误:

"permutation() takes no keyword arguments"

我还能怎么做呢?谢谢。

在前一行设置种子

np.random.seed(42)
np.random.permutation(10)
如果要多次调用
np.random.permutation(10)
并获得相同的结果,那么每次调用
permutation()
时还需要调用
np.random.seed(42)


比如说,

np.random.seed(42)
print(np.random.permutation(10))
print(np.random.permutation(10))
将产生不同的结果:

[8 1 5 0 7 2 9 4 3 6]
[0 1 8 5 3 4 7 9 6 2]

np.random.seed(42)
print(np.random.permutation(10))
np.random.seed(42)
print(np.random.permutation(10))
将给出相同的输出:

[8 1 5 0 7 2 9 4 3 6]
[8 1 5 0 7 2 9 4 3 6]

您可以将其分解为:

import numpy as np
np.random.seed(10)
np.random.permutation(10)

通过首先初始化随机种子,这将保证获得相同的排列

如果您想将其放在一行中,您可以创建一个新的
RandomState
,并在该行上调用
排列

np.random.RandomState(seed=42).permutation(10)

这比仅仅设置np.random的种子要好,因为它只会产生局部效应。

这个答案是不完整的。更多细节请参考Giorgos的答案。似乎我们每次都必须重置随机种子,以确保获得相同的随机数
np.random.RandomState(seed=42).permutation(10)