【问题标题】:Use F1 Score as metric for multiclass prediction使用 F1 分数作为多类预测的指标
【发布时间】: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行...

【问题讨论】:

    标签: r dataframe r-caret caret


    【解决方案1】:

    解决办法:

    f1 <- function(data, lev = NULL, model = NULL) {
        f1_val <- f1_score(data$pred,data$obs)
        names(f1_val) <- c("F1")
        f1_val
    }
    
    f1_score <- function(predicted, expected, positive.class="1") {
        predicted <- factor(as.character(predicted), levels=unique(as.character(expected)))
        expected  <- as.factor(expected)
        cm = as.matrix(table(expected, predicted))
    
        precision <- diag(cm) / colSums(cm)
        recall <- diag(cm) / rowSums(cm)
        f1 <-  ifelse(precision + recall == 0, 0, 2 * precision * recall / (precision + recall))
    
        #Assuming that F1 is zero when it's not possible compute it
        f1[is.na(f1)] <- 0
    
        #Binary F1 or Multi-class macro-averaged F1
        ifelse(nlevels(expected) == 2, f1[positive.class], mean(f1))
    }
    
    
    train.control <- trainControl(method = "cv",
                                  number = 2,
                                  summaryFunction = f1,
                                  classProbs = TRUE, 
                                  search = "grid")
                                  
    tune.grid <- expand.grid(.mtry = seq(from = 10, to = 15, by = 1))
                                  
    random.forest.orig <- train(target~.,
                                data = data.small,
                                method = "rf",
                                tuneGrid = tune.grid,
                                metric = "F1",
                                trControl = train.control)
    
    random.forest.orig
    

    希望对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 2021-07-19
      • 2022-12-20
      • 1970-01-01
      • 2018-03-25
      • 2020-08-21
      • 2019-10-09
      • 2019-10-12
      • 2017-12-30
      相关资源
      最近更新 更多