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

2024-06-26 01:39:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为“招生”的数据集。

我试图在一个简单的数据集上执行holdout验证。为了对数据集的索引执行排列,我使用以下命令:

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

我需要在置换之前使用np.random.seed()吗?如果是,那么np.random.seed(number)中的数字代表什么?


Tags: 数据import命令numpynumberindexasnp
1条回答
网友
1楼 · 发布于 2024-06-26 01:39:16

您不需要在随机排列之前初始化种子,因为这已经为您设置好了。 根据RandomState的文档:

Parameters:
seed : {None, int, array_like}, optional Random seed initializing the pseudo-random number generator. Can be an integer, an array (or other sequence) of integers of any length, or None (the default). 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.

种子的概念与随机数的产生有关。你可以阅读更多关于它的资料。

为了将这个答案与您的问题的评论(来自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])

相关问题 更多 >