admin管理员组文章数量:1291116
Running caret:createFolds multiple times with a set.seed prior to running the first caret:createFolds call creates different folds. But while running caret:createFolds with set.seed running just before the call every single time gives me the same folds every single time. Why?
When using the following lines of code I get different folds (although when repeating the code I do get the same different folds), but when running the second snippet of code the folds are consistent... I don't understand it.
set.seed(18)
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
gives:
212213
222311
211333
212132
312211
213231
This code:
lapply(1:5, function(x) {
+ set.seed(18)
+ head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
+ })
consistently gives 2 1 2 2 1 3
doing set.seed before every createFolds (without the lapply and function, but basically the same) gives the same consistent 2 1 2 2 1 3.
Why doesn't caret respect the seed when set?
Running caret:createFolds multiple times with a set.seed prior to running the first caret:createFolds call creates different folds. But while running caret:createFolds with set.seed running just before the call every single time gives me the same folds every single time. Why?
When using the following lines of code I get different folds (although when repeating the code I do get the same different folds), but when running the second snippet of code the folds are consistent... I don't understand it.
set.seed(18)
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
gives:
212213
222311
211333
212132
312211
213231
This code:
lapply(1:5, function(x) {
+ set.seed(18)
+ head(caret::createFolds(mtcars$cyl, k=3, list=FALSE))
+ })
consistently gives 2 1 2 2 1 3
doing set.seed before every createFolds (without the lapply and function, but basically the same) gives the same consistent 2 1 2 2 1 3.
Why doesn't caret respect the seed when set?
Share Improve this question edited Mar 1 at 8:42 Jan 9,3486 gold badges20 silver badges33 bronze badges asked Feb 13 at 15:04 user29444121user29444121 111 silver badge1 bronze badge1 Answer
Reset to default 1Caret is respecting the seed. When you set a seed, the output of the code that follows is then reproducible, which is why you consistently get 212213 222311 211333 212132 312211 213231 in that order. Setting seed does not mean that every function call that involves random number generation will give the same output, just that any sequence of function calls will give reproducible output.
本文标签: rDifferent folds using setseed and caretcreateFoldsStack Overflow
版权声明:本文标题:r - Different folds using set.seed and caret:createFolds - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741523397a2383323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论