MLmetrics
MLmetrics copied to clipboard
error when all predicted values are 1 for F1_Score function
Hi,
I encounter the error:
Error in FUN(X[[i]], ...): only defined on a data frame with all numeric variables
Traceback:
1. MLmetrics::F1_Score(y_true, y_pred_resp)
2. Precision(y_true, y_pred, positive)
3. Summary.data.frame(structure(list(Freq = integer(0)), row.names = integer(0), class = "data.frame"),
. na.rm = FALSE)
4. lapply(args, function(x) {
. x <- as.matrix(x)
. if (!is.numeric(x) && !is.complex(x))
. stop("only defined on a data frame with all numeric variables")
. x
. })
5. FUN(X[[i]], ...)
6. stop("only defined on a data frame with all numeric variables")
if I try to calculate the F1 score. I found that the cause in my case was that all predicted values were 1.
Reproducible example:
# this works:
y_true <- sample(c(0, 1), size = 20, replace = T)
y_pred_resp <- sample(c(0, 1), size = 20, replace = T)
f_one <- MLmetrics::F1_Score(y_true, y_pred_resp, positive = 1)
# this is a possible real worl scenario but produces the error:
y_true <- sample(c(0, 1), size = 20, replace = T)
y_pred_resp <- rep(1, 20)
f_one <- MLmetrics::F1_Score(y_true, y_pred_resp, positive = 1)