【发布时间】:2020-11-01 19:39:53
【问题描述】:
我正在使用 Iris 数据集来拟合逻辑回归。我想拟合每种特征组合,看看我能得到什么以获得最佳 AUC 分数。
例如,我将适合 4 * 3 * 2 * 1 = 24 个模型。 这本质上是对每个特征组合的置换。 我想把它输出到一个表格中,看看哪个组合给了我最好的分数。
数据集的前 3 行
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
拟合一个模型
这里只是拟合其中一个模型并获得 AUC
## make it binary classification
library(ROCR)
library(tidyverse)
iris.small <- iris %>%
filter(Species %in% c("virginica", "versicolor"))
is.na(iris.small$Species) <- iris.small$Species == "setosa"
iris.small$Species <- factor(iris.small$Species)
## 75% of the sample size
smp_size <- floor(0.75 * nrow(iris.small))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(iris.small)), size = smp_size)
train <- iris.small[train_ind, ]
test <- iris.small[-train_ind, ]
mod1 <- glm(Species ~ .,
data = train,
family = binomial(link = "logit")
)
pred_probs <- predict(mod1, newdata = test, type = "response")
pred_obj <- ROCR::prediction(pred_probs, test$Species)
perf_obj <- ROCR::performance(pred_obj, measure = "tpr", x.measure = "fpr")
auc <- performance(pred_obj, measure = "auc")
auc <- auc@y.values[[1]]
print(auc)
预期输出 每个拟合的 AUC 分数表。会有两列,拟合中的特征和 AUC 分数。
另外,一般来说这是一个好主意吗?拟合 24 个模型可能看起来不是很理想,但我不确定如何确定哪些特征组合是最优化的。
感谢您的帮助。
【问题讨论】:
-
为什么是 24 个模型?如果你有4个特征,不应该是model1:4个特征,model2:3个特征,model3:2个特征和model4:1个特征?
-
我会为每个单独的功能做一个拟合:这将是 4 个模型。然后拟合两个特征的每个组合,依此类推。
标签: r loops machine-learning tidyverse logistic-regression