【问题标题】:For loop in R does not output correctlyR中的for循环无法正确输出
【发布时间】:2021-10-29 05:18:25
【问题描述】:

我的循环代码输出不正确。我想遍历Test_Cs 并附加到test_accuracyTest_Cs 中每个C 值相关联的准确率。由于正在测试 11 个 C,我希望 test_accuracy 也有 11 个值。但是,我的test_accuracy 输出有超过 1000 个值,这表明我的代码出了问题。

#C is the sum of squared of all errors. The higher C means the more errors in the model and the model has low accuracy rate; C also >= 0. I will create a list of 11 C values I’d like to test the model with.

Test_Cs <- seq(0.01, 10000, length = 11)

#create a list of 0 to hold our predicted accuracies with different C values in the Test_Cs later on

test_accuracy <- rep(0, length(11))
x = 1

#loop multiple C through the KSVM model:

for (i in Test_Cs){
#call ksvm
   ksvm_model <- ksvm(as.matrix(data[,1:10]), as.factor(data[,11]), scaled=TRUE, type= “C-svc”, kernel =“vanilladot”, C=i)
#see what the model predicts
   pred <- predict(ksvm_model,data[,1:10])

# see what fraction of the model's predictions match the actual classification

   accu_percentage <- sum(pred == data[,11])/nrow(data)
   test_accuracy[[x]] <- accu_percentage
   x <- x+1
}

print(test_accuracy)

作为命令 print(test_accuracy) 的结果,我得到了 1000 多个输出值,其中大部分是 NA,如下所示:

  [1] "C"                            "C:1000.009:0.862385321100917" "C:1000.009:0.862385321100917"
   [4] "C:0.01:0.863914373088685"     "C:1000.009:0.862385321100917" "C:2000.008:0.862385321100917"
   [7] "C:3000.007:0.862385321100917" "C:4000.006:0.862385321100917" "C:5000.005:0.862385321100917"
  [10] "C:6000.004:0.862385321100917" "C:7000.003:0.862385321100917" "C:8000.002:0.862385321100917"
  [13] "C:9000.001:0.862385321100917" "C:10000:0.862385321100917"    "0.862385321100917"           
  [16] "0.863914373088685"            "0.862385321100917"            "0.862385321100917"           
  [19] "0.862385321100917"            "0.862385321100917"            "0.862385321100917"           
  [22] "0.862385321100917"            "0.862385321100917"            "0.862385321100917"           
  [25] "0.862385321100917"            "0.862385321100917"            "0.863914373088685"           
  [28] "0.862385321100917"            "0.862385321100917"            "0.862385321100917"           
  [31] "0.862385321100917"            "0.862385321100917"            "0.862385321100917"           
  [34] "0.862385321100917"            "0.862385321100917"            "0.862385321100917"           
  [37] "0.862385321100917"            NA                             NA                            
  [40] NA                             NA                             NA                            
  [43] NA                             NA                             NA                            
  [46] NA                             NA                             NA                            
  [49] NA                             NA                             NA                            
  [52] NA                             NA                             NA                            
  [55] NA                             NA                             NA                            
  [58] NA                             NA                             NA                            
  [61] NA                             NA                             NA                            
  [64] NA                             NA                             NA                            
  [67] NA                             NA                             NA                            
  [70] NA                             NA                             NA                            
  [73] NA                             NA                             NA                            
  [76] NA                             NA                             NA                            
  ....                       
 [988] NA                             NA                             NA                            
 [991] NA                             NA                             NA                            
 [994] NA                             NA                             NA                            
 [997] NA                             NA                             NA                            
[1000] "0.862385321100917"           
 [ reached getOption("max.print") -- omitted 9000 entries ]

如果您能帮我诊断问题,将不胜感激。我认为我使用ix 的方式存在一些逻辑缺陷,谢谢。

【问题讨论】:

  • 您创建的对象test_accuracy 不是您可以分配的列表。尝试test_accuracy &lt;- list() 而不是test_accuracy &lt;- rep(0, length(11))

标签: r loops svm


【解决方案1】:

如果您在 Test_Cs 中设置 11 个参数来尝试,您的计数器 x 将停止在 11。我为 test_accuracy 创建了一个空列表

现在它可以正常工作了;

Test_Cs <- seq(0.01, 10000, length = 11)

test_accuracy <- list()
x = 1

for (i in Test_Cs){
    
   ksvm_model <- ksvm(as.matrix(data[,1:10]), as.factor(data[,11]), scaled=TRUE, type= "C-svc", kernel ="vanilladot", C=i)
   pred <- predict(ksvm_model,data[,1:10])

   accu_percentage <- sum(pred == data[,11])/nrow(data)
   test_accuracy[[x]] <- accu_percentage
   x <- x+1
}

print(test_accuracy)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多