Using an example from the very helpful mlr3 book, I am trying to simply return the average score of the stacked model output. Can someone please explain how to do this using mlr3? I've tried using both LearnerClassifAvg$new( id = "classif.avg") and po("classifavg"), but not sure I've applied these correctly, thank you
Example:
library("magrittr")
library("mlr3learners") # for classif.glmnet
task = mlr_tasks$get("iris")
train.idx = sample(seq_len(task$nrow), 120)
test.idx = setdiff(seq_len(task$nrow), train.idx)
rprt = lrn("classif.rpart", predict_type = "prob")
glmn = lrn("classif.glmnet", predict_type = "prob")
# Create Learner CV Operators
lrn_0 = PipeOpLearnerCV$new(rprt, id = "rpart_cv_1")
lrn_0$param_set$values$maxdepth = 5L
lrn_1 = PipeOpPCA$new(id = "pca1") %>>% PipeOpLearnerCV$new(rprt, id = "rpart_cv_2")
lrn_1$param_set$values$rpart_cv_2.maxdepth = 1L
lrn_2 = PipeOpPCA$new(id = "pca2") %>>% PipeOpLearnerCV$new(glmn)
# Union them with a PipeOpNULL to keep original features
level_0 = gunion(list(lrn_0, lrn_1,lrn_2, PipeOpNOP$new(id = "NOP1")))
# Cbind the output 3 times, train 2 learners but also keep level
# 0 predictions
level_1 = level_0 %>>%
PipeOpFeatureUnion$new(4) %>>%
PipeOpCopy$new(3) %>>%
gunion(list(
PipeOpLearnerCV$new(rprt, id = "rpart_cv_l1"),
PipeOpLearnerCV$new(glmn, id = "glmnt_cv_l1"),
PipeOpNOP$new(id = "NOP_l1")
))
level_1$plot(html = FALSE)
level_2 <- level_1 %>>%
PipeOpFeatureUnion$new(3, id = "u2") %>>%
LearnerClassifAvg$new( id = "classif.avg")
level_2$plot(html = FALSE)
lrn = GraphLearner$new(level_2)
lrn$
train(task, train.idx)$
predict(task, test.idx)$
score()
## returns: Error: Trying to predict response, but incoming data has no factors
If we do not pass the features to
classif.avg
(PipeOpNOP
) we still end up with the same error:Created on 2021-03-27 by the reprex package (v1.0.0)
This error can be migitated by setting the correct predict type of the learner:
check error message here: https://github.com/cran/mlr3pipelines/blob/master/R/LearnerAvg.R
Solution demonstrated with a simpler ensemble
Created on 2021-03-28 by the reprex package (v1.0.0)