for循环正在工作,但没有任何好处
我正在使用R做一个ML项目,我已经准备好了数据集并将数据分成10个相等的部分,但问题是我需要手动拟合模型10次(10倍CV)。我曾尝试使用for循环创建训练和测试数据,但每次它运行时,训练就是整个数据集,测试为空。有人能帮我吗for循环正在工作,但没有任何好处,r,R,我正在使用R做一个ML项目,我已经准备好了数据集并将数据分成10个相等的部分,但问题是我需要手动拟合模型10次(10倍CV)。我曾尝试使用for循环创建训练和测试数据,但每次它运行时,训练就是整个数据集,测试为空。有人能帮我吗 # Preparing the data data <- read.csv("./project.csv") id <- seq(1:103342) data[, 'id'] <- id for (i in 3:8) { data[,i] &
# Preparing the data
data <- read.csv("./project.csv")
id <- seq(1:103342)
data[, 'id'] <- id
for (i in 3:8) {
data[,i] <- as.factor(data[,i])
}
# splitting the data into 10 equal data frames
f <- rep(seq(1, 10), each=round(103342/10), length.out=103342)
df <- split(data, f)
lapply(df, dim)
# running 10-fold cross-validation and computing error rate and AUC for each run.
results <- matrix(nrow=10, ncol=2, dimnames= list(c(), c('error_rate', 'auc')))
for (i in 1:10) {
train <- data[!(data$id %in% df$`i`$id),]
test <- df$`i`
print(dim(test)) # Here is my problem the print statement will print null 10 times
glm.fit <- glm(canceled ~ ., data=train, family=binomial)
glm.prob <- predict(glm.fit, newdata=test, type="response")
...
}
#准备数据
数据我不确定从哪里得到测试此语法是由R中的split函数生成的,例如,如果您将数据帧拆分为3个拆分,则键入df$`1`
它将检索第一个拆分。但是您不能将$
与I
等变量一起使用。美元符号只是文字值的捷径。处理变量时,需要使用双括号[[]]
。在R代码中更常见的是看到df[[1]]
而不是df$`1`
非常感谢,我将尝试使用双括号,该错误消息听起来像是除了括号外,您还在使用美元符号。你用一个或另一个。它应该是test