简体   繁体   English

numpy.random.seed() 有什么用,有什么区别吗?

[英]What is the use of numpy.random.seed() Does it make any difference?

I have a dataset named "admissions".我有一个名为“admissions”的数据集。

I am trying to carry out holdout validation on a simple dataset.我正在尝试对一个简单的数据集进行坚持验证。 In order to carry out permutation on the index of the dataset, I use the following command:为了对数据集的索引进行排列,我使用以下命令:

import numpy as np
np.random.permutation(admissions.index)

Do I need to use np.random.seed() before the permutation?我需要在排列之前使用np.random.seed()吗? If so, then why and what does the number in np.random.seed(number) represent?如果是这样,那么np.random.seed(number)中的数字为什么以及代表什么?

You don't need to initialize the seed before the random permutation, because this is already set for you.您不需要在随机排列之前初始化种子,因为这已经为您设置好了。 According to the documentation of RandomState :根据RandomState的文档:

Parameters:参数:
seed : {None, int, array_like}, optional Random seed initializing the pseudo-random number generator.种子:{None,int,array_like},可选的随机种子初始化伪随机数生成器。 Can be an integer, an array (or other sequence) of integers of any length, or None (the default).可以是整数、任意长度的整数数组(或其他序列)或 None(默认值)。 If seed is None, then RandomState will try to read data from /dev/urandom (or the Windows analogue) if available or seed from the clock otherwise.如果种子为无,则 RandomState 将尝试从 /dev/urandom(或 Windows 模拟)读取数据(如果可用)或从时钟种子读取数据。

The concept of seed is relevant for the generation of random numbers.种子的概念与随机数的生成有关。 You can read more about it here .您可以在此处阅读更多相关信息。

To integrate this answer with a comment (from JohnColeman) to your question, I want to mention this example:为了将此答案与评论(来自 JohnColeman)整合到您的问题中,我想提一下这个例子:

>>> numpy.random.seed(0)
>>> numpy.random.permutation(4)
array([2, 3, 1, 0])
>>> numpy.random.seed(0)
>>> numpy.random.permutation(4)
array([2, 3, 1, 0])

Note that np.random.seed is deprecated and only kept around for backwards-compatibility.请注意, np.random.seed已弃用,仅保留用于向后兼容。 That's because re-seeding an existing random-number generator (RNG) is bad practice.这是因为重新播种现有的随机数生成器 (RNG) 是不好的做法。 If you need to seed (eg, to make computations reproducible for tests), create a new RNG:如果您需要种子(例如,使计算可重现以进行测试),请创建一个新的 RNG:

import numpy as np


rng = np.random.default_rng(seed=0)
out = rng.random(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM