使用 R Carets 包训练函数和随机分类方法

Using the R Carets packages train function with stochastic classification methods

据我了解插入符包中的 train 函数:

model <- train(Class ~ ., data=training, method = "nnet", trControl = train_control)

通过尝试一系列参数值并根据训练数据的未见部分评估生成的模型来生成模型。然后返回表现最好的模型作为最终模型。

然而许多分类方法使用随机训练方法;例如用反向传播训练的神经网络。

因此,在训练期间尝试每组参数一次可能无法很好地表示这些参数的性能。

是否可以告诉 train 函数多次尝试每组参数并使用它们的平均性能?如果不能,是否可以使用 train 函数来实现此行为?

编辑:我没有使用 "repeatedcv",因此不能简单地设置重复次数。

这是我的 train_contol:

train_control <- trainControl(method = "LGOCV", p = .75, number = 1)

您可以在trainControl中指定number,这意味着对于每组参数,执行"LGOCV"(leave-group-out交叉验证)number 次。一个例子:

library(caret)

mod <- train(Species ~ ., 
        data = iris, 
        method = "nnet", 
        trControl = trainControl(method = "LGOCV", p = 0.75,
                number = 10))

print(mod)

Neural Network 

150 samples
  4 predictors
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Repeated Train/Test Splits Estimated (10 reps, 0.75%) 

Summary of sample sizes: 114, 114, 114, 114, 114, 114, ... 

Resampling results across tuning parameters:

  size  decay  Accuracy   Kappa      Accuracy SD  Kappa SD  
  1     0e+00  0.7888889  0.6833333  0.30037699   0.45056549
  1     1e-04  0.8027778  0.7041667  0.30944494   0.46416741
  1     1e-01  0.9555556  0.9333333  0.01434438   0.02151657
  3     0e+00  0.8388889  0.7583333  0.17411347   0.26117021
  3     1e-04  0.9500000  0.9250000  0.05037582   0.07556373
  3     1e-01  0.9722222  0.9583333  0.03207501   0.04811252
  5     0e+00  0.9166667  0.8750000  0.11785113   0.17677670
  5     1e-04  0.9583333  0.9375000  0.03000343   0.04500514
  5     1e-01  0.9722222  0.9583333  0.03207501   0.04811252

Accuracy was used to select the optimal model using  the largest value.
The final values used for the model were size = 3 and decay = 0.1. 

根据我之前的评论,这是一个新的答案。您可以在 trainControl 中指定 index,这是一个包含每个 LGOCV 训练样本索引的列表。默认情况下,未用于训练的索引用于测试。所以,你可以通过以下方式实现你想要的:

library(caret)

k <- 25L
indexList <- vector("list", k)
names(indexList) <- paste0("TrainIndexes", 1:k)

trainIdx <- createDataPartition(iris$Species, p = 0.75, list = FALSE)
for (i in 1:k) {
    indexList[[i]] <- as.integer(trainIdx)
}

mod <- train(Species ~ ., 
        data = iris, 
        method = "nnet", 
        trControl = trainControl(method = "LGOCV", index = indexList))
print(mod)

Neural Network 

150 samples
  4 predictors
  3 classes: 'setosa', 'versicolor', 'virginica' 

No pre-processing
Resampling: Repeated Train/Test Splits Estimated (25 reps, 0.75%) 

Summary of sample sizes: 114, 114, 114, 114, 114, 114, ... 

Resampling results across tuning parameters:

  size  decay  Accuracy   Kappa      Accuracy SD  Kappa SD  
  1     0e+00  0.7266667  0.5900000  0.175806490  0.26370973
  1     1e-04  0.8344444  0.7516667  0.177045657  0.26556849
  1     1e-01  0.9722222  0.9583333  0.000000000  0.00000000
  3     0e+00  0.8677778  0.8016667  0.064290205  0.09643531
  3     1e-04  0.8600000  0.7900000  0.044270493  0.06640574
  3     1e-01  0.9166667  0.8750000  0.000000000  0.00000000
  5     0e+00  0.8755556  0.8133333  0.024216105  0.03632416
  5     1e-04  0.8633333  0.7950000  0.007691318  0.01153698
  5     1e-01  0.9166667  0.8750000  0.000000000  0.00000000

也就是说,train 将对每个交叉验证过程使用 indexList 中的索引,并且由于每个元素都是相同的,因此将永远使用相同的数据进行训练。作为测试,您可以将 method 更改为 "rpart" 并看到 SD 值为 0(因为它是确定性算法)希望这就是您这次想要的。