https://mlr3gallery.mlr-org.com/posts/2020-03-11-basics-german-credit/
备查:mlr3计算features importance
#classif
task = TaskClassif$new("GermanCredit", german, target = "credit_risk")
train_set = sample(task$row_ids, 0.8 * task$nrow)
test_set = setdiff(task$row_ids, train_set)
learner_rf = lrn("classif.ranger", importance = "permutation")
learner_rf$train(task, row_ids = train_set)
learner_rf$importance()
importance = as.data.table(learner_rf$importance(), keep.rownames = TRUE)
colnames(importance) = c("Feature", "Importance")
ggplot(importance, aes(x = reorder(Feature, Importance), y = Importance)) +
geom_col() + coord_flip() + xlab("")
#regr
imp_num = po("imputehist", param_vals = list(affect_columns = selector_type(c("integer", "numeric"))))
imp_fct = po("imputeoor", param_vals = list(affect_columns = selector_type("factor")))
graph = imp_num %>>% imp_fct
task = TaskRegr$new(id = "moneyball", backend = moneyball, target = "rs")
# creates a normal learner however allows further embedding of PipeOp's.
polrn = PipeOpLearner$new(mlr_learners$get("regr.ranger"))
# sets number of trees to 1000, importance is for later
polrn$param_set$values = list(num.trees = 1000, importance = "permutation")
# the final learner is a graph consisting of the imputer and the normal learner.
lrn = GraphLearner$new(graph = graph %>>% polrn)
sort(lrn$model$regr.ranger$model$variable.importance, decreasing = TRUE)