简体   繁体   English

np.random.permutation 与种子?

[英]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.

相关问题 关于 np.random.permutation 种子的问题 - a question about the seed of np.random.permutation np.random.permutation,np.random.choice的时间性能 - Time performance of np.random.permutation, np.random.choice 函数np.random.permutation是否可以替换? - Does the function np.random.permutation work with or without replacement? 为什么 np.random.default_rng().permutation(n) 优于原始的 np.random.permutation(n)? - Why is np.random.default_rng().permutation(n) preferred over the original np.random.permutation(n)? 包含混合元素的列表的置换列表(np.random.permutation()失败,出现ValueError) - Permute list of lists with mixed elements (np.random.permutation() fails with ValueError) 从命令行运行.py文件但不在juypter笔记本中运行np.random.permutation时出现ValueError - ValueError on np.random.permutation when running .py file from command line but not in juypter notebook 使用 len() function 和使用 np.random.permutation 索引时的结果差异 - Difference in result while using len() function and index with np.random.permutation 带有种子的固定随机排列 - A fixed, random permutation with a seed np.random.seed(1) 和 np.random.seed(0) 的区别? - Difference between np.random.seed(1) and np.random.seed(0)? np.random.seed() 和 np.random.RandomState() 之间的区别 - Difference between np.random.seed() and np.random.RandomState()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM