Skip to content

Commit

Permalink
feat: add feature_ranking method
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed May 31, 2024
1 parent 376f5d4 commit 62011f3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
21 changes: 19 additions & 2 deletions R/EnsembleFSResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,26 @@ EnsembleFSResult = R6Class("EnsembleFSResult",
},

#' @description
#' Returns the feature ranking.
feature_ranking = function() {
#' Calculates the feature ranking.
#'
#' @param method (`character(1)`)\cr
#' The method to calculate the feature ranking.
#' Currently, only `"inclusion_probability"` is supported.
feature_ranking = function(method = "inclusion_probability") {
assert_choice(method, choices = "inclusion_probability")

features = self$benchmark_result$tasks$task[[1]]$feature_names

count = map_int(features, function(feature) {
sum(map_lgl(self$result$features, function(iteration) {
feature %in% iteration
}))
})

res = data.table(feature = features, inclusion_probability = count / nrow(self$result))
setorderv(res, "inclusion_probability", order = -1L)

res
},

#' @description
Expand Down
13 changes: 11 additions & 2 deletions man/EnsembleFSResult.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/mlr3fselect-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions tests/testthat/test_ensemble_fselect.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,9 @@ test_that("ensemble feature selection works", {
expect_vector(efsr$result$classif.ce, size = 4)
expect_list(efsr$result$importance, any.missing = FALSE, len = 4)
expect_benchmark_result(efsr$benchmark_result)
})

test_that("stability method works", {
efsr = ensemble_fselect(
fselector = fs("rfe", n_features = 2, feature_fraction = 0.8),
task = tsk("sonar"),
learners = lrns(c("classif.rpart", "classif.featureless")),
init_resampling = rsmp("subsampling", repeats = 2),
inner_resampling = rsmp("cv", folds = 3),
measure = msr("classif.ce"),
terminator = trm("none")
)

expect_number(efsr$stability(stability_measure = "jaccard"))
feature_ranking = efsr$feature_ranking()
expect_data_table(feature_ranking, nrows = 60)
expect_names(names(feature_ranking), identical.to = c("feature", "inclusion_probability"))
})


0 comments on commit 62011f3

Please sign in to comment.