【发布时间】:2021-05-05 05:18:16
【问题描述】:
我正在使用 R 编程语言。我正在学习如何迭代地循环一个过程(例如,生成一些随机数据并适合不同的决策树)。在上一个问题 (R: Saving the Results of a Loop) 中,我学习了如何生成随机数据、拟合不同的决策树并记录它们的准确度:
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]
}
}
}
#view the final results
head(df)
数据框“df”包含最终结果。我担心的是:如果你想多次迭代这个循环,“df”的大小会变得非常大。假设我只想保留“df”的“前 20 行”(基于 df$Accuracy 的降序值)。我可以这样做:
#sort "df" by (descending values of) "Accuracy":
df_sort <- df[order(-df$Accuracy),]
#select first 20 rows
df_final = df_sort[1:20,]
但我担心计算机内存的限制可能会阻止创建“df”(对于大量迭代)。
我的问题:有没有办法阻止“df”超过 20 行?例如
- 填充“df”的前 20 行
- 如果第 21 行的准确率低于前 20 行中的任何一行,则删除
- 如果第 21 行的准确度大于前 20 行中最小的准确度,则保留第 21 行并删除准确度最小的行
这样,“df”的大小永远不会超过 20 行。
有人可以告诉我怎么做吗?
谢谢
【问题讨论】:
标签: r loops for-loop iteration decision-tree