我们可以在mlr中使用预定义的CV进行CV(重采样)吗?

时间:2017-11-04 14:18:19

标签: r machine-learning mlr

要在mlr R包中进行交叉验证(重新取样),通常我们需要调用makeResampleDesc函数来指定方法和折叠。

我的问题是:

  1. 是否可以使用预定义的列作为折叠列?或者,
  2. makeResampleDesc中的mlr确保创建的折叠是一致的(在同一种原因下的不同学习者之间),并且可以导出以进行进一步操作?

2 个答案:

答案 0 :(得分:1)

重新采样说明独立于任何学习者;您可以与多个学习者一起使用并获得相同的折叠。如果要将重新取样结果链接回原始数据,也可以从重新取样结果中提取折叠数。

您可以使用blocking参数makeClassifTask将数据中的列用作折叠列。来自帮助:

  

阻止:['因子']

      An optional factor of the same length as the number of
      observations. Observations with the same blocking level
      “belong together”. Specifically, they are either put all in
      the training or the test set during a resampling iteration.
      Default is ‘NULL’ which means no blocking.

答案 1 :(得分:0)

我遇到了类似的问题。

尝试以下代码无法获得相同的学习者:

library(mlr)
set.seed(123)
K_Fold = 3
rdesc <- makeResampleDesc("CV", iters = K_Fold)
r <- resample("regr.rpart", bh.task, rdesc, show.info = FALSE, extract = getFeatureImportance, measures = medae)
KFoldIndex <- getResamplingIndices(r)
r2 <- resample("regr.glm", bh.task, rdesc, show.info = FALSE, measures = medae)
KFoldIndex2 <- getResamplingIndices(r2)

另一方面,如果使用makeResampleInstance,则可以将相同的索引应用于不同的独立学习者。可以在这里找到:https://mlr.mlr-org.com/articles/tutorial/resample.html

rdesc = makeResampleDesc("CV", iters = K_Fold)
rin = makeResampleInstance(rdesc, size = nrow(iris))
r.lda = resample("classif.lda", iris.task, rin, show.info = FALSE)
r.rpart = resample("classif.rpart", iris.task, rin, show.info = FALSE)

getResamplingIndices(r.lda)
getResamplingIndices(r.rpart)