R:如果加载了插入符号包,则 set.seed() 结果不匹配 [英] R: set.seed() results don't match if caret package loaded
问题描述
我在 R(版本:3.3.0)中使用 createFolds()
来创建训练/测试分区.为了使结果可重现,我使用了种子值为 10 的 set.seed()
.正如预期的那样,结果(生成的折叠)是可重现的.
I am using createFolds()
in R (version: 3.3.0) to create train/test partitions. To make results reproducible, I used set.seed()
with a seed value of 10. As expected, the results (generated folds) were reproducible.
但是一旦我在设置种子后加载了插入符号包.然后使用createFolds函数,发现创建的折叠不一样了(虽然还是可以重现的).
But once I loaded caret package just after setting the seed. And then used the createFolds function, I found that the created folds were different (although still reproducible).
具体来说,创建的折叠在以下两种情况下有所不同:
Specifically, the created folds differ in the following two cases:
情况 1:
library(caret)
set.seed(10)
folds=createFolds(y,k=5,returnTrain=TRUE)
情况 2:
set.seed(10)
library(caret)
folds=createFolds(y,k=5,returnTrain=TRUE)
其中 y
是一个向量.
为什么会发生这种情况?
Why could this be happening?
推荐答案
罪魁祸首是 ggplot2,它在您加载插入符号时附加.它定义了一个 .onAttach
函数:https://github.com/hadley/ggplot2/blob/master/R/zzz.r
The culprit is ggplot2, which is attached when you load caret. It defines an .onAttach
function: https://github.com/hadley/ggplot2/blob/master/R/zzz.r
这个函数在包被附加时被调用,参见help("ns-hooks")
.并在其中调用 runif
从而推进 RNG 的状态.
This function is called when the package is attached, see help("ns-hooks")
. And within it runif
is called thereby advancing the state of the RNG.
这篇关于R:如果加载了插入符号包,则 set.seed() 结果不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!