【问题标题】:Nested Loops for Calculating MSE of Random Forest用于计算随机森林 MSE 的嵌套循环
【发布时间】:2020-08-28 16:31:18
【问题描述】:

我正在尝试计算通过更改 mtry、nodesize 和 ntree 参数创建的多个随机森林的 MSE。我将这些参数用作 randomForest 函数中的变量,并使用这些变量作为索引创建了 3 个“for”循环。我正在尝试将这些 MSE 变量存储在一维数组中并比较结果。我的问题是在代码的最后一行,我尝试在数组中添加 729 个 MSE 值。如何将它们存储在如下的嵌套循环中?

set.seed(425)
toyota_idx =sample(1:nrow(ToyotaCorolla),nrow(ToyotaCorolla)*0.7)
toyota_train = ToyotaCorolla[toyota_idx,]
toyota_test=ToyotaCorolla[-toyota_idx,]

##random forest
forest.mse=rep(0,729)

for (i in 1:9){
  for (j in 1:9){
    for (k in 1:9){
bag.toyota=randomForest(Price~.,data=toyota_train,mtry=i,nodesize=j,ntree=k,importance =TRUE)
toyota.prediction = predict(bag.toyota ,newdata=toyota_test)
forest.mse <- c(forest.mse, mean((toyota.prediction-toyota_test$Price)^2))
    }
  }
}

【问题讨论】:

    标签: r loops vector random-forest mse


    【解决方案1】:

    要返回哪个数组属于哪个 i,j,k 将是半疯狂的。

    尝试使用您的 mrty、nodesize 等创建一个 data.frame,并在每行的 MSE 中插入:

    set.seed(425)
    ToyotaCorolla = data.frame(Price = runif(100),matrix(rnorm(100*10),ncol=10))
    
    toyota_idx =sample(1:nrow(ToyotaCorolla),nrow(ToyotaCorolla)*0.7)
    toyota_train = ToyotaCorolla[toyota_idx,]
    toyota_test=ToyotaCorolla[-toyota_idx,]
    
    ##random forest
    forest.mse=rep(0,nrow(toyota_test))
    Grid = expand.grid(mtry=1:9,nodesize=1:9,ntree=1:9)
    Grid$forest.mse = NA
    
    for(i in 1:nrow(Grid)){
    
    bag.toyota=randomForest(Price~.,data=toyota_train,
    mtry=Grid$mtry[i],nodesize=Grid$nodesize[i],ntree=Grid$ntree[i],importance =TRUE)
    toyota.prediction = predict(bag.toyota ,newdata=toyota_test)
    Grid$forest.mse[i] = mean((toyota.prediction-toyota_test$Price)^2)
    
    }
    
    head(Grid)
      mtry nodesize ntree forest.mse
    1    1        1     1  0.1431115
    2    2        1     1  0.1652446
    3    3        1     1  0.2253738
    4    4        1     1  0.1352773
    5    5        1     1  0.1561385
    

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 2021-10-12
      • 2021-06-12
      • 2018-06-06
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2021-10-19
      • 2018-05-01
      相关资源
      最近更新 更多