Skip to content

Bug (?) in predict_class #1267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chillerb opened this issue Mar 24, 2025 · 0 comments
Open

Bug (?) in predict_class #1267

chillerb opened this issue Mar 24, 2025 · 0 comments

Comments

@chillerb
Copy link

The problem

I registered my own parsnip model for classification.
In set_pred, I used a post function to post-process my results into the expected form like this:

 post = function(x, object) {
        prob_to_class_2(x[,1], object)
      },

where prob_to_class_2 is a reimplementation of parsnip's (not exported) function:

prob_to_class_2 <- function(x, object) {
    x <- ifelse(x >= 0.5, object$lvl[2], object$lvl[1])
  parsnip::format_class(unname(x))
}

However, this gave me the following error:

Warning: Unknown or uninitialised column: `values`.Error in `$<-` at parsnip/R/predict_class.R:51:9:
! Assigned data `factor(as.character(res$values), levels = object$lvl, ordered = object$ordered)` must be compatible with existing data.
✖ Existing data has 8 rows.
✖ Assigned data has 0 rows.
ℹ Only vectors of size 1 are recycled.
Caused by error in `vectbl_recycle_rhs_rows()` at tibble/R/subassign-backend.R:35:5:
! Can't recycle input of size 0 to size 8.
Run `rlang::last_trace()` to see where the error occurred.

After looking at parsnip's source code, I think I know what is going on:

# post-process the predictions
if (!is.null(object$spec$method$pred$class$post)) {
res <- object$spec$method$pred$class$post(res, object)
}
# coerce levels to those in `object`
if (is.vector(res) || is.factor(res)) {
res <- factor(as.character(res), levels = object$lvl, ordered = object$ordered)
} else {
if (!inherits(res, "tbl_spark")) {
# Now case where a parsnip model generated `res`
if (is.data.frame(res) && ncol(res) == 1 && is.factor(res[[1]])) {
res <- res[[1]]
} else {
res$values <- factor(as.character(res$values),
levels = object$lvl,
ordered = object$ordered)
}
}
}

Although the object returned by parsnip::format_class is a data.frame with 1 column, the first column isn't a factor but characters. Therefore, the first condition fails, and control is given to the else branch, trying to access a non-existent column res$values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant