美文网首页
数据集拆分:训练集、验证集、测试集

数据集拆分:训练集、验证集、测试集

作者: 小贝学生信 | 来源:发表于2021-10-30 01:30 被阅读0次

    1、数据集拆分

    • 首先最基本的是将数据集分为训练集(Training)与测试集(Test)两部分。在测试集用于训练、确定一个最终的模型;然后在测试集测试模型对于未知数据的评价效果。

    1.1 训练集

    • 如上所述,在训练集就要确定了最终的模型,包括参数优化;
    • 一般来说原始Train训练集会进一步再分为Train训练集与Validation验证集两部分,以评价不同参数组合的效果,以确定最终的模型。最后放到Test测试集中评价其效果。

    1.2 测试集

    • 一定要注意Test测试集自始至终没有参与到模型的训练过程;它的目的只有一个:在确定一个最终模型后,评价其泛化能力。

    1.3 拆分方法

    • 一般先拆分数据为(Train + Validation)与Test两部分,其比例一般为 8:2、7:3、6:4
    • 其次进一步拆分出Train + Validation的方法:可以按照设定比例进行拆分,也有K折交叉验证☆、Bootstrap自助法抽样。
    (1)K折交叉验证

    K折交叉验证是比较常用的拆分训练集、测试集,并用于模型训练、验证的方式。具体步骤如下--

    • 首先将原始训练集(区别test测试集数据)分为K份;
    • 然后选择其中K-1 份数据用于训练,剩余的1份用于评价效果;
    • 重复K次,保证每份数据都作为过训练数据与验证数据;
    • 最后得到K次的模型评价结果;取均值作为该模型参数下的最终评价结果;
    • 从而可以比较不同模型参数下的评价结果,进行模型优化,确定最终的模型。
    (2)Bootstrapping自助法抽样

    自助法抽样的核心理解就是:有放回的抽样。

    • 因为有放回,所以可能会抽到重复值;据统计计算会抽到63.21%的数据,而未被抽到过的数据(out-of-bag, OOB)则被视为validation验证集。


    再强调一点就是:是将原始训练集进一步拆分为 训练集与测试集。在上述过程中,都是把测试集放到一边的,不去管它,直到确定好模型之后才会用到test测试集。

    2、数据实操

    # Ames housing data
    ames <- AmesHousing::make_ames()
    

    2.1 原始训练集与测试集的拆分

    有多种方式可供选择

    • (1) base R sample()
    set.seed(123)  # for reproducibility
    index_1 <- sample(1:nrow(ames), round(nrow(ames) * 0.7))
    train_1 <- ames[index_1, ]
    test_1  <- ames[-index_1, ]
    
    • (2) caret package
    library(caret)
    set.seed(123)  # for reproducibility
    index_2 <- createDataPartition(ames$Sale_Price, p = 0.7, 
                                   list = FALSE)
    train_2 <- ames[index_2, ]
    test_2  <- ames[-index_2, ]
    
    • (3) rsample
    library(rsample)
    set.seed(123)  # for reproducibility
    split_1  <- initial_split(ames, prop = 0.7)
    train_3  <- training(split_1)
    test_3   <- testing(split_1)
    
    补充:对于不均衡样本的拆分方式

    对于分类为目的的有监督学习(例如癌症恶性、良性预测);当收集的样本分布很不均衡时,在抽样还有训练过程中都需要多加考虑。

    # Job attrition data
    library(tidyverse)
    library(modeldata)
    churn <- attrition %>% 
      mutate_if(is.ordered, .funs = factor, ordered = FALSE)
    
    #如下未离职员工与离职员工比例约为 84:16
    table(churn$Attrition) %>% prop.table()
    ## 
    ##        No       Yes 
    ## 0.8387755 0.1612245
    
    • 分层抽样Stratified sampling
    # stratified sampling with the rsample package
    set.seed(123)
    split_strat  <- initial_split(churn, prop = 0.7, 
                                  strata = "Attrition")
    train_strat  <- training(split_strat)
    test_strat   <- testing(split_strat)
    
    table(train_strat$Attrition) %>% prop.table()
    ## 
    ##       No      Yes 
    ## 0.838835 0.161165
    table(test_strat$Attrition) %>% prop.table()
    ## 
    ##        No       Yes 
    ## 0.8386364 0.1613636
    

    通过抽样来均衡比例
    Down-sampling balances the dataset by reducing the size of the abundant class(es) to match the frequencies in the least prevalent class. This method is used when the quantity of data is sufficient.
    On the contrary, up-sampling is used when the quantity of data is insufficient. It tries to balance the dataset by increasing the size of rarer samples. Rather than getting rid of abundant samples, new rare samples are generated by using repetition or bootstrapping

    • 另外关于K-折交叉验证在后面算法学习过程中经常用到,而Bootstrap抽样在随机森林等算法中也会用到。
      这里简单演示通过rsample包的实现方式:
    # K-折交叉验证
    rsample::vfold_cv(ames, v = 10)
    
    # Bootstrap
    rsample::bootstraps(ames, times = 10)
    

    相关文章

      网友评论

          本文标题:数据集拆分:训练集、验证集、测试集

          本文链接:https://www.haomeiwen.com/subject/wigialtx.html