当前位置: 首页 > 知识库问答 >
问题:

在用插入符号训练多个模型时使用相同的trainControl对象进行交叉验证是否允许精确的模型比较?

谷梁向荣
2023-03-14

我最近一直在研究R包cert,有一个关于训练过程中模型的可再现性和比较性的问题,但我还没能确定下来。

我的意图是,每个train调用以及由此产生的每个模型都使用相同的交叉验证拆分,以便交叉验证的初始存储结果与构建过程中计算的模型样本外估计具有可比性。

我见过的一种方法是,您可以在每次train调用之前指定种子:

set.seed(1)
model <- train(..., trControl = trainControl(...))
set.seed(1)
model2 <- train(..., trControl = trainControl(...))
set.seed(1)
model3 <- train(..., trControl = trainControl(...))

但是,在train调用之间共享traincontrol对象是否意味着它们通常使用相同的重采样和索引,或者是否必须将seeds参数显式传递到函数中。列控对象在使用时是否具有随机功能,还是在申报时设置?

我现在的方法是:

set.seed(1)
train_control <- trainControl(method="cv", ...)
model1 <- train(..., trControl = train_control)
model2 <- train(..., trControl = train_control)
model3 <- train(..., trControl = train_control)

这些列车呼叫是否将使用相同的拆分并具有可比性,或者我必须采取进一步的步骤来确保这一点?即在创建traincontrol对象时指定种子,还是在每个训练之前调用set.seed?还是两者兼而有之?

我正在查询的代码项目可以在这里找到。读起来可能会更容易,你就会明白了。

共有1个答案

张子墨
2023-03-14

在定义traincontrol过程中不会创建CV折叠,除非使用index参数显式说明,我推荐使用该参数。可以使用专门的插入符号函数之一创建这些代码:

CreateFolds
CreateMultiFolds
CreateTimeSlices
GroupKFolds

也就是说,在traincontrol定义之前使用特定的种子不会导致相同的CV折叠。

示例:

library(caret)
library(tidyverse)

set.seed(1)
trControl = trainControl(method = "cv",
                         returnResamp = "final",
                         savePredictions = "final")

创建两个模型:

knnFit1 <- train(iris[,1:4], iris[,5],
                 method = "knn",
                 preProcess = c("center", "scale"),
                 tuneLength = 10,
                 trControl = trControl)

ldaFit2 <- train(iris[,1:4], iris[,5],
                 method = "lda",
                 tuneLength = 10,
                 trControl = trControl)
knnFit1$pred %>%
  left_join(ldaFit2$pred, by = "rowIndex") %>%
  mutate(same = Resample.x == Resample.y) %>%
  {all(.$same)}
#FALSE
set.seed(1)
knnFit1 <- train(iris[,1:4], iris[,5],
                 method = "knn",
                 preProcess = c("center", "scale"),
                 tuneLength = 10,
                 trControl = trControl)

set.seed(1)
ldaFit2 <- train(iris[,1:4], iris[,5],
                 method = "lda",
                 tuneLength = 10,
                 trControl = trControl)


set.seed(1)
rangerFit3 <- train(iris[,1:4], iris[,5],
                 method = "ranger",
                 tuneLength = 10,
                 trControl = trControl)


knnFit1$pred %>%
  left_join(ldaFit2$pred, by = "rowIndex") %>%
  mutate(same = Resample.x == Resample.y) %>%
  {all(.$same)}

knnFit1$pred %>%
  left_join(rangerFit3$pred, by = "rowIndex") %>%
  mutate(same = Resample.x == Resample.y) %>%
  {all(.$same)}
 类似资料:
  • 我有一个相对大的数据: 超过37万个观测数据,分类因变量有250个水平,10个自变量包括数值变量和分类变量。 下面是我的代码: 有人告诉我包'CV tools'或'cert'可以预形成k-folds CV,但我仍然不能成功地执行这些包或函数。

  • *种子为重复性设置为123,我运行的是3.63 R。

  • 问题内容: 我想通过交叉验证从Logistic回归模型预测概率。我知道您可以获取交叉验证分数,但是可以从predict_proba返回值而不是分数吗? 问题答案: 现在,这已作为scikit- learn版本0.18的一部分实现。您可以将’method’字符串参数传递给cross_val_predict方法。文档在这里。 例: 还要注意,这是新的sklearn.model_selection包的一

  • 我正在工作中的一些数据上建立一个随机森林(这意味着我不能分享那个数据,有15K个观测),使用插入符号训练函数进行交叉验证,模型的准确率很低:0.9%。 下面是我使用的代码: --edit2 以下是有关我的数据的一些详细信息: 15493 OBS。17个变量中: ICNUmber是一个包含1531个不同值的字符串,这些是类 其他16个变量是具有33个级别的因子 --edit2 --edit3 我的最

  • 我的Keras CNN模型(基于AlexNet的一个实现)的训练精度总是接近0.5(在+-0.02以内),验证精度总是精确的0.5。它是一个二进制分类模型,其中train/val的分割大约为85/15,并且在这两个集合中,图像对每个类进行50/50的分割。

  • 我已经在AWS SageMaker上使用内置算法语义分割训练了一个模型。这个名为model.tar.gz的训练模型存储在S3上。所以我想从S3下载这个文件,然后使用它在我的本地电脑上进行推断,而不使用AWS SageMaker。 以下是三个文件: > :包括网络架构、数据输入和训练的参数。请参阅语义分割超参数。 我的代码: 错误: