【发布时间】:2021-10-25 20:31:14
【问题描述】:
我已经找到了这个Training Model in Caret Using F1 Metric,它描述了如何使用 F1 作为使用自定义 summaryFunction 的指标。
但这仅适用于二进制分类。我想将它用于多类数据集。
到目前为止,我所做的如下:
f1 <- function(data, lev = NULL, model = NULL) {
print(data)
precision <- posPredValue(data$pred, data$obs, positive = "pass")
recall <- sensitivity(data$pred, data$obs, positive = "pass")
f1_val <- (2*precision*recall) / (precision + recall)
names(f1_val) <- c("F1")
f1_val
}
train.control <- trainControl(method = "repeatedcv",
number = 2,
summaryFunction = defaultSummary,
classProbs = TRUE,
search = "grid")
tune.grid <- expand.grid(.mtry = seq(from = 5, to = 10, by = 1))
random.forest.orig <- train(target~.,
data = data.small,
method = "rf",
tuneGrid = tune.grid,
metric = "F1",
trControl = train.control)
random.forest.orig
正如预期的那样,它会产生以下错误:
Error in posPredValue.default(data$pred, data$obs, positive = "pass") : input data must have the same two levels
我希望有人已经这样做并且可以帮助我解决这个问题。不然我也想知道为什么f1函数中使用的数据框只包含10行...
【问题讨论】: