Caret package Custom metric

后端 未结 2 1073
清酒与你
清酒与你 2020-12-24 09:07

I\'m using the caret function \"train()\" in one of my project and I\'d like to add a \"custom metric\" F1-score. I looked at this url caret package But I cannot understand

2条回答
  •  情书的邮戳
    2020-12-24 09:36

    You should look at The caret Package - Alternate Performance Metrics for details. A working example:

    library(caret)
    library(MLmetrics)
    
    set.seed(346)
    dat <- twoClassSim(200)
    
    ## See https://topepo.github.io/caret/model-training-and-tuning.html#metrics
    f1 <- function(data, lev = NULL, model = NULL) {
      f1_val <- F1_Score(y_pred = data$pred, y_true = data$obs, positive = lev[1])
      c(F1 = f1_val)
    }
    
    set.seed(35)
    mod <- train(Class ~ ., data = dat,
                 method = "rpart",
                 tuneLength = 5,
                 metric = "F1",
                 trControl = trainControl(summaryFunction = f1, 
                                          classProbs = TRUE))
    

提交回复
热议问题