首页 文章

参数选择和k折交叉验证

提问于
浏览
2

我有一个数据集,需要对整个数据集进行交叉验证,例如10倍交叉验证 . 我想使用径向基函数(RBF)内核和参数选择(RBF内核有两个参数:C和gamma) . 通常,人们使用开发集选择SVM的超参数,然后使用基于开发集的最佳超参数并将其应用于测试集以进行评估 . 但是,就我而言,原始数据集被划分为10个子集 . 顺序地,使用在剩余的9个子集上训练的分类器来测试一个子集 . 很明显,我们没有固定的培训和测试数据 . 在这种情况下,我该如何进行超参数选择?

1 回答

  • 0

    出于特定原因,您的数据是否被划分为这10个分区?如果不是,您可以再次将它们连接/混洗,然后进行常规(重复)交叉验证以执行参数网格搜索 . 例如,使用10个分区和10个重复可以提供总共100个训练和评估集 . 这些现在用于训练和评估所有参数集,因此您将尝试 100 results per parameter set . average performance per parameter set 可以从每组100个结果中计算出来 .

    这个过程已经内置在大多数ML工具中,就像R中的这个简短示例一样,使用 caret 库:

    library(caret)
    library(lattice)
    library(doMC)
    registerDoMC(3)
    
    model <- train(x = iris[,1:4], 
                y = iris[,5], 
                method = 'svmRadial', 
                preProcess = c('center', 'scale'),
                tuneGrid = expand.grid(C=3**(-3:3), sigma=3**(-3:3)), # all permutations of these parameters get evaluated
                trControl = trainControl(method = 'repeatedcv', 
                                            number = 10, 
                                            repeats = 10, 
                                            returnResamp = 'all', # store results of all parameter sets on all partitions and repeats
                                            allowParallel = T))
    
    # performance of different parameter set (e.g. average and standard deviation of performance)
    print(model$results) 
    # visualization of the above
    levelplot(x = Accuracy~C*sigma, data = model$results, col.regions=gray(100:0/100), scales=list(log=3)) 
    # results of all parameter sets over all partitions and repeats. From this the metrics above get calculated
    str(model$resample)
    

    一旦评估了超参数网格,就可以选择合理的参数集(“模型选择”,例如通过选择表现良好而仍然合理的非复杂模型) .

    顺便说一句:如果可能的话,我会建议重复交叉验证而不是交叉验证(最终使用超过10次重复,但细节取决于你的问题);正如@ christian-cerri已经建议的那样,有一个额外的,看不见的测试集用于估计最终模型在新数据上的性能是一个好主意 .

相关问题