random_state sklearn train_test_split 中的参数

random_state parameter in sklearn's train_test_split

不同的随机状态值对输出有什么影响?例如,如果我设置 0 和 100 会对输出产生什么影响?

来自docs

The random_state is the seed used by the random number generator.

一般来说,种子用于创建可重现的输出。在 train_test_split 的情况下,random_state 决定了数据集的拆分方式。 除非你想创建可重复的运行,否则你可以跳过这个参数。

For instance, if is set 0 and if i set 100 what difference would it make to the output ?

对于特定种子,您将始终获得相同的 train/test 拆分。不同的种子将导致不同的 train/test 分裂。

将不同的整数传递给 random_state 使用这些值和 makes the resulting "random" train and test data reproducible 为 NumPy 的伪随机数生成器提供种子。这意味着如果您使用 random_seed=0 传递函数数组 a,使用 0 种子值将始终产生相同的训练和测试数据。

当您传递一个整数时,该值最终会传递给 scklearn.utils.check_random_state(),变成:

if isinstance(seed, (numbers.Integral, np.integer)):
    return np.random.RandomState(seed)

这又被 class 之类的 ShuffleSplit 用来调用随机排列:

rng = check_random_state(self.random_state)
for i in range(self.n_splits):
    # random partition
    permutation = rng.permutation(n_samples)
    ind_test = permutation[:n_test]
    ind_train = permutation[n_test:(n_test + n_train)]
    yield ind_train, ind_test

这是一个使用实际方法的示例:

>>> np.random.RandomState(0).permutation([1, 4, 9, 12, 15])
array([ 9,  1,  4, 12, 15])
>>> np.random.RandomState(0).permutation([1, 4, 9, 12, 15])
array([ 9,  1,  4, 12, 15])
>>> np.random.RandomState(0).permutation([1, 4, 9, 12, 15])
array([ 9,  1,  4, 12, 15])
>>> np.random.RandomState(100).permutation([1, 4, 9, 12, 15])
array([ 4,  9, 12, 15,  1])
>>> np.random.RandomState(100).permutation([1, 4, 9, 12, 15])
array([ 4,  9, 12, 15,  1])
>>> np.random.RandomState(100).permutation([1, 4, 9, 12, 15])
array([ 4,  9, 12, 15,  1])