-
Notifications
You must be signed in to change notification settings - Fork 93
Open
Description
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:
Lines 37 to 56 in 04d1bc9
# 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
.
Metadata
Metadata
Assignees
Labels
No labels