【问题标题】:Progress bar in random forest model in RR中随机森林模型的进度条
【发布时间】:2015-12-23 20:21:23
【问题描述】:

我在 R 中使用 randomForest 模型。

对于大量的树,我的程序需要很长时间才能完成。

在“randomForest”函数中,我可以使用“do.trace=TRUE”来查看实时进度。在 R 控制台上实时输出的示例如下

ntree    OOB      1      2      3      4      5      6      7      8      9 
100:   2.31%  7.14%  2.08%  0.00%  2.25% 10.81%  0.90%  0.00%  0.00%  1.72% 
200:   1.95%  7.14%  2.08%  0.00%  2.25%  8.11%  0.00%  0.00%  0.00%  1.72% 
300:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
400:   1.95%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  3.45% 
500:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
600:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
700:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
800:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
900:   1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 
1000:  1.78%  7.14%  2.08%  0.00%  1.69%  8.11%  0.00%  0.00%  0.00%  1.72% 

第一行(100:2.31% ....)排在第一位。 1 秒后,它来到第 2 行,依此类推。我想修改这个输出。

当第一行到来时,我只需要从整行中抓取“100”并在 R 控制台上只显示“100”而不是显示整行。其余行也是如此。

[ 我试过 sink()。但它不会工作,因为sink 将完整的输出写入输出文件]

[我在 randomForest 函数中搜索了do.trace 选项。但我迷失了自己,因为我怀疑它调用了 C 程序;虽然我不确定。]

我想在 R 控制台上获取实时输出。

注意:我看到了以下问题。

  1. https://github.com/jni/ray/issues/33
  2. Problematic Random Forest training runtime when using formula interface

【问题讨论】:

  • 我在 Rhelp 上看到了您的帖子。感谢您在重新发布之前等待适当的时间间隔。那时我确实尝试检查 R 代码并确定您对这些消息来自底层 C 代码的预感是正确的。我的编辑只是为了更正大小写并使消息更符合 SO 发布指南。由于至少有一个链接答案与 randomForest 调用的语法有关,因此您应该发布您的答案。

标签: r random-forest


【解决方案1】:

已下载:https://cran.r-project.org/src/contrib/randomForest_4.6-10.tar.gz

查看 refRF.C 的 C 代码时(我怀疑 classRF.C 在分类问题时也使用do.trace 调用),然后跟随 @ 接收到的“jprint”标志987654323@-flag在周围的R代码中,我们看到:

/* print header for running output */
    if (*jprint <= *nTree) {
    Rprintf("     |      Out-of-bag   ");
    if (*testdat) Rprintf("|       Test set    ");
    Rprintf("|\n");
    Rprintf("Tree |      MSE  %%Var(y) ");
    if (*testdat) Rprintf("|      MSE  %%Var(y) ");
    Rprintf("|\n");
    }

还有:

 /* Print running output. */
    if ((j + 1) % *jprint == 0) {
        Rprintf("%4d |", j + 1);
        Rprintf(" %8.4g %8.2f ", errb, 100 * errb / varY);
        if(*labelts == 1) Rprintf("| %8.4g %8.2f ",
                                  errts, 100.0 * errts / varYts);
        Rprintf("|\n");
    }
    mse[j] = errb;
    if (*labelts) msets[j] = errts;

将该代码修剪到仅以您想要的形式发出第一百个树通知的程度应该不是特别困难。

【讨论】:

  • 谢谢。我会试试的。
猜你喜欢
  • 2020-02-25
  • 2022-11-11
  • 2018-09-26
  • 1970-01-01
  • 2021-04-26
  • 2016-11-30
  • 2021-04-26
  • 2019-07-10
  • 2017-08-11
相关资源
最近更新 更多