我使用的是 R 编程语言。我正在学习循环以及如何存储循环的结果。例如,我编写了以下代码来循环一个函数(生成随机数据并拟合不同的决策树):
#load libraries
library(caret)
library(rpart)
#generate data
a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5) )
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)
#place results in table
#final_table = matrix(1, nrow = 10, ncol=10)
e <- d
vec1 <- sample(200:300, 5)
vec2 <- sample(400:500,5)
vec3 <- sample(700:800,5)
for (i in seq_along(vec1)) {
for (j in seq_along(vec2)) {
for (k in seq_along(vec3)) {
# d <- e
d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i] & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j] & d$group_1 < vec3[k] , 2,3))))
d$group_2 = as.factor(d$group_2)
fitControl <- trainControl(## 10-fold CV
method = "repeatedcv",
number = 2,
## repeated ten times
repeats = 1)
TreeFit <- train(group_2 ~ ., data = d[,-5],
method = "rpart",
trControl = fitControl)
pred <- predict(
TreeFit,
d[,-5])
con <- confusionMatrix(
d$group_2,
pred)
#update results into table
#final_table[i,j] = con$overall[1]
acc=c(vec1[i],vec2[j],vec3[k],con$overall[1])
print(acc)
}
}
}
我有兴趣将“acc”的结果保存到表格(或数据框)中。我能够打印“acc”的所有值,但是当我正式查看“acc”的结果时:只显示最后一行。
我的问题:是否可以将整个打印输出(即“acc”)存储到表格中?
谢谢
答案 0 :(得分:2)
您在此处发布的非常好的示例。要启动您的数据框,我们可以添加:
#place results in table
final_table = data.frame(vec1=double(),vec2=double(),vec3=double(),Accuracy=double())
我们可以将 acc
的输出存储到其中:
#update results into table
acc=c(vec1[i],vec2[j],vec3[k],con$overall[1])
final_table<-rbind(final_table,acc)
答案 1 :(得分:1)
您可以使用 expand.grid
创建 vec1
、vec2
和 vec3
的所有可能组合,并在数据帧中的每次迭代中保存 con$overall[1]
。>
library(caret)
library(rpart)
#generate data
a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5))
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)
e <- d
vec1 <- sample(200:300, 5)
vec2 <- sample(400:500,5)
vec3 <- sample(700:800,5)
z <- 0
df <- expand.grid(vec1, vec2, vec3)
df$Accuracy <- NA
for (i in seq_along(vec1)) {
for (j in seq_along(vec2)) {
for (k in seq_along(vec3)) {
# d <- e
d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i] & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j] & d$group_1 < vec3[k] , 2,3))))
d$group_2 = as.factor(d$group_2)
fitControl <- trainControl(## 10-fold CV
method = "repeatedcv",
number = 2,
## repeated ten times
repeats = 1)
TreeFit <- train(group_2 ~ ., data = d[,-5],
method = "rpart",
trControl = fitControl)
pred <- predict(
TreeFit,
d[,-5])
con <- confusionMatrix(
d$group_2,
pred)
#update results into table
#final_table[i,j] = con$overall[1]
z <- z + 1
df$Accuracy[z] <- con$overall[1]
}
}
}
head(df)
# Var1 Var2 Var3 Accuracy
#1 300 492 767 0.299
#2 202 492 767 0.299
#3 232 492 767 0.299
#4 293 492 767 0.376
#5 231 492 767 0.299
#6 300 435 767 0.331
答案 2 :(得分:1)
有一种替代方法,将每次迭代的结果保存为列表的元素,然后将结果组合起来。通过在循环开始之前分配列表,我们可以避免代价高昂的 growing in a loop。此外,这种方法在循环顺序发生变化的情况下是稳健的。
#load libraries
library(caret)
library(rpart)
#generate data
a = rnorm(1000, 10, 10)
b = rnorm(1000, 10, 5)
c = rnorm(1000, 5, 10)
group <- sample( LETTERS[1:2], 1000, replace=TRUE, prob=c(0.5,0.5) )
group_1 <- 1:1000
#put data into a frame
d = data.frame(a,b,c, group, group_1)
d$group = as.factor(d$group)
#place results in table
#final_table = matrix(1, nrow = 10, ncol=10)
e <- d
vec1 <- sample(200:300,2)
vec2 <- sample(400:500,2)
vec3 <- sample(700:800,2)
result_list <- vector("list", length(vec1)*length(vec2)*length(vec3))
result_count <- 0L
for (i in seq_along(vec1)) {
for (j in seq_along(vec2)) {
for (k in seq_along(vec3)) {
# d <- e
d$group_2 = as.integer(ifelse(d$group_1 < vec1[i] , 0, ifelse(d$group_1 >vec1[i] & d$group_1 < vec2[j] , 1, ifelse(d$group_1 >vec2[j] & d$group_1 < vec3[k] , 2,3))))
d$group_2 = as.factor(d$group_2)
fitControl <- trainControl(## 10-fold CV
method = "repeatedcv",
number = 2,
## repeated ten times
repeats = 1)
TreeFit <- train(group_2 ~ ., data = d[,-5],
method = "rpart",
trControl = fitControl)
pred <- predict(
TreeFit,
d[,-5])
con <- confusionMatrix(
d$group_2,
pred)
#update results into table
#final_table[i,j] = con$overall[1]
acc=c(vec1=vec1[i],vec2=vec2[j],vec3=vec3[k],con$overall[1])
result_count <- result_count + 1L
result_list[[result_count]] <- acc
print(acc)
}
}
}
(final_table <- do.call(rbind, result_list))