mlr-org / mlr3

mlr3: Machine Learning in R - next generation
https://mlr3.mlr-org.com
GNU Lesser General Public License v3.0
927 stars 86 forks source link

How to predict in new data in mlr3 0.20.0 #1049

Closed DylanGuo1101 closed 1 month ago

DylanGuo1101 commented 1 month ago

I recently updated mlr3 to the latest version.

I used to predict new data with a trained model (lrn.rf) in mlr3 0.18.0 using the following code, and it worked fine. However, when I use the same code in mlr3 0.20.0, I receive the error: "Error: Setting row roles 'test'/'holdout' is no longer possible."

How can I predict new data without a target in mlr3 0.20.0?

Thank you!

lrn.rf <- readRDS('./aHCC-ML/tuned_RandomForest_model.rds')

dd <- data.frame( target = NA, Sex = 'Female', Age = 80, HBsAg = 'Positive', PLT = 44, AST = 20, AFP = 20, stringsAsFactors = FALSE ) |> as_tibble() |> mutate( HBsAg = case_when( HBsAg == 'Positive' ~ '1', HBsAg == 'Negative' ~ '0' ), Sex = case_when( Sex == 'Female' ~ '1', Sex == 'Male' ~ '0' ) ) |> mutate(across(c(Sex, HBsAg), factor))

res <- predict(lrn.rf, dd, predict_type = "prob") Error: Setting row roles 'test'/'holdout' is no longer possible.

task <- TaskClassif$new("test", backend = dd, target = "target") Error: Target column 'target' must be a factor or ordered factor

be-marc commented 1 month ago

I see that you load a learner from disk. I assume this learner was created with an older mlr3 version? If you train the model with the version you are predicting with, there should be no problems. The error message points to a recent change in mlr3.

be-marc commented 1 month ago

Please reopen if the issue persist.

DylanGuo1101 commented 1 month ago

I see that you load a learner from disk. I assume this learner was created with an older mlr3 version? If you train the model with the version you are predicting with, there should be no problems. The error message points to a recent change in mlr3.

Thank you!I have solved this problem after retraining the model.