2013-04-30 18 views
0

我正在试图根据袋装样品上的性能在袋装模型中设置投票。R,取出袋装样品以生成袋样品

construct.annet = function(trainset,n,p=1){ 
    annet.struct = vector(mode="list",length=n) 
    cat("Constructing Agregate Neural Network with ",p,"\n") 
    for(i in 1:n){ 
    cat("iteration ",i,"\n") 
    bsamp = trainset[sample(p*dim(trainset)[1],replace=T),] 
    annet.struct[[i]] = nnet(class~.,data=bsamp,size=sample(4:12,1),maxit=1000) 
    } 
    return(annet.struct) 
} 

打印迭代只是告诉我事情要花多长时间。至于为什么我随机改变隐藏层的大小,这似乎是当时要做的事情。

我想要做的是在每次迭代构建模型之后添加另一行,在该模型上测试出包示例中的模型,然后记录其预测准确性。然后我将使用这些数据来衡量最终模型中的类别百分比投票。 (较低性能的型号会减少重量等)

问题是,我无法弄清楚如何从传入数据中删除自举样本。而我的谷歌显然没有帮助。

谢谢。

回答

1

的想法是使用索引来直接提取这些样品被吸入引导子集,而不是样品:

ibsamp <- sample (nrow (trainset), replace = TRUE) 
annet.struct[[i]] = nnet (class ~ ., data = trainset [ibsamp, ], 
          size = sample (4 : 12, 1), maxit = 1000) 

然后可以使用subset找出哪些样品纳入训练集

itest <- setdiff (seq_len (nrow (trainset)), ibsamp) 
test [[i]] <- predict (annet.struct [[i]], newdata = trainset [itest, ]) 

(我建议重新命名traindata逼到data为清楚起见)

+0

我现在看到,你实际上并没有对观察结果进行抽样,仅仅是行数。在这种情况下,将要采样的数字乘以p是没有意义的。 (p是从一个不同的模型剩下的,我使用的是小于n的样本大小。)谢谢,这个工作很完美。 – Faydey 2013-05-02 06:51:34

+0

@ user24926:是的,我想知道那个'p',但决定把它留在你的代码中。我现在删除它。 – cbeleites 2013-05-02 09:36:49