-
-
Notifications
You must be signed in to change notification settings - Fork 28
filterensemble #796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
filterensemble #796
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
79e1f4a
filterensemble first draft
mb706 9647525
small fixes
mb706 92f7b01
Merge branch 'master' into filterensemble
mb706 9a5d9f2
document
mb706 30ea811
document
mb706 3f0f2a3
Merge branch 'master' into filterensemble
mb706 84c9039
document
mb706 b9c7617
Document and test FilterEnsemble
mb706 60b22b6
more tests
mb706 eb4c3a6
Improve FilterEnsemble weight handling and tests
mb706 b0dc285
Document TuneToken behavior for custom checks
mb706 1af5dc8
more tests
mb706 5addc3b
repair examples requirement nosuggest
mb706 24ef41b
NEWS
mb706 cbdb747
Merge branch 'master' into filterensemble
mb706 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
|
|
||
|
|
||
| #' @title Filter Ensemble | ||
| #' | ||
| #' @usage NULL | ||
| #' @name mlr_filters_ensemble | ||
| #' @format [`R6Class`][R6::R6Class] object inheriting from [`Filter`][mlr3filters::Filter]. | ||
| #' | ||
| #' @description | ||
| #' Implements the filte rensemble proposed in `r cite_bib("binder_2020")`. | ||
|
|
||
| FilterEnsemble = R6Class("FilterEnsemble", inherit = mlr3filters::Filter, | ||
| public = list( | ||
| initialize = function(filters) { | ||
| private$.wrapped = assert_list(filters, types = "Filter", min.len = 1) | ||
| fnames = map_chr(private$.wrapped, "id") | ||
| names(private$.wrapped) = fnames | ||
| types_list = map(discard(private$.wrapped, function(x) test_scalar_na(x$task_types)), "task_types") | ||
| if (length(types_list)) { | ||
| task_types = Reduce(intersect, types_list) | ||
| } else { | ||
| task_types = NA_character_ | ||
| } | ||
| .own_param_set = ps( | ||
| weights = p_uty(custom_check = crate(function(x) { | ||
| check_numeric(x, len = length(fnames)) %check&&% | ||
| (check_names(names(x), type = "unnamed") %check||% | ||
| check_names(names(x), type = "unique", permutation.of = fnames)) | ||
| }, fnames), | ||
| tags = "required" | ||
| ), | ||
| rank_transform = p_lgl(init = FALSE, tags = "required") | ||
| ) | ||
|
|
||
| super$initialize( | ||
| id = paste(fnames, collapse = "."), | ||
| task_types = task_types, | ||
| task_properties = unique(unlist(map(private$.wrapped, "task_properties"))), | ||
| param_set = ParamSetCollection$new(c(list(.own_param_set), map(private$.wrapped, "param_set"))), | ||
| feature_types = Reduce(intersect, map(private$.wrapped, "feature_types")), | ||
| packages = unique(unlist(map(private$.wrapped, "packages"))), | ||
| label = "meta", | ||
| man = "mlr3pipelines::mlr_filters_ensemble" | ||
| ) | ||
| }, | ||
| get_weights_tunetoken = function(normalize_weights = "uniform") { | ||
| assert_choice(normalize_weights, c("uniform", "naive", "no")) | ||
| paradox::to_tune(self$get_weights_search_space(normalize_weights = normalize_weights)) | ||
| }, | ||
| set_weights_to_tune = function(normalize_weights = "uniform") { | ||
| assert_choice(normalize_weights, c("uniform", "naive", "no")) | ||
| self$param_set$set_values(.values = list(weights = self$get_weights_tunetoken(normalize_weights = normalize_weights))) | ||
| invisible(self) | ||
| }, | ||
| get_weights_search_space = function(weights_param_name = "weights", normalize_weights = "uniform", prefix = "w") { | ||
| assert_string(prefix) | ||
| assert_string(weights_param_name) | ||
| assert_choice(normalize_weights, c("uniform", "naive", "no")) | ||
| fnames = names(private$.wrapped) | ||
| innames = if (prefix == "") fnames else paste0(prefix, ".", fnames) | ||
| domains = rep(list(p_dbl(0, 1)), length(fnames)) | ||
| names(domains) = innames | ||
|
|
||
| domains$.extra_trafo = crate(function(x) { | ||
| w = unlist(x[innames], use.names = FALSE) | ||
| names(w) = fnames | ||
| x[innames] = NULL | ||
|
|
||
| if (normalize_weights == "uniform") { | ||
| w[w > 1 - .Machine$double.eps] = 1 - .Machine$double.eps | ||
| w = -log1p(-w) | ||
| w = w / max(sum(w), .Machine$double.eps) | ||
| } else if (normalize_weights == "naive") { | ||
| w = w / max(sum(w), .Machine$double.eps) | ||
| } | ||
| x[[weights_param_name]] = w | ||
| x | ||
| }, innames, fnames, normalize_weights, weights_param_name) | ||
|
|
||
| do.call(paradox::ps, domains) | ||
| } | ||
| ), | ||
| private = list( | ||
| .wrapped = NULL, | ||
| .own_param_set = NULL, | ||
| .calculate = function(task, nfeat) { | ||
| pv = private$.own_param_set$get_values() | ||
| fn = task$feature_names | ||
| nfeat = length(fn) # need to rank all features in an ensemble | ||
| weights = pv$weights | ||
| wnames = names(private$.wrapped) | ||
| if (!is.null(names(weights))) { | ||
| weights = weights[wnames] | ||
| } | ||
| scores = pmap(list(private$.wrapped, weights), function(x, w) { | ||
| x$calculate(task, nfeat) | ||
| s = x$scores[fn] | ||
| if (pv$rank_transform) s = rank(s) | ||
| s * w | ||
| }) | ||
| structure(rowSums(as.data.frame(scores)), names = fn) | ||
| } | ||
| ) | ||
|
|
||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the history of PipeOpLearner, before the PipeOp base class handled paramset collection / alist stuff properly. See there how cloning for paramset works. test this thoroughly.