Training Model in Caret Using F1 Metric

爱⌒轻易说出口 提交于 2020-12-31 14:55:38

问题


I am trying to fit a random forest model to my dataset and I would like to select the best model based off of the F1 score. I saw a post here describing the code necessary. I attempted to copy the code but I am getting the error

"Error in { : task 1 failed - "could not find function "F1_Score"

while I run the train function. (FYI the variable I am trying to predict ("pass") is a two class factor "Fail" and "Pass")

See Code Below:

library(MLmetrics)
library(caret)
library(doSNOW)

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)
}



train.control <- trainControl(method = "repeatedcv",
                              number = 10,
                              repeats = 3,
                              classProbs = TRUE,
                              summaryFunction = f1,
                              search = "grid")


tune.grid <- expand.grid(.mtry = seq(from = 1, to = 10, by = 1))


cl <- makeCluster(3, type = "SOCK")
registerDoSNOW(cl)
random.forest.orig <- train(pass ~ manufacturer+meter.type+premise+size+age+avg.winter+totalizer, 
                     data = meter.train,
                     method = "rf",
                     tuneGrid = tune.grid,
                     metric = "F1",
                     weights = model_weights,
                     trControl = train.control)
stopCluster(cl)

回答1:


I've rewritten the f1 function not using the MLmetrics library and it seems to work. See below for a working code to create a f1 score:

f1 <- function (data, lev = NULL, model = NULL) {
  precision <- posPredValue(data$pred, data$obs, positive = "pass")
  recall  <- sensitivity(data$pred, data$obs, postive = "pass")
  f1_val <- (2 * precision * recall) / (precision + recall)
  names(f1_val) <- c("F1")
  f1_val
} 

train.control <- trainControl(method = "repeatedcv",
                          number = 10,
                          repeats = 3,
                          classProbs = TRUE,
                          #sampling = "smote",
                          summaryFunction = f1,
                          search = "grid")


tune.grid <- expand.grid(.mtry = seq(from = 1, to = 10, by = 1))


cl <- makeCluster(3, type = "SOCK")
registerDoSNOW(cl)
random.forest.orig <- train(pass ~ manufacturer+meter.type+premise+size+age+avg.winter+totalizer, 
                 data = meter.train,
                 method = "rf",
                 tuneGrid = tune.grid,
                 metric = "F1",
                 trControl = train.control)
stopCluster(cl)


来源:https://stackoverflow.com/questions/47820750/training-model-in-caret-using-f1-metric

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!