【问题标题】:Record a series of plot using a loop使用循环记录一系列情节
【发布时间】:2015-01-17 18:44:58
【问题描述】:

我确实有一个名为primes 的向量,长度为 100,包含前一百个素数,如下所示:

primes
  [1]   2   3   5   7  11  13  17  19  23  29  31  37  41  43  47  53  59  61  67  71  73  79  83  89  97 101 103 107 109
 [30] 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271
 [59] 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449
 [88] 457 461 463 467 479 487 491 499 503 509 521 523 541

我还有一个名为 optm 的数据框,所有这些素数都作为变量,学习率下降作为行。例如,我通过绘制第 37 个素数得到:

plot.default(optm$step, optm$seed37, type = "l", ylab = "learning rate",
             xlab = "first third iterations", main = 'seed37')

我想在 4*5 多帧上提取并保存所有这 100 个图。因此,我想要 5 个文件(= 100 个图),这就是为什么我在以下循环中实现 a 作为记录限制:

   a <- 20
    par(mfrow = c(4, 5))
    for(i in primes) { 
      cln <- paste0("seed",i)
      plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
      for(j in 1:length(primes)) { 
      if (j==a){
             dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
             a = a + 20
             dev.off()                 
    }
    }
    }

问题是我电脑上的记录文件相似并且具有相同的情节(第一个种子2)。我的错在哪里?

编辑:

a = 0
par(mfrow = c(4, 5))
for(i in primes) { for(j in length(primes)) {cln <- paste0("seed",i)
               plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)  
               a = (a +1)
               if(a==20) {
                dev.copy(png, paste0(j,'optm.png'),width=8,height=6,units="in",res=100)
                dev.off()
                                                          }
     }
}

这种模式适用于前 20 个。所以我编写了这样的序列:

foo = seq(0, length(primes), by=20)

现在,我需要在 if 命令中调用 foo 的任何值,而不是 20。我该如何处理?

【问题讨论】:

    标签: r loops plot


    【解决方案1】:

    你只需要 1 个 for 循环:

    a <- 20
    par(mfrow = c(4, 5))
    for(i in seq(length(primes))) { 
      cln <- paste0("seed",primes[i])
      plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)
      if (i&&a == 0){
             dev.copy(png, paste0(i,'optm.png'),width=8,height=6,units="in",res=100)
             dev.off()                 
      }
    }
    

    【讨论】:

    • 您的代码给了我一个错误Error: unexpected '{' in "for(i in seq(length(primes)) {" 我正在尝试使用foo = seq(0, length(primes), by=20) 解决问题,但它还没有解决。感谢您的帮助
    • 哎呀。在 for 循环中缺少括号(已修复)并删除了不必要的行
    • 我只是指出你的代码的问题,我不能使用 i 作为序列,因为我的标题列对应于素数 2,3,5,7,11.... 而不是 1 ,2,3,4... cln 是一种根据相关素数调用情节标题的方法
    • 我不知道为什么seq() 是个问题。 cln &lt;- paste0("seed",primes[i]) 行得到了正确的列名,不是吗?你试过修改后的代码吗?
    • 如果您想坚持使用您的代码,请尝试在您的 if 块中设置 a == 0
    【解决方案2】:

    很抱歉回答我自己的问题,但我确实找到了方法。肯定不是最好的,但以下代码正在运行。

    a = 0
    b = 20
    par(mfrow = c(4, 5))
    for(i in primes) {cln <- paste0("seed",i)
                   plot.default(optm$step, optm[,cln], type = "l", ylab = "learning rate", xlab = "step first third", main = cln)  
                   a = (a +1)
                   print(a)
                   if (a == b) {
                   dev.copy(png, paste0(a,'optm.png'),width=8,height=6,units="in",res=100)
                   b =(b +20)
                   dev.off()                                                        }
         }
    

    每次a 到达b 都会记录剧情系列。每次a 达到bb 增加 20。我对任何优化它的方法持开放态度,尤其是通过删除 a 和 b 并调用类似这样的一种 seq:

    foo = seq(0, length(primes), by=20)
    

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 2011-09-14
      • 2011-02-04
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2020-05-01
      相关资源
      最近更新 更多