如何检索 sklearn.model_selection.train_test_split 的 random_state? [英] How to retrieve the random_state of sklearn.model_selection.train_test_split?
问题描述
如何检索sklearn.model_selection.train_test_split
的随机状态?
没有设置random_state
,我用train_test_split
分割了我的数据集.因为在分割数据集上训练的机器学习模型表现非常好,我想检索用于分割数据集的 random_state
.有没有类似 numpy.random.get_state()
Without setting the random_state
, I split my dataset with train_test_split
. Because the machine learning model trained on the split dataset performs quite well, I want to retrieve the random_state
that was used to split the dataset. Is there something like numpy.random.get_state()
推荐答案
如果你跟踪train_test_split
的调用栈,你会发现使用了random_state
参数像这样:
If you trace through the call stack of train_test_split
, you'll find the random_state
parameters is used like this:
from sklearn.utils import check_random_state
rng = check_random_state(self.random_state)
print(rng)
check_random_state
的相关部分是
def check_random_state(seed):
if seed is None or seed is np.random:
return np.random.mtrand._rand
如果 random_state=None
,你会得到默认的 numpy.random.RandomState
单例,你可以用它来生成新的随机数,例如:
If random_state=None
, you get the default numpy.random.RandomState
singleton, which you can use to generate new random numbers, e.g.:
print(rng.permutation(10))
print(rng.randn(10))
有关详细信息,请参阅这些问题:
See these questions for more information:
这篇关于如何检索 sklearn.model_selection.train_test_split 的 random_state?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!