I was wonder how I could reorder the importance of features produced from a trained model from 'mlr3' to match the order of the feature names from task$feature_names
? For example, if I create a task
and model from mlr3
like so:
#Get data
aq <- data.frame(airquality)
aq <- na.omit(aq)
# Create mlr3 task and model
aq_T = TaskRegr$new(id = "aq", backend = aq, target = "Ozone")
aqLrn = lrn("regr.ranger", importance = "permutation")
aq_M <- aqLrn$train(aq_T)
and then I take a look at both the feature names and the importance values, it results in the following:
name <- aq_T$feature_names #task feature names
imp <- aq_M$importance(). #models importance values
name
imp
> name
[1] "Solar.R" "Wind" "Temp" "Month" "Day"
> imp
Temp Wind Solar.R Month Day
597.43488 455.69392 112.31195 30.28683 21.80924
The importance values are ordered by the highest to lowest values. But I was wondering if it's possible to reorder the imp
values to match the order of the feature names given by name
(in the above example).
Taking a look at the structure of both name
and imp
tells me:
str(name)
str(imp)
> str(name)
chr [1:5] "Solar.R" "Wind" "Temp" "Month" "Day"
> str(imp)
Named num [1:5] 597.4 455.7 112.3 30.3 21.8
- attr(*, "names")= chr [1:5] "Temp" "Wind" "Solar.R" "Month" ...
I figured out a way to reorder the named numeric imp
. This did the trick:
imp[order(factor(names(imp), levels = name))]
Please be aware that $fature_names
is not guaranteed to be in the same order as in the underlying data. So if you want to make sure that the order is the same as in the data then you could do the following:
library(mlr3)
library(mlr3learners)
#Get data
aq <- data.frame(airquality)
aq <- na.omit(aq)
# Create mlr3 task and model
aq_T <- TaskRegr$new(id = "aq", backend = aq, target = "Ozone")
aqLrn <- lrn("regr.ranger", importance = "permutation")
aq_M <- aqLrn$train(aq_T)
imp <- aq_M$importance()
imp[intersect(aq_T$backend$colnames, names(imp))]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.