谁能给我解释一下 random_state
在下面的例子中是什么意思?
import numpy as np
from sklearn.model_selection import train_test_split
X, y = np.arange(10).reshape((5, 2)), range(5)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)
为什么硬编码为 42?
这不是很明显吗? 42 是 Answer to the Ultimate Question of Life, the Universe, and Everything .
严肃地说,random_state
只是为随机生成器设置一个种子,因此您的训练测试拆分始终是确定性的。如果不设置种子,每次都不一样。
Relevant documentation :
random_state
: int
, RandomState
instance or None
, optional
(default=None
)
If int
, random_state
is the seed used by the random
number generator; If RandomState
instance, random_state
is the random
number generator; If None
, the random number generator is the
RandomState
instance used by np.random
.