foreach rf

作者: 柳叶刀与小鼠标 | 来源:发表于2018-05-04 00:02 被阅读11次
    library(randomForest)
    library(foreach)
    library(cvTools)
    set.seed(1234)
    K =10
    R = 3
    cv <- cvFolds(NROW(iris),K=K,R=R)
    grid <- expand.grid(ntree=c(10,100,200),mtry=c(3,4))
    result <- foreach(g=1:NROW(grid),.combine = rbind) %do% {
      foreach(r=1:R,.combine = rbind) %do% {
        foreach(k=1:K,.combine = rbind) %do% {
          validation_idx <- cv$subsets[which(cv$which ==k ),r]
          train  <- iris[-validation_idx,]
          validation <- iris[validation_idx,]
          m <- randomForest(Species~.,
                            data=train,
                            ntree=grid[g,"ntree"],
                             mtry=grid[g,"mtry"])
          predicted <- predict(m,newdata=validation)
          precision <- sum(prediced == validation$Species) / NROW(predicted)
          return(data.frame(g=g, precision=precision))
        }
      }
    }
    

    相关文章

      网友评论

        本文标题:foreach rf

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