【问题标题】:Caret package for R. Which samples are held out?R 的插入符号包。保留了哪些样本?
【发布时间】:2015-03-10 22:51:38
【问题描述】:

我正在使用 caret 包来玩转许多分类方法。目前我想使用一个离开组的交叉验证方法(我知道有更好的方法)。

这是我正在使用的列车控制:

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

我的问题是当我通过 train 函数应用它时,例如

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

我如何访问用于训练的样本和组中保留的样本?

谢谢

【问题讨论】:

    标签: r statistics r-caret


    【解决方案1】:

    让我们通过一个例子来看看:

    首先,您需要在trainControl 函数上指定另一个参数returnResamp='all',以便它返回所有重采样的信息。

    示例数据:

    #classification example
    y  <- rep(c(0,1), c(25,25))
    x1 <- runif(50)
    x2 <- runif(50)
    df <- data.frame(y,x1,x2)
    

    解决方案:

    你的代码应该是这样的(我在下面使用number=2,这样你就可以看到它是如何工作的):

    #notice the returnResamp argument set to 'all'
    train_control <- trainControl(method = "LGOCV", p = .7, number = 2, returnResamp='all')
    
    model <- train(y ~ ., data=df, method = "rpart", trControl = train_control)
    

    现在为了访问重采样,您执行以下操作:

    > model$control$index
    $Resample1
     [1]  1  3  4  5  6  7  9 10 12 13 15 16 17 18 20 21 22 23 26 27 28 29 30 34 35 36 37 38 39 40 41 42 43 44 45 46
    
    $Resample2
     [1]  2  3  4  5  6  9 11 12 13 14 15 16 17 19 20 21 24 25 26 28 29 30 31 33 34 35 36 37 38 40 41 42 45 47 49 50
    

    上面的数字显示了为每个重新采样保留在训练集中的行号。显然其余的都是遗漏组。

    要确认这一点(例如对于 resample1):

    > nrow(df[model$control$index$Resample1,])
    [1] 36 #36 observations kept in training set
    > 36/50
    [1] 0.72 #36/50 = 0.72 is the number specified in p
    

    要访问您执行此操作的行(再次以 resample1 为例):

    > df[model$control$index$Resample1,]
       y           x1         x2
    1  0 0.9706626355 0.90786863
    3  0 0.5664755146 0.66014308
    4  0 0.5540436453 0.95919639
    5  0 0.1941235152 0.60869461
    6  0 0.7966452301 0.64245296
    7  0 0.1021302647 0.50045568
    9  0 0.9963372331 0.86199347
    10 0 0.0641849677 0.83714478
    12 0 0.0007932109 0.83086593
    13 0 0.7914607469 0.98313602
    15 0 0.4176381815 0.26584837
    16 0 0.8913181033 0.78030297
    17 0 0.3896608590 0.40215619
    18 0 0.6155101282 0.50859816
    20 0 0.4252773556 0.73868264
    21 0 0.9494552673 0.96442255
    22 0 0.6675511154 0.35240024
    23 0 0.6931768688 0.42016284
    26 1 0.6049248914 0.85045559
    27 1 0.8878736692 0.20937898
    28 1 0.0881897225 0.49006904
    29 1 0.3561574069 0.87316667
    30 1 0.7379366003 0.57722477
    34 1 0.0762609572 0.85021965
    ...
    ...
    ...
    

    与减号相同的内容将为您提供重新采样的遗漏观察结果:

    > df[-model$control$index$Resample1,]
       y          x1         x2
    2  0 0.495293215 0.16392350
    8  0 0.057934150 0.90044716
    11 0 0.794459804 0.46207494
    14 0 0.268692204 0.80763156
    19 0 0.515704584 0.82078298
    24 0 0.031054236 0.40846695
    25 0 0.218243275 0.40132438
    31 1 0.694632679 0.36696466
    32 1 0.002055724 0.99023235
    33 1 0.584879035 0.37515622
    ....
    ....
    

    【讨论】:

    • 这正是我正在寻找的答案!感谢您的宝贵时间。
    • 真的很高兴我能帮上忙 :)
    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2020-07-15
    • 2018-04-09
    • 2015-09-17
    相关资源
    最近更新 更多