r - 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
。
解决方案
如果您设置 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)
推荐阅读
- android - Jetpack Compose 可以从任何线程绘制/更新 UI 吗?
- windows - 如何从 Windows 批处理文件 (.bat) 文件中删除受 Windows 保护的 PC?
- javascript - 从父组件清除子组件状态
- mysql - 在两个事件发生之间聚合数据
- java - DDD:聚合和实体的关系
- r - R中的GGPlot2条形图中未生成条形图
- c - 在 c 中使用 memcpy 后,我尝试打印整数数组
- r - 如何生成具有以编程方式设置的参数值的 .rmd 文件
- facebook - V11.0 Facebook Graph API - 自/直到不填充整个日期范围
- python - 并非所有参数都在 SQL 语句 Python - MySql 中使用