首页 > 解决方案 > R中的for循环无法正确输出

问题描述

我的循环代码输出不正确。我想循环遍历并附Test_Cs加到  test_accuracy与每个 C 值相关联的准确率Test_Cs。由于有 11 个 Cs 正在测试,我希望它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 ]

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

标签: rloopssvm

解决方案


如果您设置 11 个参数来尝试Test_Cs,您的计数器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)

推荐阅读